kafka工具类

 生产者

public class TestProducer {

    public static void main(String[] args) {
        KafkaProducer producer = KafkaUtils.createProducer();
        for (int i = 0; i < 100; i++) {
            KafkaUtils.sendMsg(producer, "test_topic_test", "key", "value" + System.currentTimeMillis());
        }
        KafkaUtils.closeProducer(producer);
        System.out.println("结束");
    }
}

 消费者

public class TestConsumer {

    public static void main(String[] args) {
        KafkaConsumer consumer = KafkaUtils.createConsumer("test1123");
        while (true) {
            ConsumerRecords msg = KafkaUtils.getMsg(consumer, "test_topic_test");
            for (ConsumerRecord record : msg) {
                System.out.println(record.value());
            }
        }
    }

}

工具类 

 

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

@Slf4j
public class KafkaUtils {

    //kafka地址
    private static final String server_ip = "192.168.2.219:9092,192.168.2.220:9092,192.168.2.221:9092";


    /**
     * 私有静态方法,创建Kafka生产者
     *
     * @return KafkaProducer
     */
    public static KafkaProducer createProducer() {
        //生产者配置
        Properties properties = new Properties();
        properties.put("bootstrap.servers", server_ip);
        properties.put("acks", "all");
        //消息发送重试次数
        properties.put("retries", 0);
        properties.put("batch.size", 0);
        //提交延迟
        properties.put("linger.ms", 1);
        properties.put("buffer.memory", 33554432);
        //Kafka提供的序列化和反序列化类
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer producer = new KafkaProducer<>(properties);
        return producer;
    }

    /**
     * 私有静态方法,创建Kafka消费者
     *
     * @param groupId 分组名称
     * @return KafkaComsumer
     */
    public static KafkaConsumer createConsumer(String groupId) {
        //消费者配置
        Properties properties = new Properties();
        properties.put("bootstrap.servers", server_ip);
        //分组名称
        properties.put("group.id", groupId);
        //每次拉取一百条,一条条消费,当然是具体业务状况设置
        properties.put("max.poll.records", 100);
        properties.put("enable.auto.commit", false);
        properties.put("auto.offset.reset", "earliest");
        //Kafka提供的序列化和反序列化类
        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer consumer = new KafkaConsumer<>(properties);
        return consumer;
    }

    public static void sendMsg(KafkaProducer producer, String topicName, String key, String value) {
        producer.send(new ProducerRecord<>(topicName, key, value));
    }

    public static void closeProducer(KafkaProducer producer) {
        producer.close();
    }

    public static ConsumerRecords getMsg(KafkaConsumer consumer, String topicName) {
        consumer.subscribe(Arrays.asList(topicName));
        return consumer.poll(Duration.ofMillis(10000));
    }

    public static void closeConsumer(KafkaConsumer consumer) {
        consumer.close();
    }

}

你可能感兴趣的:(kafka,java,大数据)