国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

搭建 kafka 運行環境

J4ck_Chan / 473人閱讀

摘要:準備環境系統下載并解壓啟動啟動啟動創建我修改了默認的端口查看創建的查看的詳細信息測試運行并在控制臺中輸一些消息測試注意需要開兩個終端搭建開發環境生產者

準備環境

系統

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() {
        Map topicCountMap = 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
                
                
                    mail
                    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開源項目)

    摘要:項目地址前言大數據技術棧思維導圖大數據常用軟件安裝指南一分布式文件存儲系統分布式計算框架集群資源管理器單機偽集群環境搭建集群環境搭建常用命令的使用基于搭建高可用集群二簡介及核心概念環境下的安裝部署和命令行的基本使用常用操作分區表和分桶表視圖 項目GitHub地址:https://github.com/heibaiying... 前 言 大數據技術棧思維導圖 大數據常用軟件安裝指...

    guyan0319 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<