Kafka Java 牛刀小试
来源:互联网 发布:java yield join 编辑:程序博客网 时间:2024/06/09 23:03
1、producer --生产者
package com.nanine.kafka;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author WangZhanpeng
*/
public class KafkaProducerDemo {
Logger logger = LoggerFactory.getLogger(KafkaProducerDemo.class);
public static void main(String[] args) throws InterruptedException {
Properties props = new Properties();
//A list of host/port pairs to use for establishing the initial connection to the Kafka cluster
props.put("bootstrap.servers", "localhost:9092");
//This mean leader will wait for the full set of in-sync replicas to acknowledge the record;
props.put("acks", "all");
//Setting a value greater than zero will cause the client to resend any record whose send fails with a potentially transient error
props.put("retries", 0);
// This configuration controls the default batch size in bytes.
props.put("batch.size", 16384);
//The milliseconds to delay
props.put("linger.ms", 1);
//This setting should correspond roughly to the total memory the producer will use, but is not a hard
//bound since not all memory the producer uses is used for buffering
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<String, String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 100; i++){
producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), Integer.toString(i)));
Thread.sleep(1000);
}
producer.close();
}
}
2、消费者-consumer
package com.nanine.kafka;
import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author WangZhanpeng
*/
public class KafkaConsumerDemo {
Logger logger = LoggerFactory.getLogger(KafkaConsumerDemo.class);
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
//A unique string that identifies the consumer group this consumer belongs to.
props.put("group.id", "wangzhefeng");
//If true the consumer's offset will be periodically committed in the background.
props.put("enable.auto.commit", "true");
//The frequency in milliseconds that the consumer offsets are auto-committed to Kafka if enable.auto.commit is set to true.
props.put("auto.commit.interval.ms", "1000");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
Consumer<String,String> consumer = new KafkaConsumer<>(props);
//Pointed the topic (same to the topic of producer)
consumer.subscribe(Arrays.asList("my-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records)
System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
}
}
}
3、eclipse 导出KafkaProducerDemo .jar 、KafkaConsumerDemo.jar 指定main 函数以及依赖文件lib;
4、linux 启动生产者和消费者;
java -Djava.ext.dirs=./lib -jar KafkaProducerDemo .jar ;
java -Djava.ext.dirs=./lib -jar KafkaConsumerDemo.jar ;
阅读全文
0 0
- Kafka Java 牛刀小试
- 牛刀小试 - 详解Java多线程
- Java RMI 之牛刀小试
- 牛刀小试!
- 牛刀小试
- 牛刀小试
- 牛刀小试
- 牛刀小试
- 牛刀小试 JAVA内置 观察者模式
- 牛刀小试 - Java泛型程序设计
- JAVA WEB自动化部署牛刀小试
- java初学者面试题牛刀小试
- mapxtreme java manger web 应用程序牛刀小试
- java应用服务器resin之牛刀小试
- JAVA GUI牛刀小试——记事本
- Velocity和Freemarker模板牛刀小试(java)
- 牛刀小试 - 趣谈Java中的异常处理
- 牛刀小试 - 浅析Java集合框架的使用
- 小白的Python之路——初识Python
- MySQL多表查询
- Mybatis单个参数的if判断(针对异常:There is no getter for property..)
- 马里奥吃金币消失
- Unity Profiler 性能分析
- Kafka Java 牛刀小试
- C语言实现直方图统计和直方图均衡化
- android studio 3.0 升级问题,遇到的坑;
- 文章标题
- 2018,中国区块链将引爆硅谷
- Maven项目,无法显示src/main/java资源文件夹
- android MediaPlayer简单使用
- springboot配置kafka与原生kafka配置
- plsql中文乱码,显示问号