我的kafka JAVA调试代码

发布于:2021-11-27 12:06:20

仅供自己参考,别人可能看不懂。


kafka 是很好的供所有分析库从生产库多次提取数据的中转库,特别是kafka 0.9后出现的kafka connect,个人认为能作为实时的ETL工具。


另外,kafka和storm都是流,但kafka不处理数据,storm可在kafka的基础上处理数据。storm在原理上和hadoop的mapreduce差不多,都有map reduce的过程,只是hadoop处理完一次MR后,就会结束,但storm不会结束,除非手动kill。这篇介绍storm的文章不错:?http://os.51cto.com/art/201308/408739.htm


个人认为,对于每次都是处理结构化数据的工作,可以不用storm。


下面是kafka的java调试程序,含json处理。



/**
* Created by hadoop on 16-6-21.
*/
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.databind.ObjectMapper;
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;
public class ceshi {

private final ConsumerConnector consumer;

private ceshi() {
Properties props = new Properties();
//zookeeper 配置
props.put("zookeeper.connect", "192.168.3.31:2181");

//group 代表一个消费组
props.put("group.id", "jd-group");

//zk连接超时
props.put("zookeeper.session.timeout.ms", "4000");
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("test1", 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("test1").get(0);
ConsumerIterator iterator = stream.iterator();
Map> maps;

while (iterator.hasNext()) {
//System.out.println(iterator.next().message());
try {
maps=new ObjectMapper().readValue(iterator.next().message(), Map.class);
System.out.println( (Object)(maps.get("xm")) );
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("请输入一个正整");
new ceshi().consume();
}

}





相关推荐

最新更新

猜你喜欢