kafka地址: http://kafka.apache.org/
在pom.xml文件中添加下面的依赖
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.11.0.2</version>
</dependency>
下面看看 生产者:
package kafkaAndJava;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class ProducerTest {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("bootstrap.servers", "hadoop002:9092");
properties.put("acks", "all");
properties.put("retries", 0);
properties.put("batch.size", 16384);
properties.put("linger.ms", 1);
properties.put("buffer.memory", 33554432);
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = null;
try {
producer = new KafkaProducer<String, String>(properties);
for (int i = 0; i < 100; i++) {
String msg = "This is Message " + i;
producer.send(new ProducerRecord<String, String>("xuhaitao", msg));
System.out.println("Sent:" + msg);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
producer.close();
}
}
}
下面看看 消费者代码:
package kafkaAndJava;
import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class kafkaConsumerTest {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("bootstrap.servers", "hadoop002:9092"); // 指向kafka集群的IP地址
properties.put("group.id", " group-1"); // Consumer分组ID
properties.put("enable.auto.commit", "true");
properties.put("auto.commit.interval.ms", "1000"); /* 自动确认offset的时间间隔 */
properties.put("auto.offset.reset", "earliest");
properties.put("session.timeout.ms", "30000");
properties.put("max.poll.records", "100");// max.poll.records条数据需要在在session.timeout.ms这个时间内处理完
properties.put("fetch.min.bytes", "1");//server发送到消费端的最小数据,若是不满足这个数值则会等待直到满足指定大小。默认为1表示立即接收。
properties.put("fetch.wait.max.ms", "1000");
properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // 反序列化
properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // 反序列化
KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties);
kafkaConsumer.subscribe(Arrays.asList("xuhaitao")); // 设置消费的主题
while (true) {
ConsumerRecords<String, String> records = kafkaConsumer.poll(100); // 调用poll方法来轮循Kafka集群的消息,其中参数100是超时时间
for (ConsumerRecord<String, String> record : records) {
System.out.printf("offsetConsumer = %d, value = %s", record.offset(), record.value());
System.out.println();
}
}
}
}
FR:徐海涛(hunk Xu)
QQ技术交流群:386476712