Kafka消息处理集群测试用例

xiaoxiao2021-02-28  14

package cn.hadoop.hdfs.conf; /**  * @Date Apr 28, 2015  *  * @Author huxin  *  * @Note Set param path  */ public class ConfigureAPI {     public interface KafkaProperties {         public final static String ZK = "10.211.55.15:2181,10.211.55.17:2181,10.211.55.18:2181";         public final static String GROUP_ID = "test_group1";         public final static String TOPIC = "test2";         public final static String BROKER_LIST = "10.211.55.15:9092,10.211.55.17:9092,10.211.55.18:9092";         public final static int BUFFER_SIZE = 64 * 1024;         public final static int TIMEOUT = 20000;         public final static int INTERVAL = 10000;     } } package cn.hadoop.hdfs.kafka; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import cn.hadoop.hdfs.conf.ConfigureAPI.KafkaProperties; import kafka.consumer.Consumer; import kafka.consumer.ConsumerConfig; import kafka.consumer.ConsumerIterator; import kafka.consumer.KafkaStream; import kafka.javaapi.consumer.ConsumerConnector; /**  * @Date May 22, 2015  *  * @Author dengjie  *  * @Note Kafka Consumer  */ public class JConsumer extends Thread {     private ConsumerConnector consumer;     private String topic;     private final int SLEEP = 1000 * 3;     public JConsumer(String topic) {         consumer = Consumer.createJavaConsumerConnector(this.consumerConfig());         this.topic = topic;     }     private ConsumerConfig consumerConfig() {         Properties props = new Properties();         props.put("zookeeper.connect", KafkaProperties.ZK);         props.put("group.id", KafkaProperties.GROUP_ID);         props.put("zookeeper.session.timeout.ms", "40000");         props.put("zookeeper.sync.time.ms", "200");         props.put("auto.commit.interval.ms", "1000");         return new ConsumerConfig(props);     }     @Override     public void run() {         Map<String, Integer> topicCountMap = new HashMap<String, Integer>();         topicCountMap.put(topic, new Integer(1));         Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);         KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0);         ConsumerIterator<byte[], byte[]> it = stream.iterator();         while (it.hasNext()) {             System.out.println("Receive->[" + new String(it.next().message()) + "]");             try {                 sleep(SLEEP);             } catch (Exception ex) {                 ex.printStackTrace();             }         }     } } package cn.hadoop.hdfs.kafka; import java.util.Properties; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; /**  * @Date May 22, 2015  *  * @Author dengjie  *  * @Note Kafka JProducer  */ public class JProducer extends Thread {     private Producer<Integer, String> producer;     private String topic;     private Properties props = new Properties();     private final int SLEEP = 1000 * 3;     public JProducer(String topic) {         props.put("serializer.class", "kafka.serializer.StringEncoder");         props.put("metadata.broker.list", "10.211.55.18:9092");         producer = new Producer<Integer, String>(new ProducerConfig(props));         this.topic = topic;     }     @Override     public void run() {         int offsetNo = 1;         while (true) {             String msg = new String("Message_" + offsetNo);             System.out.println("Send->[" + msg + "]");             producer.send(new KeyedMessage<Integer, String>(topic, msg));             offsetNo++;             try {                 sleep(SLEEP);             } catch (Exception ex) {                 ex.printStackTrace();             }         }     } } package cn.hadoop.hdfs.kafka.client; import cn.hadoop.hdfs.conf.ConfigureAPI.KafkaProperties; import cn.hadoop.hdfs.kafka.JConsumer; import cn.hadoop.hdfs.kafka.JProducer; /**  * @Date May 22, 2015  *  * @Author dengjie  *  * @Note To run Kafka Code  */ public class KafkaClient {     public static void main(String[] args) {         JProducer pro = new JProducer(KafkaProperties.TOPIC);         pro.start();         JConsumer con = new JConsumer(KafkaProperties.TOPIC);         con.start();     } }
转载请注明原文地址: https://www.6miu.com/read-2250278.html

最新回复(0)