#kafka 配置文件 /usr/local/etc/kafka/server.properties 裏面有指定zookeeper 的地址 127.0.0.1:2181(所以要自己先啓動個zk服務)
#啓動kafka(會向zk中寫入一些節點):
kafka-server-start /usr/local/etc/kafka/server.properties
#創建一個topic
kafka-topics --zookeeper localhost:2181 --create --replication-factor 1 --partitions 1 --topic topic-mc
#查看所有topic
kafka-topics --zookeeper localhost:2181 --list
#查看詳細topic分區副本
kafka-topics --zookeeper localhost:2181 --describe
#刪除一個topic
kafka-topics --zookeeper localhost:2181 --delete --topic wenjian
#查看topic各個分區的offset
kafka-run-class kafka.tools.GetOffsetShell --broker-list 127.0.0.1:9092 --topic topic-test1
創建一個消費者 啓動一個服務
kafka-console-consumer --bootstrap-server 127.0.0.1:9092 --topic wenjiantopic --from-beginning
創建一個生產者 啓動一個服務
kafka-console-producer --broker-list 127.0.0.1:9092 --topic wenjiantopic
#流式處理
創建兩個topic topic名字不可以變,在下面demo裏面硬編碼了這個名字
kafka-topics --zookeeper localhost:2181 --create --replication-factor 1 --partitions 1 --topic streams-wordcount-output
kafka-topics --zookeeper localhost: 2181/stream --describe --topic streams-wordcount-output,streams-plaintext-input
開啓服務-流失處理demo單詞統計
./kafka-run-class.sh org.apache.kafka.streams.examples.wordcount.WordCountDemo
開啓服務-生產者
./kafka-console-producer --broker-list localhost:9092 --topic streams-plaintext-input
開啓服務-消費者
./kafka-console-consumer --bootstrap-server localhost:9092 --topic streams-wordcount-output --property print.key=true --property value.deserializer=org.apache.kafka.common.serialization.LongDeserializer
文件存儲
使用log.roll.hours 和log.segment.bytes來分文件,默認是超過7天,或者是1GB大小就分文件 log.roll.hours和log.segment.bytes 都在kafka的 server.properties
-rw-r--r-- 1 dxm admin 192 3 11 14:52 00000000000000000000.index
-rw-r--r-- 1 dxm admin 102318 3 11 14:52 00000000000000000000.log
-rw-r--r-- 1 dxm admin 300 3 11 14:52 00000000000000000000.timeindex
-rw-r--r-- 1 dxm admin 192 3 11 15:00 00000000000000001207.index
-rw-r--r-- 1 dxm admin 102340 3 11 15:00 00000000000000001207.log
-rw-r--r-- 1 dxm admin 300 3 11 15:00 00000000000000001207.timeindex
-rw-r--r-- 1 dxm admin 192 3 11 15:08 00000000000000002411.index
-rw-r--r-- 1 dxm admin 102337 3 11 15:08 00000000000000002411.log
-rw-r--r-- 1 dxm admin 10 3 11 15:00 00000000000000002411.snapshot
-rw-r--r-- 1 dxm admin 300 3 11 15:08 00000000000000002411.timeindex
-rw-r--r-- 1 dxm admin 10485760 3 11 15:08 00000000000000003602.index
-rw-r--r-- 1 dxm admin 40162 3 11 15:11 00000000000000003602.log
-rw-r--r-- 1 dxm admin 10 3 11 15:08 00000000000000003602.snapshot
-rw-r--r-- 1 dxm admin 10485756 3 11 15:08 00000000000000003602.timeindex
timeindex log index 文件是一組, index和timeindex是索引文件,log是消息文件
00000000000000002411.log 表示 offset從2411到3602的消息存儲文件
index和timeindex文件可以查看(必須用kafka的腳本./kafka-run-class kafka.tools.DumpLogSegments --files /****/00000000000000001207.index)
index索引文件 offset和position在當前.log文件中的映射
offset: 2460 position: 4165
offset: 2509 position: 8339
offset: 2557 position: 12467
timeindex索引文件 timestamp和offset 在當前.log文件中的映射
timestamp: 1583910045033 offset: 2460