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 ;
原创粉丝点击