public class KafkaProducerDemo {
private final Producer<String,String> kafkaProducer;
public final static String TOPIC="JAVA_TOPIC";
private KafkaProducerDemo(){
kafkaProducer=createKafkaProducer() ;
}
private Producer<String,String> createKafkaProducer(){
Properties props = new Properties();
props.put("bootstrap.servers", "10.10.22.0:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
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> kafkaProducer = new KafkaProducer<>(props);
return kafkaProducer;
}
void produce(){
for(int i=1;i<1000;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String key=String.valueOf("key"+i);
String data="hello kafka message:"+key;
kafkaProducer.send(new ProducerRecord<>(TOPIC, key, data), new Callback() {
@Override
public void onCompletion(RecordMetadata recordMetadata, Exception e) {
}
});
System.out.println(data);
}
}
public static void main(String[] args){
new KafkaProducerDemo().produce();
}
}
public class KafkaConsumerDemo {
private final KafkaConsumer<String, String> consumer;
private KafkaConsumerDemo(){
Properties props = new Properties();
props.put("bootstrap.servers", "10.10.22.0:9092");
props.put("group.id", "test");
props.put("enable.auto.commit", "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 = new KafkaConsumer<>(props);
}
void consume(){
consumer.subscribe(Arrays.asList(KafkaProducerDemo.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());
}
}
public static void main(String[] args){
new KafkaConsumerDemo().consume();
}
}