005_kafka_Java_API
1、生产者Producer
1)添加依赖
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.0.0</version>
</dependency>
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", );
props.put("batch.size", );
props.put("linger.ms", );
props.put("buffer.memory", );
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> producer = new KafkaProducer<>(props); for(int i = ; i < ; i++)
producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), Integer.toString(i))); producer.close();
异步推送:
public Future<RecordMetadata> send(ProducerRecord<K,V> 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<byte[],byte[]> record = new ProducerRecord<byte[],byte[]>("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<byte[],byte[]> record = new ProducerRecord<byte[],byte[]>("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)添加依赖
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.0.0</version>
</dependency>
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", "");
props.put("session.timeout.ms", "");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("foo", "bar"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll();
for (ConsumerRecord<String, String> 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<KeyModel, Object> producer = null;
RecordMetadata recordMetadata = null;
try {
producer = new KafkaProducer<KeyModel, Object>(props);
recordMetadata = producer.send(new ProducerRecord<KeyModel, Object>(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;
}
005_kafka_Java_API的更多相关文章
- [kafka] 005_kafka_Java_API
1.生产者Producer 1)添加依赖 <dependency> <groupId>org.apache.kafka</groupId> <artifact ...
随机推荐
- js 函数总结
函数的基本语法如下所示: function functionName(arg0, arg1,...,argN) { statements } 函数如果有返回值则return 后的语句将不会被执行,返回 ...
- 编写高质量ios-之一 OC 语言的起源
要点 Objective-c为C语言添加了面向对象的特性,是其超级.Objective-c使用动态绑定的消息结构,也就是说,在运行时才会检查对象类型.接受一条消息之后,究竟应执行何种代码,由运行期环境 ...
- hibernateTools插件安装
先去官方网站工具页面 http://hibernate.org/tools/ 点击Download 然后出现 你可以选择在线安装 在eclipse里面.help->install->add ...
- C# ASP.NET 读取EXCEL 单元格 读取 空值 不显示
跟大家分享一下,[摘自]:http://blog.csdn.net/li185416672/article/details/8213729 读取excel时,某些单元格为空值 原来如此: 当我们用ol ...
- PowerDesigner设计Name和Comment 替换
这两天在用powerdesigner设计数据库.一直以为name就是注释名字来着.后来生成sql语句 怎么就没有注释信息那. 后来看了半天才知道自己范2了. 通过各种信息查找.大多都是改databas ...
- 用kryonet时kryo报buffer underflow错误
原因是客户端和服务器端的kryo必须register同样的class类,某一端多register一个class类导致的
- fnciton
-----oracle将字段字符分隔作为临时表 select column_value as site_id from table (select fn_split_clob(dashboard_pr ...
- 2016年12月28日 星期三 --出埃及记 Exodus 21:23
2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...
- ma60笔记
60日均线是股价的生命线,跌破生命线就有一定的下跌空间.大 盘指数也有这种规律,当指数跌破60日平均线后,意味着大盘有一定的下跌空间.大盘指数是由所有个股价格加权统计后产生的,而个股价格却有很大的差异 ...
- 基于springMVC+angular+bootstrap+mysql的简易购物网站搭建
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=10&cad=rja& ...