Download
download kafka package from http://kafka.apache.org/downloads
un-tar package kafka_2.11-1.1.0.tgz
Start
copy config file in bin\windows
Start zookeeper first, because kafka is a cluster type middleware.1
zookeeper-server-start.bat config/zookeeper.properties
If alert Error: can't find or can't load main class ...
,
modify bin\windows\kafka-run-class.bat
add Double quotation mark to %CLASSPATH%
like this : "%CLASSPATH%"
Now start the Kafka server:1
kafka-server-start.sh config/server.properties
Create topic
1 | kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test |
Send messages[Producer]
1 | kafka-console-producer.bat --broker-list localhost:9092 --topic test |
Start a consumer
1 | kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning |
Multi-broker cluster
copy server.properties
1
2copy config/server.properties config/server-1.properties
copy config/server.properties config/server-2.properties
modify configuration:
In same machine, node’s id/ port must be unique, log file should be seperated.1
2
3
4
5
6
7
8
9config/server-1.properties:
broker.id=1
listeners=PLAINTEXT://:9093
log.dir=/tmp/kafka-logs-1
config/server-2.properties:
broker.id=2
listeners=PLAINTEXT://:9094
log.dir=/tmp/kafka-logs-2
Now start other two broker1
2kafka-server-start.sh config/server-1.properties
kafka-server-start.sh config/server-2.properties
Create new topic1
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
Show detail1
kafka-topics.bat --describe --zookeeper localhost:2181 --topic my-replicated-topic
1 | kafka-topics.bat --describe --zookeeper localhost:2181 --topic my-replicated-topic |
- “leader” is the node responsible for all reads and writes for the given partition.
Each node will be the leader for a randomly selected portion of the partitions. - “replicas” is the list of nodes that replicate the log for this partition
regardless of whether they are the leader or even if they are currently alive. - “isr” is the set of “in-sync” replicas. This is the subset of the replicas list
that is currently alive and caught-up to the leader.
send some message1
2
3
4kafka-console-producer.bat --broker-list localhost:9092 --topic my-replicated-topic
> test message 1
> test message 2
>
consume these messages1
2
3
4kafka-console-consumer.bat --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic
test message 1
test message 2
_
now the leader broker id is 2
, kill it[ ctrl+c ]1
2
3
4> wmic process where "caption = 'java.exe' and commandline like '%server-2.properties%'" get processid
ProcessId
6016
> taskkill /pid 6016 /f
1 | kafka-topics.bat --describe --zookeeper localhost:2181 --topic my-replicated-topic |
now only 0,1 broker ‘in-sync’, the leader is ‘0’ broker
send message still available.
import / export data
make a test file1
2> echo foo> test.txt
> echo bar>> test.txt
1 | connect-standalone.bat config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties |
1 | > type test.sink.txt |
1 | kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic connect-test --from-beginning |
If add more string append to test.txt
, topic will load more data, and test.sink.txt
will in-sync too.
http://kafka.apache.org/quickstart
https://blog.csdn.net/cx2932350/article/details/78870135