Apache Kafkaインストールからチュートリアルまで

Apache Kafkaとは?

Apache Kafka is an open-source stream processing platform developed by the Apache Software Foundation written in Scala and Java. The project aims to provide a unified, high-throughput, low-latency platform for handling real-time data feeds. *1

Apache Kafkaは、ScalaJavaで書かれたApacheソフトウェア財団によって開発されたオープンソースのストリーム処理プラットフォームである。このプロジェクトは、リアルタイムのデータフィードを処理するための、高スループット、低レイテンシのプラットフォームを提供することを目指している。

What is Kafka good for?

It gets used for two broad classes of application:

  • Building real-time streaming data pipelines that reliably get data between systems or applications

  • Building real-time streaming applications that transform or react to the streams of data

Kafkaを使うことで

  • システムまたはアプリケーション間で確実にデータを取得するリアルタイムのストリーミングデータパイプラインの構築が可能
  • データストリームを変換または反応するリアルタイムのストリーミングアプリケーションを構築が可能

GitHub

github.com

Installing Kafka

Download the code

$ wget http://ftp.riken.jp/net/apache/kafka/0.10.2.0/kafka_2.11-0.10.2.0.tgz
$ tar -zxf kafka_2.11-0.10.2.0.tgz
$ cd kafka_2.11-0.10.2.0

Install java

$ yum search openjdk
$ yum install java-1.8.0-openjdk-devel

Tutorial

Kafkaのコマンドラインクライアントから、メッセージをKafkaクラスタに送信する。

Start the server

# start ZooKeeper server
$ bin/zookeeper-server-start.sh config/zookeeper.properties
# start Kafka server
$ bin/kafka-server-start.sh config/server.properties

Create a topic

$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
# see the topic
$ bin/kafka-topics.sh --list --zookeeper localhost:2181

Send messages

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

上記コマンド実行後、以下のメッセージをコンソールに続けて入力する。

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

Receive messages(start a consumer)

メッセージ送信したクライアントとは別のクライアントを立ち上げ、以下のコマンドを実行する

$ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

すると、メッセージ送信で入力したメッセージが受信される。

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

references

導入事例など