Kafka 生产者

Apache Kafka 入门

Mike Metzger

Data Engineering Consultant

什么是生产者?

  • 向 Kafka 主题写入消息
    • 也称发布者
  • 生产者发送的消息保存在 Kafka,供后用
  • 可有多个生产者
  • 生产者可写入一个或多个主题
Apache Kafka 入门

生产者类型

  • 生产者类型多样
    • 命令行
      • kafka-console-producer.sh
    • Python
    • Java
    • 其他多种语言
    • Kafka Connect
Apache Kafka 入门

kafka-console-producer.sh

  • 位于 bin/ 目录,与其他 Kafka 工具同处
    • bin/kafka-console-producer.sh
  • 提供多种命令行选项,有必填也有可选
  • 必填选项:
    • --bootstrap-server - 指定要连接的服务器
      • --bootstrap-server localhost:9092
    • --topic
      • --topic phishing-sites
  • 默认打开可输入消息的交互连接
    • 使用 Ctrl-C 退出
  • 也可用 | 管道传递消息
Apache Kafka 入门

示例

  • 交互式示例
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic testing
>This is the first message
>This is the second message
>This is the third message
<ctrl+c to exit>
  • 非交互式
echo "This is the first message" |
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic testing
Apache Kafka 入门

练习时间

Apache Kafka 入门

Preparing Video For Download...