What is the method of kafka based on producer and consumer
This article mainly introduces "what is the method of kafka based on producer and consumer". In the daily operation, I believe that many people have doubts about what the method of kafka based on producer and consumer is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of kafka based on producer and consumer?" Next, please follow the editor to study!
Producer:
Properties props = new Properties (); props.put ("bootstrap.servers", "node0:9092,node1:9092,node2: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 producer = new KafkaProducer (props); for (int I = 0; I < 100; iTunes +) producer.send (new ProducerRecord ("my-topic", Integer.toString (I), Integer.toString (I)); producer.close ()
Consumer:
Properties props = new Properties (); props.put ("bootstrap.servers", "node0:9092,node1:9092,node2:9092"); props.put ("group.id", "test"); props.put ("enable.auto.commit", "true"); props.put ("auto.commit.interval.ms", "1000"); props.put ("session.timeout.ms", "30000"); props.put ("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer") Props.put ("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); KafkaConsumer consumer = new KafkaConsumer (props); consumer.subscribe (Arrays.asList ("my-topic")); while (true) {ConsumerRecords records = consumer.poll; for (ConsumerRecord record: records) System.out.printf ("offset =% d, key =% s, value =% s\ n\ r", record.offset (), record.key (), record.value ());}
Compared with the old version, the new api is more readable, less readable and much more convenient to use.
But it's a pity that I want to try kafkastream at the same time, but the example code found on java doc doesn't match the jar package. I don't know why.
At this point, the study of "what is the method of kafka based on producer and consumer" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!