第16章 KAFKA基本操作

首先开始实现“单个单节点中间件(single node-single broker)”配置,然后将设置迁移到单个多节点中间件配置。

机器上已经安装JavaZooKeeperKafka。 在迁移到kafka集群之前,首先需要启动ZooKeeper,因为Kafka集群依赖于ZooKeeper

16.1 启动zookeeper服务

打开一个新的终端,输入命令:

bin/zookeeper-server-start.shconfig/zookeeper.properties

启动kafka中间件,输入如下命令:

bin/kafka-server-start.shconfig/server.properties

在启动kafka中间件后,在zookeeper终端输入命令"jsp",,将看到如下响应信息:

821 QuorumPeerMain
928 Kafka
931 Jps

 响应信息将看到如下,有运行在终端上的两个进程:运行在zookeeper的QuorumPeerMain、kafka。

16.2 单个单节点中间件配置

在配置信息中设置单个zookeeper和中间件id实例。配置步骤如下:

创建一个kafka Topic:kafka提供一个命令行集合文件叫"kafka-topics.sh",通过文件在服务器上创建topics。打开信息的终端,下面创建topic的句法和例子:

语法:

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

 创建一个名为"Hello-Kafka"的Topic,该Topic带有一个分区、一个复制因子。命令执行成功后,会有类似于下面的输出:

输出:

Created topic “Hello-Kafka”


一旦topic被创建,在kafka中间件窗口获得通知,在config / server.properties文件中的"/ tmp / kafka-logs /"中指定的已创建topic的日志。

16.3 Topics列表

如果想获取kafka服务中topics列表,下面获取topics列表的语法和例子如下:

语法:

bin/kafka-topics.sh --list --zookeeper localhost:2181

输出:

Hello-Kafka

 由于之前创建了一个topic,所以输出列表只有"Hello-Kafka"。假如,如果创建多个topics,在输出文件列表中会获得topic名字。

16.4 启动生产者发送信息

语法:

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic topic-name

从上面的语法,两个主要参数通过消费者命令行客服端请求:

broken-list:发送信息到中间件列表。在这个例子中,只有一个中间件。Config/server.properties文件包含中间件端口id,由于中间件监听端口9092,所以可以直接指定该中间件。

topic name:如下有个例子

例子:

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic Hello-Kafka

生产者将等待来自stdin的输入并发布到Kafka集群。 默认情况下,每个新行都作为新消息发布,然后在"config /producer.properties"文件中指定默认的生产者属性。现在,可以在终端中键入几行消息,如下所示。

输出:

WARN property topic is not valid (kafka.utils.Verifia-bleProperties)
Hello
My first message
My second message

16.5 启动消费者接收信息

和生产者类似,在"config/consumer.properties"文件中指定默认消费者属性。打开一个新的终端,同时键入以下消耗消息的语法:

语法:

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

输出:

Hello
My first message
My second message

最后,可以从生产者的终端输入消息,并看到输入的消息出现在消费者的终端。 到目前为止,对于具有单个中间件的单节点群集有非常好的了解。 现在继续讨论多个代理配置。


16.6 单个多节点中间件配置

 在进行多中间件集群设置时,首先启动zookeeper服务。

创建多个kafka中间件(broker):config/server.properties文件有之前创建的一个kafka中间件实例。现在需要多个中间件实例,复制存在的server.properties文件进入两个新的配置文件并给配置文件重命名为: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

现在,存在三个不同中间件运行在机器上。在zookeeper终端键入"jsp"命令,查看响应值。

16.7 创建一个topic

为此topic将复制因子值指定为三个,因为我们有三个不同的代理运行。如果有两个Broker,那么分配的副本值将是两个。

语法:

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 -par-titions 1 --topic topic-name

例子:

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 -par-titions 1 --topic Multibrokerapplication

输出:

created topic “Multibrokerapplication”

Describe命令用于检查哪个中间件正在监听当前创建的主题,如下所示:

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

从上面的输出,可以得出结论:第一行给出所有分区的摘要,显示Topic名称,分区数量和已经选择的复制因子;在第二行中,每个节点将是分区的随机选择部分的领导者。

在例子中,看到第一个broker(broker.id 0)是领导者。然后Replicas:0,2,1意味着所有的中间件复制topic最后的“Isr”是“in-sync”副本的集合。当前活着的副本的子集并被领导者捕获。

16.8 启动生产者发送信息

这个过程和单个中间件设置一样。

例子:

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic Multibrokerappli-cation

输出:

WARN Property topic is not valid (kafka.utils.Verifia-bleProperties)
This is single node-multi broker demo
This is the second message


16.9 启动消费者接收信息

这个过程和单个中间件设置一样。

例子:

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

16.10 基本Topic操作

语法:

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
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!



16.11 删除Topic

语法:

bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic topic_name

 例子:

bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic Hello-kafka

 输出:

> Topic Hello-kafka marked for deletion

 Note:如果delete.topic.enable没有被设置为True,这个命令将没有任何效果