package com.lya.kafka;
/**
* 配置项
* @author liuyazhuang
*
*/
public class Config {
/**
* 话题
*/
public static final String TOPIC = "wordcount";
/**
* 线程数
*/
public static final Integer THREADS = 1;
}
package com.lya.kafka;
import java.util.Properties;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
/**
* 生产者实例
* @author liuyazhuang
*
*/
public class ProducerDemo {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("zk.connect", "192.168.209.121:2181");
props.put("metadata.broker.list","192.168.209.121:9092");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("zk.connectiontimeout.ms", "15000");
ProducerConfig config = new ProducerConfig(props);
Producer<String, String> producer = new Producer<String, String>(config);
// 发送业务消息
// 读取文件 读取内存数据库 读socket端口
for (int i = 1; i <= 100; i++) {
Thread.sleep(500);
producer.send(new KeyedMessage<String, String>(Config.TOPIC,
"this number ===>>> " + i));
}
}
}
package com.lya.kafka;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.message.MessageAndMetadata;
/**
* 消费者实例
* @author liuyazhuang
*
*/
public class ConsumerDemo {
public static void main(String[] args) {
Properties props = new Properties();
props.put("zookeeper.connect", "192.168.209.121:2181");
props.put("group.id", "1111");
props.put("auto.offset.reset", "smallest");
props.put("zk.connectiontimeout.ms", "15000");
ConsumerConfig config = new ConsumerConfig(props);
ConsumerConnector consumer =Consumer.createJavaConsumerConnector(config);
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(Config.TOPIC, Config.THREADS);
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(Config.TOPIC);
for(final KafkaStream<byte[], byte[]> kafkaStream : streams){
new Thread(new Runnable() {
@Override
public void run() {
for(MessageAndMetadata<byte[], byte[]> mm : kafkaStream){
String msg = new String(mm.message());
System.out.println(msg);
}
}
}).start();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有