博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kafka] 005_kafka_Java_API
阅读量:6425 次
发布时间:2019-06-23

本文共 4254 字,大约阅读时间需要 14 分钟。

 

1、生产者Producer

1)添加依赖

org.apache.kafka
kafka-clients
0.10.0.0

2)简单推送代码

文档参考:http://kafka.apache.org/0100/javadoc/index.html?org/apache/kafka/clients/producer/KafkaProducer.html

同步推送:

Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("acks", "all"); props.put("retries", 0); props.put("batch.size", 16384); props.put("linger.ms", 1); props.put("buffer.memory", 33554432); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer
producer = new KafkaProducer<>(props); for(int i = 0; i < 100; i++) producer.send(new ProducerRecord
("my-topic", Integer.toString(i), Integer.toString(i))); producer.close();

 

异步推送:

public Future
send(ProducerRecord
record,Callback callback)

 

对比:

If you want to simulate a simple blocking call you can call the get() method immediately: byte[] key = "key".getBytes(); byte[] value = "value".getBytes(); ProducerRecord
record = new ProducerRecord
("topic1", key, value) producer.send(record).get(); Fully non-blocking usage can make use of the Callback parameter to provide a callback that will be invoked when the request is complete. ProducerRecord
record = new ProducerRecord
("topic1", key, value); producer.send(myRecord, new Callback() { public void onCompletion(RecordMetadata metadata, Exception e) { if(e != null) e.printStackTrace(); System.out.println("The offset of the record we just sent is: " + metadata.offset()); } });

 

2、消费者Consumer

1)添加依赖

org.apache.kafka
kafka-clients
0.10.0.0

2)简单拉取代码

更多请查看:http://kafka.apache.org/0100/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html

   Properties props = new Properties();     props.put("bootstrap.servers", "localhost:9092");     props.put("group.id", "test");     props.put("enable.auto.commit", "true");     props.put("auto.commit.interval.ms", "1000");     props.put("session.timeout.ms", "30000");     props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");     props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");      KafkaConsumer
consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList("foo", "bar")); while (true) { ConsumerRecords
records = consumer.poll(100); for (ConsumerRecord
record : records) System.out.printf("offset = %d, key = %s, value = %s",           record.offset(), record.key(), record.value()); }

 

附上以验证通过的producer推送代码:

public RecordMetadata sendSyncKafkaRequest(String topic, KeyModel keyModel, Object message) {        logger.info("=== 推送服务开始:sendSyncKafkaRequest start ===");        logger.info("=== topic: " + topic + "===");        logger.info("=== keyModel: " + JSON.toJSONString(keyModel) + "===");        logger.info("=== message: " + JSON.toJSONString(message) + "===");        Properties props = kafkaProducerProperties.getProperties();        KafkaProducer
producer = null; RecordMetadata recordMetadata = null; try { producer = new KafkaProducer
(props); recordMetadata = producer.send(new ProducerRecord
(topic, keyModel, message)).get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } if (recordMetadata != null) { logger.info("===kafka请求推送成功!Topic:" + recordMetadata.topic() + ";分区:" + recordMetadata.partition() + "==="); } else { logger.info("=== recordMetadata为 null!本次kafka 写入请求没有完成!==="); } return recordMetadata; }

 

转载地址:http://pnyga.baihongyu.com/

你可能感兴趣的文章
Codeforces Round #369 (Div. 2) A. Bus to Udayland 水题
查看>>
adb上使用cp/mv命令的替代方法(failed on '***' - Cross-device link解决方法)
查看>>
C++标准库简介、与STL的关系。
查看>>
Spring Boot 3 Hibernate
查看>>
查询EBS请求日志的位置和名称
查看>>
大型机、小型机、x86服务器的区别
查看>>
J2EE十三个规范小结
查看>>
算法(第四版)C#题解——2.1
查看>>
网关支付、银联代扣通道、快捷支付、银行卡支付分别是怎么样进行支付的?...
查看>>
大数据开发实战:Stream SQL实时开发一
查看>>
C++返回引用的函数例程
查看>>
dll 问题 (转)
查看>>
REST API用得也痛苦
查看>>
test for windows live writer plugins
查看>>
Tiny210 U-BOOT(二)----配置时钟频率基本原理
查看>>
代理模式
查看>>
javaweb学习总结(二十四)——jsp传统标签开发
查看>>
让script的type属性等于text/html
查看>>
linux 文件系统sysvinit 流程分析
查看>>
体素科技:2018年,算法驱动下的医学影像分析进展
查看>>