摘要:準備環境系統下載并解壓啟動啟動啟動創建我修改了默認的端口查看創建的查看的詳細信息測試運行并在控制臺中輸一些消息測試注意需要開兩個終端搭建開發環境生產者
準備環境
系統
CentOS release 6.6 (Final)
下載并解壓
wget http://mirror.bit.edu.cn/apache/kafka/0.8.0/kafka_2.10-0.8.0.tgz tar -zxvf kafka_2.10-0.8.0.tgz`啟動
啟動Zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties &
啟動kafka
bin/kafka-server-start.sh config/server.properties &
創建topic
我修改了默認的端口
bin/kafka-topics.sh --create --zookeeper localhost:2182 --replication-factor 1 --partitions 1 --topic testweixuan &
查看創建的topic
bin/kafka-topics.sh --list --zookeeper localhost:2182
查看topic的詳細信息
bin/kafka-topics.sh --describe --zookeeper localhost:2182
測試producer
運行producer并在控制臺中輸一些消息:
bin/kafka-console-producer.sh --broker-list 192.168.1.116:2182:9092 --topic testweixuan
測試consumer
bin/kafka-console-consumer.sh --zookeeper 192.168.1.116:2182 --topic testweixuan --from-beginning
注意:需要開兩個終端
搭建開發環境 生產者代碼package com.fengtang; import java.util.Properties; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; /** * Create by fengtang * 2015/10/8 0008 * KafkaDemo_01 */ public class KafkaProducer { private final Producer消費者代碼producer; public final static String TOPIC = "TEST-TOPIC"; private KafkaProducer() { Properties props = new Properties(); /** * 此處配置的是kafka的端口 */ props.put("metadata.broker.list", "192.168.1.116:9092"); /** * 配置value的序列化類 */ props.put("serializer.class", "kafka.serializer.StringEncoder"); /** * 配置key的序列化類 */ props.put("key.serializer.class", "kafka.serializer.StringEncoder"); props.put("request.required.acks", "-1"); producer = new Producer<>(new ProducerConfig(props)); } void produce() { int messageNo = 1000; final int COUNT = 10000; while (messageNo < COUNT) { String key = String.valueOf(messageNo); String data = "hello kafka message " + key; producer.send(new KeyedMessage<>(TOPIC, key, data)); System.out.println(data); messageNo++; } } public static void main(String[] args) { new KafkaProducer().produce(); } }
package com.fengtang; import kafka.consumer.ConsumerConfig; import kafka.consumer.ConsumerIterator; import kafka.consumer.KafkaStream; import kafka.javaapi.consumer.ConsumerConnector; import kafka.serializer.StringDecoder; import kafka.utils.VerifiableProperties; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; /** * Create by fengtang * 2015/10/8 0008 * KafkaDemo_01 */ public class KafkaConsumer { private final ConsumerConnector consumer; private KafkaConsumer() { Properties props = new Properties(); /** * zookeeper 配置 */ props.put("zookeeper.connect", "192.168.1.116:2182"); /** * group 代表一個消費組 */ props.put("group.id", "jd-group"); /** * zk連接超時 */ props.put("zookeeper.session.timeout.ms", "400000"); props.put("zookeeper.sync.time.ms", "200"); props.put("auto.commit.interval.ms", "1000"); props.put("auto.offset.reset", "smallest"); /** * 序列化類 */ props.put("serializer.class", "kafka.serializer.StringEncoder"); ConsumerConfig config = new ConsumerConfig(props); consumer = kafka.consumer.Consumer.createJavaConsumerConnector(config); } void consume() { MaptopicCountMap = new HashMap (); topicCountMap.put(KafkaProducer.TOPIC, new Integer(1)); StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties()); StringDecoder valueDecoder = new StringDecoder(new VerifiableProperties()); Map >> consumerMap = consumer.createMessageStreams(topicCountMap, keyDecoder, valueDecoder); KafkaStream stream = consumerMap.get(KafkaProducer.TOPIC).get(0); ConsumerIterator it = stream.iterator(); while (it.hasNext()) System.out.println(it.next().message()); } public static void main(String[] args) { new KafkaConsumer().consume(); } }
pom.xml
解決報錯4.0.0 com.fengtang kafkademo 1.0-SNAPSHOT jar kafkademo http://maven.apache.org UTF-8 org.apache.kafka kafka_2.10 0.8.0 log4j log4j 1.2.15 jmxtools com.sun.jdmk jmxri com.sun.jmx jms javax.jms javax.mail
kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
需要改動config文件夾下的server.properties中的以下兩個屬性
zookeeper.connect=localhost:2181改成zookeeper.connect=192.168.1.116 (自己的服務器IP地址):2181
以及默認注釋掉的 #host.name=localhost 改成 host.name=192.168.1.116 (自己的服務器IP地址)
成功截圖文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/64659.html
摘要:項目地址前言大數據技術棧思維導圖大數據常用軟件安裝指南一分布式文件存儲系統分布式計算框架集群資源管理器單機偽集群環境搭建集群環境搭建常用命令的使用基于搭建高可用集群二簡介及核心概念環境下的安裝部署和命令行的基本使用常用操作分區表和分桶表視圖 項目GitHub地址:https://github.com/heibaiying... 前 言 大數據技術棧思維導圖 大數據常用軟件安裝指...
閱讀 4021·2021-11-22 13:53
閱讀 3617·2021-11-19 11:29
閱讀 1265·2021-09-08 09:35
閱讀 3162·2020-12-03 17:26
閱讀 514·2019-08-29 16:06
閱讀 2106·2019-08-26 13:50
閱讀 1180·2019-08-23 18:32
閱讀 2153·2019-08-23 18:12