生产者
package com.hehe.kafka;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.KafkaProducer;
import java.util.Properties;
public class Kafkaproduce {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("bootstrap.servers","master:9092");
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer<String,String>(properties);
for(int i=0;i<10;i++){
ProducerRecord<String, String> record = new ProducerRecord<String, String>("test_topic3", String.valueOf(i));
producer.send(record);
i++;
}
producer.close();
}
}
打开消费者端查看
消费者
package com.hehe.kafka;
import java.util.*;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class kafkaconsume {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("bootstrap.servers","192.168.154.123:9092");
properties.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
properties.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
properties.setProperty("group.id","aa");
properties.put("auto.offset.reset","earliest");
properties.put("zookeeper.session.timeout.ms","10000");
properties.put("zookeeper.sync.time.ms","200");
properties.put("auto.commit.interval.ms","1000");
KafkaConsumer<String, String> consume = new KafkaConsumer<String, String>(properties);
consume.subscribe(Collections.singletonList("test_topic3"));
try {
while (true) {
ConsumerRecords<String, String> consumerRecords = consume.poll(1000);
for (ConsumerRecord<String, String> record : consumerRecords) {
String key = record.key();
String value = record.value();
long offset = record.offset();
int partition = record.partition();
String topic = record.topic();
System.out.println(key + " " + value + " " + offset + " " + partition + " " + topic);
}
}
}finally {
consume.close();
}
}
}