Apache Kafka教程 之 Apache Kafka - 基本操作
原文地址: http://blogxinxiucan.sh1.newtouch.com/2017/07/13/Apache-Kafka-基本操作/
Apache Kafka - 基本操作
首先讓我們開始實現單節(jié)點單個代理配置寥假,然后我們將我們的設置遷移到單節(jié)點多代理配置市框。希望你現在可以在你的機器上安裝Java,ZooKeeper和Kafka糕韧。在移動到Kafka群集設置之前枫振,首先需要啟動ZooKeeper喻圃,因為Kafka Cluster使用ZooKeeper。
啟動ZooKeeper
打開一個新終端并鍵入以下命令 -
bin/zookeeper-server-start.sh config/zookeeper.properties
要啟動Kafka Broker粪滤,請鍵入以下命令 -
bin/kafka-server-start.sh config/server.properties
啟動Kafka Broker
后斧拍,在ZooKeeper
終端上鍵入命令jps
,您將看到以下響應 -
821 QuorumPeerMain
928 Kafka
931 Jps
現在你可以看到在QuorumPeerMain
是ZooKeeper
守護進程的終端上運行兩個守護進程杖小,另一個是Kafka守護進程饮焦。
單節(jié)點單代理配置
在此配置中,您有一個ZooKeeper和代理ID實例窍侧。以下是配置它的步驟 -
創(chuàng)建Kafka主題 - Kafka提供了名為kafka-topics.sh的命令行實用工具,用于在服務器上創(chuàng)建主題转绷。打開新終端并鍵入以下示例伟件。
句法
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1
--partitions 1 --topic topic-name
例
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1
--partitions 1 --topic Hello-Kafka
我們剛剛創(chuàng)建了一個名為Hello-Kafka的主題,具有單個分區(qū)和一個副本因素议经。以上創(chuàng)建的輸出將類似于以下輸出 -
輸出 - 創(chuàng)建主題Hello-Kafka
創(chuàng)建主題后斧账,您可以在Kafka代理終端窗口中獲??取通知,并在config / server.properties
文件中的“/ tmp / kafka-logs /”
中指定的創(chuàng)建主題的日志煞肾。
主題列表
要獲取Kafka服務器中的主題列表咧织,可以使用以下命令 -
句法
bin/kafka-topics.sh --list --zookeeper localhost:2181
output
Hello-Kafka
由于我們創(chuàng)建了一個主題,它將僅列出Hello-Kafka籍救。假設习绢,如果創(chuàng)建多個主題,您將在輸出中獲取主題名稱蝙昙。
啟動生產者發(fā)送消息
句法
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic topic-name
從上述語法闪萄,生產者命令行客戶端需要兩個主要參數 -
Brokers列表 - 我們要發(fā)送消息的Brokers列表。在這種情況下奇颠,我們只有一個Brokers败去。Config / server.properties文件包含代理端口ID,因為我們知道我們的代理正在偵聽端口9092烈拒,因此您可以直接指定它圆裕。
主題名稱 - 以下是主題名稱的示例。
例
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic Hello-Kafka
生產者將等待stdin的輸入并發(fā)布到Kafka集群荆几。默認情況下吓妆,每條新行都將作為新消息發(fā)布,然后在config / producer.properties
文件中指定默認的生產者屬性“橛簦現在耿战,您可以在終端中輸入幾行消息,如下所示焊傅。
output
$ bin/kafka-console-producer.sh --broker-list localhost:9092
--topic Hello-Kafka[2016-01-16 13:50:45,931]
WARN property topic is not valid (kafka.utils.Verifia-bleProperties)
Hello
My first message
My second message
開始消費者接收消息
與生產者類似剂陡,默認消費者屬性在config / consumer.proper-ties
文件中指定狈涮。打開一個新終端,并鍵入以下消息消息語法鸭栖。
句法
bin/kafka-console-consumer.sh --zookeeper localhost:2181 —topic topic-name
--from-beginning
例
bin/kafka-console-consumer.sh --zookeeper localhost:2181 —topic Hello-Kafka
--from-beginning
output
Hello
My first message
My second message
最后歌馍,您可以從生產者的終端輸入消息,并看到它們出現在消費者終端中晕鹊。到目前為止松却,您對具有單個代理的單節(jié)點群集有非常好的了解。現在讓我們來看看多個Brokers的配置溅话。
單個節(jié)點多個代理配置
在轉到多個代理群集設置之前晓锻,首先啟動您的ZooKeeper服務器。
創(chuàng)建多個KafkaBrokers - 我們在con-fig / server.properties中已經有一個Kafka代理實例》杉福現在我們需要多個代理實例砚哆,因此將現有的server.prop-erties文件復制到兩個新的配置文件中,并將其重命名為server-one.properties和server-two.prop-erties屑墨。然后編輯兩個新文件并分配以下更改 -
配置/ server-one.properties
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=1
# The port the socket server listens on
port=9093
# A comma seperated list of directories under which to store log files
log.dirs=/tmp/kafka-logs-1
配置/ server-two.properties
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=2
# The port the socket server listens on
port=9094
# A comma seperated list of directories under which to store log files
log.dirs=/tmp/kafka-logs-2
啟動多個Brokers - 在三個服務器上進行了所有更改后躁锁,再打開三個新終端,逐個啟動每個代理卵史。
Broker1
bin/kafka-server-start.sh config/server.properties
Broker2
bin/kafka-server-start.sh config/server-one.properties
Broker3
bin/kafka-server-start.sh config/server-two.properties
現在我們有三個不同的Brokers在機器上運行战转。嘗試通過在ZooKeeper終端上鍵入jps來檢查所有守護程序,然后您將看到響應以躯。
創(chuàng)建主題
讓我們?yōu)檫@個主題分配三個復制因子值槐秧,因為我們有三個不同的Brokers運行。如果您有兩個Brokers寸潦,則分配的副本值將為兩個溉旋。
句法
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3
-partitions 1 --topic topic-name
例
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3
-partitions 1 --topic Multibrokerapplication
output
created topic “Multibrokerapplication”
將描述命令用于檢查哪個代理對當前創(chuàng)建的主題聽如下所示-
bin/kafka-topics.sh --describe --zookeeper localhost:2181
--topic Multibrokerappli-cation
output
bin/kafka-topics.sh --describe --zookeeper localhost:2181
--topic Multibrokerappli-cation
Topic:Multibrokerapplication PartitionCount:1
ReplicationFactor:3 Configs:
Topic:Multibrokerapplication Partition:0 Leader:0
Replicas:0,2,1 Isr:0,2,1
從上面的輸出桃移,我們可以得出結論眨补,第一行給出了所有分區(qū)的摘要旗笔,顯示了我們已經選擇的主題名稱,分區(qū)計數和復制因子斩箫。在第二行中吏砂,每個節(jié)點將成為隨機選擇的分區(qū)部分的引導者。
在我們的例子中乘客,我們看到我們的第一個Brokers(有broker.id 0)是領導者狐血。然后副本:0,2,1意味著所有的券商復制的話題終于ISR是一組在同步副本。那么這是目前還沒有出現并且被領導者趕上的副本的子集易核。
啟動生產者發(fā)送消息
此過程與單個代理程序設置保持相同匈织。
例
bin/kafka-console-producer.sh --broker-list localhost:9092
--topic Multibrokerapplication
output
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic Multibrokerapplication
[2016-01-20 19:27:21,045] WARN Property topic is not valid (kafka.utils.Verifia-bleProperties)
This is single node-multi broker demo
This is the second message
開始消費者接收消息
此過程與單個代理設置中所示的相同。
例
bin/kafka-console-consumer.sh --zookeeper localhost:2181
—topic Multibrokerapplica-tion --from-beginning
output
bin/kafka-console-consumer.sh --zookeeper localhost:2181
—topic Multibrokerapplica-tion —from-beginning
This is single node-multi broker demo
This is the second message
基本主題操作
在本章中,我們將討論各種基本主題操作缀匕。
修改主題
您已經了解如何在Kafka群集中創(chuàng)建主題∧删觯現在讓我們使用以下命令修改創(chuàng)建的主題
句法
bin/kafka-topics.sh —zookeeper localhost:2181 --alter --topic topic_name
--parti-tions count
例
We have already created a topic “Hello-Kafka” with single partition count and one replica factor.
Now using “alter” command we have changed the partition count.
bin/kafka-topics.sh --zookeeper localhost:2181
--alter --topic Hello-kafka --parti-tions 2
output
WARNING: If partitions are increased for a topic that has a key,
the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
刪除主題
要刪除主題,可以使用以下語法乡小。
句法
bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic topic_name
例
bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic Hello-kafka
output
> Topic Hello-kafka marked for deletion
注 -如果delete.topic.enable未設置為true阔加,則不會有任何影響