packagecom.storm.storm_my.kafka;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.Properties;importkafka.consumer.Consumer;importkafka.consumer.ConsumerConfig;importkafka.consumer.ConsumerIterator;importkafka.consumer.KafkaStream;importkafka.javaapi.consumer.ConsumerConnector;importcom.storm.storm_my.kafka.KafkaConfigApiConstant.KafkaPropertiesValues;importcom.storm.storm_my.kafka.KafkaConfigApiConstant.kafkaPropertiesKeys;/***
*@authorPeng.Li
**/
public class KafkaConsumer implementsRunnable {privateConsumerConnector consumer;privateString topic;/***
*@paramtopic*/
publicKafkaConsumer(String topic) {
consumer= Consumer.createJavaConsumerConnector(this.newConsumerConfig());this.topic =topic;
}/***
*@return配置*/
publicConsumerConfig newConsumerConfig() {
Properties props= newProperties();
props.put(kafkaPropertiesKeys.ZK_CONNECT, KafkaPropertiesValues.ZK_CONNECT);
props.put(kafkaPropertiesKeys.ZK_CONNECTIONTIMEOUT_MS, String.valueOf(KafkaPropertiesValues.ZK_CONNECTIONTIMEOUT_MS));
props.put(kafkaPropertiesKeys.ZK_SESSION_TIMEOUT_MS, String.valueOf(KafkaPropertiesValues.ZK_SESSION_TIMEOUT_MS));
props.put(kafkaPropertiesKeys.ZK_SYNC_TIME_MS, String.valueOf(KafkaPropertiesValues.ZK_SYNC_TIME_MS));
props.put(kafkaPropertiesKeys.AUTO_COMMIT_ENABLE, String.valueOf(KafkaPropertiesValues.AUTO_COMMIT_ENABLE));
props.put(kafkaPropertiesKeys.TOPIC, KafkaPropertiesValues.TOPIC);
props.put(kafkaPropertiesKeys.GROUP_ID, KafkaPropertiesValues.GROUP_ID);
props.put(kafkaPropertiesKeys.FETCH_MESSAGE_MAX_BYTES, String.valueOf(KafkaPropertiesValues.FETCH_MESSAGE_MAX_BYTES));
props.put(kafkaPropertiesKeys.QUEUED_MAX_MESSAGE_CHUNKS, String.valueOf(KafkaPropertiesValues.QUEUED_MAX_MESSAGE_CHUNKS));
props.put(kafkaPropertiesKeys.FETCH_SIZE, String.valueOf(KafkaPropertiesValues.FETCH_SIZE));return newConsumerConfig(props);
}
@Overridepublic voidrun() {
Map topicMap = new HashMap();
topicMap.put(topic,new Integer(1));
Map>> topicOutPutStreamMap =consumer.createMessageStreams(topicMap);
KafkaStream topicStream = topicOutPutStreamMap.get(topic).get(0);
ConsumerIterator steamIt =topicStream.iterator();while(steamIt.hasNext()) {try{
System.out.println("Recieve -> [" + new String(steamIt.next().message(), "UTF-8") + "]");try{
Thread.sleep(1000 * 3);
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}