KafkaProducerDemo
package com.pcy.kafkastream;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
import java.util.Random;
public class KafkaProducerDemo {
private static final String topic = "kafkaTopic";
public static void main(String[] args) throws Exception {
Properties p = new Properties();
p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.112.1:9092");
p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
p.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(p);
try {
while (true) {
String msg = "Hello Kafka," + new Random().nextInt(100);
ProducerRecord<String, String> record = new ProducerRecord<>(topic, msg);
kafkaProducer.send(record);
System.out.println("消息发送成功:" + msg);
Thread.sleep(1000);
}
} finally {
kafkaProducer.close();
}
}
}
KafkaConsumerDemo
package com.pcy.kafkastream;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import java.util.Collections;
import java.util.Properties;
public class KafkaConsumerDemo {
private static final String topic = "kafkaTopic";
public static void main(String[] args) {
Properties p = new Properties();
p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.112.1:9092");
p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
p.put(ConsumerConfig.GROUP_ID_CONFIG, topic);
KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(p);
kafkaConsumer.subscribe(Collections.singletonList(topic));
while (true) {
ConsumerRecords<String, String> records = kafkaConsumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
System.out.printf("topic:%s,offset:%d,消息:%s%n", record.topic(), record.offset(), record.value());
}
}
}
}
结果预览
消息发送成功:Hello Kafka,44
消息发送成功:Hello Kafka,8
消息发送成功:Hello Kafka,34
消息发送成功:Hello Kafka,57
消息发送成功:Hello Kafka,19
消息发送成功:Hello Kafka,20
消息发送成功:Hello Kafka,35
消息发送成功:Hello Kafka,36
消息发送成功:Hello Kafka,90
消息发送成功:Hello Kafka,71
topic:kafkaTopic,offset:1034,消息:Hello Kafka,44
topic:kafkaTopic,offset:1035,消息:Hello Kafka,8
topic:kafkaTopic,offset:1036,消息:Hello Kafka,34
topic:kafkaTopic,offset:1037,消息:Hello Kafka,57
topic:kafkaTopic,offset:1038,消息:Hello Kafka,19
topic:kafkaTopic,offset:1039,消息:Hello Kafka,20
topic:kafkaTopic,offset:1040,消息:Hello Kafka,35
topic:kafkaTopic,offset:1041,消息:Hello Kafka,36
topic:kafkaTopic,offset:1042,消息:Hello Kafka,90
topic:kafkaTopic,offset:1043,消息:Hello Kafka,71