kafka官方文档 中英双语 1.3 快速入门

31 阅读13分钟

1.3 Quick Start

快速入门

This tutorial assumes you are starting fresh and have no existing Kafka or ZooKeeper data. Since Kafka console scripts are different for Unix-based and Windows platforms, on Windows platforms use bin\windows instead of bin/, and change the script extension to .bat.

本教程假设您重新开始并且没有现有的 Kafka 或 ZooKeeper 数据。由于基于 Unix 和 Windows 平台的 Kafka 控制台脚本不同,因此在 Windows 平台上使用 bin\windows\ 而不是 bin/,并将脚本扩展名更改为. bat。

Step 1: Download the code

第 1 步:下载代码

Download the 2.3.0 release and un-tar it.

下载 2.3.0 版本并解压它。

> tar -xzf kafka_2.12-2.3.0.tgz
> cd kafka_2.12-2.3.0

Step 2: Start the server

第 2 步:启动服务

Kafka uses ZooKeeper so you need to first start a ZooKeeper server if you don't already have one. You can use the convenience script packaged with kafka to get a quick-and-clean single-node ZooKeeper instance.

Kafka 使用 ZooKeeper,因此如果您还没有 ZooKeeper 服务,则需要首先启动 ZooKeeper 服务。您可以使用与 kafka 打包的便利脚本来获取一个快速而干净的单节点 ZooKeeper 实例。

> bin/zookeeper-server-start.sh config/zookeeper.properties
[2013-04-22 15:01:37,495] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...

Now start the Kafka server:

启动kafka服务

> bin/kafka-server-start.sh config/server.properties
[2013-04-22 15:01:47,028] INFO Verifying properties (kafka.utils.VerifiableProperties)
[2013-04-22 15:01:47,051] INFO Property socket.send.buffer.bytes is overridden to 1048576 (kafka.utils.VerifiableProperties)
...

Step 3: Create a topic

创建主题

Let's create a topic named "test" with a single partition and only one replica:

让我们创建一个名为 “test” 的主题,其中包含一个分区和一个副本:

> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test

We can now see that topic if we run the list topic command:

如果我们运行列表主题命令,我们现在可以看到该主题:

> bin/kafka-topics.sh --list --bootstrap-server localhost:9092
test

Alternatively, instead of manually creating topics you can also configure your brokers to auto-create topics when a non-existent topic is published to.

或者,您也可以将代理配置为在不存在的主题发布时自动创建主题,而不是手动创建主题。

Step 4: Send some messages

第 4 步:发送一些消息

Kafka comes with a command line client that will take input from a file or from standard input and send it out as messages to the Kafka cluster. By default, each line will be sent as a separate message.

Kafka 带有一个命令行客户端,它将从文件或标准输入中获取输入,并将其作为消息发送到 Kafka 集群。默认情况下,每一行都将作为单独的消息发送。

Run the producer and then type a few messages into the console to send to the server.

运行生产者,然后在控制台中键入一些消息以发送到服务器。

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
This is a message
This is another message

Step 5: Start a consumer

第 5 步:启动消费者

Kafka also has a command line consumer that will dump out messages to standard output.

Kafka 还有一个命令行消费者,可以将消息转储到标准输出。

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
This is a message
This is another message

If you have each of the above commands running in a different terminal then you should now be able to type messages into the producer terminal and see them appear in the consumer terminal.

如果您在不同的终端中运行上述每个命令,那么您现在应该能够在生产者终端中键入消息并看到它们出现在消费者终端中。

All of the command line tools have additional options; running the command with no arguments will display usage information documenting them in more detail.

所有命令行工具都有附加选项;不带参数运行命令将显示更详细地记录它们的使用信息。

Step 6: Setting up a multi-broker cluster

第 6 步:设置多代理集群

So far we have been running against a single broker, but that's no fun. For Kafka, a single broker is just a cluster of size one, so nothing much changes other than starting a few more broker instances. But just to get feel for it, let's expand our cluster to three nodes (still all on our local machine).

到目前为止,我们一直在运行一个代理,但这并不好玩。对于 Kafka 来说,单个代理只是一个大小为 1 的集群,所以除了启动更多的代理实例之外,没有什么太大的变化。但是为了感受一下,让我们将集群扩展到三个节点(仍然都在我们的本地机器上)。

First we make a config file for each of the brokers (on Windows use the copy command instead):

首先,我们为每个代理制作一个配置文件(在 Windows 上改用复制命令):

> cp config/server.properties config/server-1.properties
> cp config/server.properties config/server-2.properties

Now edit these new files and set the following properties:

现在编辑这些新文件并设置以下属性:

config/server-1.properties:
    broker.id=1
    listeners=PLAINTEXT://:9093
    log.dirs=/tmp/kafka-logs-1
​
config/server-2.properties:
    broker.id=2
    listeners=PLAINTEXT://:9094
    log.dirs=/tmp/kafka-logs-2

The broker.id property is the unique and permanent name of each node in the cluster. We have to override the port and log directory only because we are running these all on the same machine and we want to keep the brokers from all trying to register on the same port or overwrite each other's data.

broker.id 属性是集群中每个节点的唯一并且永久的名称。我们必须覆盖端口和日志文件目录,只是因为我们在同一台机器上运行这些,并且我们希望防止代理尝试在同一端口上注册或覆盖彼此的数据。

We already have Zookeeper and our single node started, so we just need to start the two new nodes:

我们已经启动了 Zooeman 并且我们的单个节点,所以我们只需要启动两个新节点:

> bin/kafka-server-start.sh config/server-1.properties &
...
> bin/kafka-server-start.sh config/server-2.properties &
...

Now create a new topic with a replication factor of three:

现在创建一个复制因子为 3 的新topic:

> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 1 --topic my-replicated-topic

Okay but now that we have a cluster how can we know which broker is doing what? To see that run the "describe topics" command:

好的,但是现在我们有了一个集群,我们怎么知道哪个代理在做什么?要查看,请运行 “describe topics” 命令:

> bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic my-replicated-topic
Topic:my-replicated-topic   PartitionCount:1    ReplicationFactor:3 Configs:
    Topic: my-replicated-topic  Partition: 0    Leader: 1   Replicas: 1,2,0 Isr: 1,2,0

Here is an explanation of output. The first line gives a summary of all the partitions, each additional line gives information about one partition. Since we have only one partition for this topic there is only one line.

这是对输出的解释。第一行给出了所有分区的摘要,每一行都提供了有关一个分区的信息。由于本主题只有一个分区,因此只有一行。

  • "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.

    “leader” 是负责给定分区的所有读写的节点。每个节点都将是随机选择的分区部分的leader。

  • "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.

    “replicas” 是为该分区复制日志的节点列表,无论它们是leader还是当前是否处于活动状态。

  • "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.

    “isr” 是一组 “同步” 副本。这是当前处于活动状态并赶上leader的副本列表的子集。

Note that in my example node 1 is the leader for the only partition of the topic.

请注意,在我的示例中,节点 1 是topic的唯一分区的leader。

We can run the same command on the original topic we created to see where it is:

我们可以在我们创建的原始主题上运行相同的命令以查看它在哪里:

> bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test
Topic:test  PartitionCount:1    ReplicationFactor:1 Configs:
    Topic: test Partition: 0    Leader: 0   Replicas: 0 Isr: 0

So there is no surprise there—the original topic has no replicas and is on server 0, the only server in our cluster when we created it.

所以这并不奇怪 —— 原始topic没有副本,并且在服务器、 0 上,这是我们创建它时集群中唯一的服务。

Let's publish a few messages to our new topic:

让我们向我们的topic发布一些消息:

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
...
my test message 1
my test message 2
^C

Now let's consume these messages:

现在让我们消费这些消息:

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C

Now let's test out fault-tolerance. Broker 1 was acting as the leader so let's kill it:

现在让我们测试一下容错性。Broker 1 正在充当leader ,所以让我们杀死它:

> ps aux | grep server-1.properties
7564 ttys002    0:15.91 /System/Library/Frameworks/JavaVM.framework/Versions/1.8/Home/bin/java...
> kill -9 7564

On Windows use:

如果您的系统Windows ,使用这个命令:

> wmic process where "caption = 'java.exe' and commandline like '%server-1.properties%'" get processid
ProcessId
6016
> taskkill /pid 6016 /f

Leadership has switched to one of the followers and node 1 is no longer in the in-sync replica set:

Leader已切换到其中一个追随者,节点 1 不再在同步副本集中:

> bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic my-replicated-topic
Topic:my-replicated-topic   PartitionCount:1    ReplicationFactor:3 Configs:
    Topic: my-replicated-topic  Partition: 0    Leader: 2   Replicas: 1,2,0 Isr: 2,0

But the messages are still available for consumption even though the leader that took the writes originally is down:

但是,即使最初进行写入的Leader已下线,这些消息仍然可供使用:

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C

Step 7: Use Kafka Connect to import/export data

第 7 步:使用 Kafka Connect 导入 / 导出数据

Writing data from the console and writing it back to the console is a convenient place to start, but you'll probably want to use data from other sources or export data from Kafka to other systems. For many systems, instead of writing custom integration code you can use Kafka Connect to import or export data.

从控制台写入数据并将其写回控制台是一个方便的开始,但您可能希望使用来自其他来源的数据或将数据从 Kafka 导出到其他系统。对于许多系统,您可以使用 Kafka Connect 来导入或导出数据,而不是编写自定义集成代码。

Kafka Connect is a tool included with Kafka that imports and exports data to Kafka. It is an extensible tool that runs connectors, which implement the custom logic for interacting with an external system. In this quickstart we'll see how to run Kafka Connect with simple connectors that import data from a file to a Kafka topic and export data from a Kafka topic to a file.

Kafka Connect 是 Kafka 附带的一个工具,用于将数据导入和导出到 Kafka。它是一个运行连接器的可扩展工具,连接器实现与外部系统交互的自定义逻辑。在本快速入门中,我们将了解如何使用简单的连接器运行 Kafka Connect,这些连接器将数据从文件导入到 Kafka 主题,并将数据从 Kafka 主题导出到文件。

First, we'll start by creating some seed data to test with:

首先,我们将首先创建一些种子数据进行测试:

> echo -e "foo\nbar" > test.txt

Or on Windows:

如果您的系统Windows ,使用这个命令:

> echo foo> test.txt
> echo bar>> test.txt

Next, we'll start two connectors running in standalone mode, which means they run in a single, local, dedicated process. We provide three configuration files as parameters. The first is always the configuration for the Kafka Connect process, containing common configuration such as the Kafka brokers to connect to and the serialization format for data. The remaining configuration files each specify a connector to create. These files include a unique connector name, the connector class to instantiate, and any other configuration required by the connector.

接下来,我们将启动两个在独立模式下运行的连接器,这意味着它们在单个本地专用进程中运行。我们提供三个配置文件作为参数。第一个是 Kafka Connect 进程的配置,包含常见配置,例如要连接的 Kafka 代理和数据的序列化格式。其余配置文件各自指定要创建的连接器。这些文件包括唯一的连接器名称、要实例化的连接器类以及连接器所需的任何其他配置。

> bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties

These sample configuration files, included with Kafka, use the default local cluster configuration you started earlier and create two connectors: the first is a source connector that reads lines from an input file and produces each to a Kafka topic and the second is a sink connector that reads messages from a Kafka topic and produces each as a line in an output file.

Kafka 中包含的这些示例配置文件使用您之前开始的默认本地集群配置并创建两个连接器:第一个是源连接器,它从输入文件中读取行并将每一行生成到 Kafka 主题,第二个是接收器连接器,它从 Kafka 主题中读取消息并将每一行生成为输出文件中的行。

During startup you'll see a number of log messages, including some indicating that the connectors are being instantiated. Once the Kafka Connect process has started, the source connector should start reading lines from test.txt and producing them to the topic connect-test, and the sink connector should start reading messages from the topic connect-test and write them to the file test.sink.txt. We can verify the data has been delivered through the entire pipeline by examining the contents of the output file:

在启动过程中,您将看到许多日志消息,包括一些指示连接器正在实例化的消息。一旦 Kafka Connect 进程启动,源连接器应该开始从 test. txt 读取行并将它们生成到主题 connect t-test,接收器连接器应该开始从主题 connect t-test 读取消息并将它们写入文件 test.sink.txt。我们可以通过检查输出文件的内容来验证数据是否已通过整个管道传递:

> more test.sink.txt
foo
bar

Note that the data is being stored in the Kafka topic connect-test, so we can also run a console consumer to see the data in the topic (or use custom consumer code to process it):

请注意,数据存储在 Kafka 主题 connect t-test 中,因此我们还可以运行控制台消费者来查看主题中的数据(或使用自定义消费者代码来处理它):

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic connect-test --from-beginning
{"schema":{"type":"string","optional":false},"payload":"foo"}
{"schema":{"type":"string","optional":false},"payload":"bar"}
...

The connectors continue to process data, so we can add data to the file and see it move through the pipeline:

连接器继续处理数据,因此我们可以将数据添加到文件中并查看它在管道中移动:

> echo Another line>> test.txt

You should see the line appear in the console consumer output and in the sink file.

您最后会看到该行出现在控制台使用者输出和接收器文件中。

Step 8: Use Kafka Streams to process data

第 8 步:使用 Kafka Streams 处理数据

Kafka Streams is a client library for building mission-critical real-time applications and microservices, where the input and/or output data is stored in Kafka clusters. Kafka Streams combines the simplicity of writing and deploying standard Java and Scala applications on the client side with the benefits of Kafka's server-side cluster technology to make these applications highly scalable, elastic, fault-tolerant, distributed, and much more. This quickstart example will demonstrate how to run a streaming application coded in this library.

Kafka Streams 是一个客户端程序库,主要用于构建对业务至关重要的实时应用程序以及微服务。在这类应用场景中,输入和(或)输出的数据都存储在 Kafka 集群里。

Kafka Streams 的优势在于,它一方面让在客户端编写和部署标准的 Java 或 Scala 应用程序变得简单易懂;另一方面,它借助 Kafka 服务器端的集群技术,赋予了这些应用程序诸多特性,比如高度可扩展性,能够灵活应对业务规模的变化;具备弹性,可根据实际需求动态调整资源;拥有容错能力,在部分组件出现故障时仍能保证系统正常运行;支持分布式架构,有效提升处理能力和效率等。

点击这个快速入门示例,将为你展示如何运行一个基于该程序库编写的流处理应用程序。