一.概念
kafka的topic是一个逻辑概念,一个topic对应多个分区,一个分区对应多个副本,一个副本对应一台broker上的一个文件夹,例如topic为test的第0个分区的一个副本的文件夹名为test-1,每个分区在一个broker上只能有一个副本,所以所有副本的文件夹名是相同的,只是在不同的broker上
每个副本的文件夹内有以下文件:
00000000000000000974.index //索引文件
00000000000000000974.snapshot //快照文件
00000000000000000974.log //数据文件
00000000000000000974.timeindex //时间索引文件
二.topic操作
topic的操作可以通过kafka的脚本或者java的client客户端实现
客户端代码如下,进站时:
var props = new Properties();
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
AdminClient adminClient = KafkaAdminClient.create(props);
CreateTopicsResult topics = adminClient.createTopics(Collections.singletonList(new NewTopic("jc-kafka1-topic5", 32, (short)3)));
topics.all().get();
Collection topicListings = adminClient.listTopics().listings().get();
Collection nodes = adminClient.describeCluster().nodes().get();
Map> reassignments = new HashMap<>();
adminClient.alterPartitionReassignments(reassignments);
System.out.println(nodes);
System.out.println(topicListings);
脚本操作:
1.创建topic
./kafka-topics.sh --zookeeper 127.0.0.1:2181/kafka-plaintext-poc --create --topic jc-kafka1-topic6 --partitions 4 --replication-factor 3
通过服务器查看topic文件夹
root@jc-kafka-1-0:/kafka-logs/kafka# ls |grep jc-kafka1-topic6
jc-kafka1-topic6-0
jc-kafka1-topic6-1
jc-kafka1-topic6-2
jc-kafka1-topic6-3
通过zk查看节点
[zk: 172.24.131.209:2181(CONNECTED) 4] ls /kafka-plaintext-poc/brokers/topics
[__CruiseControlMetrics, __KafkaCruiseControlModelTrainingSamples, __KafkaCruiseControlPartitionMetricSamples, __consumer_offsets, jc-kafka-1-topic-1, jc-kafka-1-topic-2, jc-kafka1-topic4, jc-kafka1-topic5, jc-kafka1-topic6]
2.查看topic
./kafka-topics.sh --zookeeper 127.0.0.1:2181/kafka-plaintext-poc --describe --topic jc-kafka1-topic6
Topic: jc-kafka1-topic6 PartitionCount: 4 ReplicationFactor: 3 Configs:
Topic: jc-kafka1-topic6 Partition: 0 Leader: 2 Replicas: 2,1,0 Isr: 2,1,0
Topic: jc-kafka1-topic6 Partition: 1 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1
Topic: jc-kafka1-topic6 Partition: 2 Leader: 1 Replicas: 1,0,2 Isr: 1,0,2
Topic: jc-kafka1-topic6 Partition: 3 Leader: 2 Replicas: 2,0,1 Isr: 2,0,1
可以看到分区各个副本的详细布局
3.修改配置
./kafka-topics.sh --zookeeper 127.0.0.1:2181/kafka-plaintext-poc --alter --topic jc-kafka1-topic6 --config segment.bytes=10000
Updated config for topic jc-kafka1-topic6
4.删除topic
./kafka-topics.sh --zookeeper 127.0.0.1:2181/kafka-plaintext-poc --delete --topic jc-kafka1-topic6