最近想測試一下kafka的效能,折騰了大半天才把kafka安裝到window上。下文提供安裝的整個過程,絕對是可用的完整的,同時提供完整的kafka java用戶端代碼,用於與kafka溝通。在這裡必須吐槽一下,網上大部分關於如何把kafka安裝到window上的文章,要麼不完整,要麼kafka用戶端代碼是錯誤的或者不是基於0.8版本的。但是必須提醒一下,這篇文章只是介紹了其中的一種安裝方法,可能不是最簡潔的。
1、軟體準備
kafka_2.9.1-0.8.2.1.tgz,需要到kafka官網下載。 sbt-0.13.9.msi,需要到sbt官網下載下載。 git bash 網上大把,我用的是Git-1.9.4-preview20140611.exe Eclipse Maven Java 7
2、kafka環境設定 2.1 切換到kafka目錄 先解壓kafka_2.9.1-0.8.2.1.tgz,例如解壓到D:\sam.lin\software\kafka\kafka_2.9.1-0.8.2.1。使用git bash,切換到kafka目錄下。例如:
kafka需要的一些組件可以使用sbt update命令得到,如下:
2.2 sbt update 敲打斷行符號鍵後,會出現一個錯誤資訊,"sbt: No such file or directory",這個時候你需要安裝sbt,下載網址我在上面的【軟體準備】中已經提供了。本文下載的是sbt-0.13.9.msi這個版本。
sbt update命令會開始下載組件,如果是第一次安裝,這個過程比較漫長,耐心等待吧。 當出現[success]字樣時,說明安裝組件成功了。 2.3 sbt package 如下圖:
當出現[success]字樣時,說明命令執行成功了。
2.4 sbt sbt-dependency 如下圖:
當出現[info] org.scala-sbt:sbt:0.13.8,說明命令執行成功了。 到此,kafka環境搭建完畢。接下來需要搭建zookeeper.
3、zookeeper環境設定 注意,安裝zookeeper的時候,在window上使用shell來安裝是沒法成功的,至少我嘗試很多次都沒成功。其實kafka已經為我們提供了在window安裝zookeeper的bat指令碼了,這些指令碼存放在kafka安裝目錄中的bin/window上,如下:
注意在這裡不能使用git bash了,原因是git bash執行bat檔案時,會報語法錯誤。我們換成window的cmd命令列。
3.1 修改zookeeper和kafka的設定檔 1) 修改config目錄下的server.properties檔案,修改 log.dirs=/D/sam.lin/software/kafka/kafka_2.9.1-0.8.2.1/kafka-logs
2) 修改config目錄下的log4j.properties檔案,修改 log4j.appender.kafkaAppender.File=/D/sam.lin/software/kafka/kafka_2.9.1-0.8.2.1/logs/server.log
3) 修改config目錄下的zookeeper.properties檔案,加入或者修改 dataDir=/D/sam.lin/software/kafka/kafka_2.9.1-0.8.2.1/data/zookeeper
3.2 使用window cmd命令列介面,切換到/bin/window目錄 3.3 啟動zookeeper
命令:zookeeper-server-start.bat ../../config/zookeeper.properties
啟動成功後,不要關閉這個cmd命令列介面,因為關閉它,zookeeper進程就停了。
4、啟動kafka broker
命令:kafka-server-start.bat ../../config/server.properties
啟動成功後,不要關閉這個cmd命令列介面,因為關閉它,kafka進程就停了。
5、建立topic
命令:kafka-run-class.bat kafka.admin.TopicCommand --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic hellotest
這個命令建立了名字為"hellotest"的topic。
6、發送訊息 命令:kafka-console-producer.bat --broker-list localhost:9092 --topic hellotest
注意這裡的hellotest就是剛才建立的topic。這個命令執行後,可以直接在命令列後面輸入你要發的訊息。
後面的samsamsam就我輸入的,往hellotest這個topic發送的訊息。下面我們建立消費者來消費訊息。
7、接收訊息
命令:kafka-console-consumer.bat --zookeeper localhost:2181 --topic hellotest --from-beginning
同樣的,必須指定到hellotest這個topic消費訊息,我在生產者的cmd命令列介面中輸入samsamsam後,訊息者cmd命令列介面立刻接收到了。
8、使用kafka提供的java 用戶端來發送和接收訊息 上面提到可以使用kafka內建的命令列工具來發送訊息和接收訊息,下面介紹一下使用java發送和接收訊息。
8.1 寄件者
package com.kafka;import java.util.Properties;import kafka.javaapi.producer.Producer;import kafka.producer.KeyedMessage;import kafka.producer.ProducerConfig;public class Sender { public static void main(String[] args) { Properties prop = new Properties(); prop.put("metadata.broker.list", "127.0.0.1:9092"); prop.put("serializer.class", "kafka.serializer.StringEncoder"); ProducerConfig producerConfig = new ProducerConfig(prop); Producer<String, String> producer = new<String, String> Producer(producerConfig); String topic = "hellotest"; KeyedMessage<String, String> message = new<String, String> KeyedMessage(topic, "Sam Hello Test message2"); producer.send(message); producer.close(); }}
8.2 消費者
代碼一
package com.kafka;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import kafka.consumer.ConsumerConfig;import kafka.consumer.KafkaStream;import kafka.javaapi.consumer.ConsumerConnector;public class ConsumerDemo { private final ConsumerConnector consumer; private final String topic; private ExecutorService executor; public ConsumerDemo(String zookeeper, String groupid, String aTopic) { consumer = kafka.consumer.Consumer.createJavaConsumerConnector(ConsumerProps(zookeeper, groupid)); this.topic = aTopic; } public void run(int threads) { Map<String, Integer> topicMap = new HashMap<String, Integer>(); topicMap.put(topic, new Integer(threads)); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicMap); List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic); executor = Executors.newFixedThreadPool(threads); int numThread = 0; for (final KafkaStream stream : streams) { executor.submit(new ConsumerDemoRun(stream, numThread)); numThread++; } } private static ConsumerConfig ConsumerProps(String zookeeper, String groupid) { Properties properties = new Properties(); // config properties file properties.put("zookeeper.connect", zookeeper); properties.put("group.id", groupid); properties.put("zookeeper.session.timeout.ms", "400"); properties.put("zookeeper.sync.time.ms", "200"); properties.put("auto.commit.interval.ms", "1000"); properties.put("auto.offset.reset", "smallest"); return new ConsumerConfig(properties); } public void shutdown() { if (consumer != null) consumer.shutdown(); if (executor != null) executor.shutdown(); try { if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) { System.out.println("Timed out waiting for consumer threads to shut down, exiting uncleanly"); } } catch (InterruptedException e) { System.out.println("Interrupted during shutdown, exiting uncleanly"); } } public static void main(String[] args) { String zookeeper = "localhost:2181"; String groupid = "group1"; String topic = "hellotest"; int threads = 1; ConsumerDemo test = new ConsumerDemo(zookeeper, groupid, topic); test.run(threads); try { Thread.sleep(10000); } catch (InterruptedException ie) { } test.shutdown(); }}
代碼二
package com.kafka;import kafka.consumer.ConsumerIterator;import kafka.consumer.KafkaStream;public class ConsumerDemoRun implements Runnable { private KafkaStream aStream; private int aThread; public ConsumerDemoRun(KafkaStream stream, int thread) { aStream = stream; // set stream from main read aThread = thread; // set thread from main read } public void run() { ConsumerIterator<byte[], byte[]> iterator = aStream.iterator(); // used to // check // throughout // the list // continiously while (iterator.hasNext()) { System.out.println("Thread " + aThread + ": " + new String(iterator.next().message())); } System.out.println("Shutting down Thread: " + aThread); }}
要運行上面的代碼,需要加入如下依賴
<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka_2.9.2</artifactId><version>0.8.2.1</version></dependency>
總結: 在window上安裝kafka還挺麻煩的,上面提供的安裝方式比較繁瑣,應該有更加簡單的方式。目前還沒找到。