启动:
./kafka-server-start.sh ../config/server.properties
创建topic
./kafka-topics.sh –create –zookeeper localhost:2182 –replication-factor 1 –partitions 1 –topic mytopic
查看topic
./kafka-topics.sh –list –zookeeper localhost:2182
删除topic
kafka-topics.sh –delete –zookeeper localhost:2182 –topic mytopic
发送消息:
./kafka-console-producer.sh –broker-list localhost:9092 –topic mytopic
接收消息:
./kafka-console-consumer.sh –zookeeper localhost:2182 –topic mytopic –from-beginning
生产者Demo
//可同时建立多个组进行数据消费,不同组使用不同数据即可。保证集群性能。
//设置方法: props.put("group.id", "jd-group2");
// props.put("group.id", "jd-group3");
package com.demo.ec2.test.kafka;
import java.util.Properties;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
public class KafkaProducer
{
private final Producer<String, String> producer;
public final static String TOPIC = "CUSTOMERACTIVITY";
private KafkaProducer(){
Properties props = new Properties();
props.put("metadata.broker.list", "1.1.238.82:9092");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("key.serializer.class", "kafka.serializer.StringEncoder");
props.put("request.required.acks","-1");
producer = new Producer<String, String>(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<String, String>(TOPIC, key ,data));
messageNo ++;
}
}
public static void main( String[] args )
{
new KafkaProducer().produce();
}
}
消费者Demo
package com.demo.ec2.test.kafka;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
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 KafkaConsumer {
private final ConsumerConnector consumer;
private KafkaConsumer() {
Properties props = new Properties();
props.put("zookeeper.connect", "1.1.238.82:2181");
props.put("group.id", "jd-group");
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<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(KafkaProducer.TOPIC, new Integer(1));
StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties());
StringDecoder valueDecoder = new StringDecoder(new VerifiableProperties());
Map<String, List<KafkaStream<String, String>>> consumerMap =
consumer.createMessageStreams(topicCountMap,keyDecoder,valueDecoder);
KafkaStream<String, String> stream = consumerMap.get(KafkaProducer.TOPIC).get(0);
ConsumerIterator<String, String> it = stream.iterator();
while (it.hasNext())
System.out.println(it.next().message());
}
public static void main(String[] args) {
new KafkaConsumer().consume();
}
}