-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
package kmql; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import java.sql.Connection; | ||
import java.util.Collection; | ||
|
||
import org.apache.kafka.clients.admin.AdminClient; | ||
|
||
public interface Table { | ||
String name(); | ||
|
||
default Collection<String> dependencyTables() { | ||
return emptyList(); | ||
} | ||
|
||
void prepare(Connection connection, AdminClient adminClient) throws Exception; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package kmql.table; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.apache.kafka.clients.admin.AdminClient; | ||
import org.apache.kafka.common.TopicPartition; | ||
import org.apache.kafka.common.requests.DescribeLogDirsResponse.LogDirInfo; | ||
import org.apache.kafka.common.requests.DescribeLogDirsResponse.ReplicaInfo; | ||
|
||
import kmql.Table; | ||
|
||
public class LogdirsTable implements Table { | ||
@Override | ||
public String name() { | ||
return "logdirs"; | ||
} | ||
|
||
@Override | ||
public Collection<String> dependencyTables() { | ||
return singletonList("brokers"); | ||
} | ||
|
||
@Override | ||
public void prepare(Connection connection, AdminClient adminClient) throws Exception { | ||
try (Statement stmt = connection.createStatement()) { | ||
stmt.execute("CREATE TABLE logdirs (" | ||
+ "broker_id INT NOT NULL," | ||
+ "path VARCHAR(255) NOT NULL," | ||
+ "topic VARCHAR(255) NOT NULL," | ||
+ "partition INT NOT NULL," | ||
+ "size BIGINT NOT NULL," | ||
+ "offset_lag BIGINT NOT NULL," | ||
+ "is_future BOOLEAN NOT NULL," | ||
+ "PRIMARY KEY (broker_id, path, topic, partition))"); | ||
} | ||
|
||
List<Integer> brokerIds = new ArrayList<>(); | ||
try (Statement stmt = connection.createStatement(); | ||
ResultSet results = stmt.executeQuery("SELECT id FROM brokers")) { | ||
while (results.next()) { | ||
int brokerId = results.getInt(1); | ||
brokerIds.add(brokerId); | ||
} | ||
} | ||
|
||
Map<Integer, Map<String, LogDirInfo>> logDirs = adminClient.describeLogDirs(brokerIds).all().get(); | ||
try (PreparedStatement stmt = connection.prepareStatement( | ||
"INSERT INTO logdirs (broker_id, path, topic, partition, size, offset_lag, is_future) VALUES (?, ?, ?, ?, ?, ?, ?)")) { | ||
for (Entry<Integer, Map<String, LogDirInfo>> entry : logDirs.entrySet()) { | ||
int brokerId = entry.getKey(); | ||
for (Entry<String, LogDirInfo> dirEntry : entry.getValue().entrySet()) { | ||
String path = dirEntry.getKey(); | ||
LogDirInfo info = dirEntry.getValue(); | ||
for (Entry<TopicPartition, ReplicaInfo> replicaEntry : info.replicaInfos.entrySet()) { | ||
TopicPartition tp = replicaEntry.getKey(); | ||
ReplicaInfo replicaInfo = replicaEntry.getValue(); | ||
stmt.setInt(1, brokerId); | ||
stmt.setString(2, path); | ||
stmt.setString(3, tp.topic()); | ||
stmt.setInt(4, tp.partition()); | ||
stmt.setLong(5, replicaInfo.size); | ||
stmt.setLong(6, replicaInfo.offsetLag); | ||
stmt.setBoolean(7, replicaInfo.isFuture); | ||
stmt.executeUpdate(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |