当前位置:   article > 正文

Kafka开发实战(三)-Kafka API使用_kaf-cli api

kaf-cli api

上一篇Kafka开发实战(二)-集群环境搭建文章中,我们已经搭建起了Kafka集群了,接下来我们通过代码演示如何发布、订阅消息。 
1、添加maven依赖

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka-clients</artifactId>
  4. <version>0.9.0.1</version>
  5. </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

我使用的Kafka版本是0.9.0.1,下面来看看Kafka Producer代码

2、KafkaProducer

  1. package com.ricky.codelab.kafka;
  2. import java.io.IOException;
  3. import java.util.Properties;
  4. import org.apache.kafka.clients.producer.KafkaProducer;
  5. import org.apache.kafka.clients.producer.Producer;
  6. import org.apache.kafka.clients.producer.ProducerRecord;
  7. import com.ricky.codelab.kafka.util.PropertyUtils;
  8. public class KafkaProducerDemo {
  9. private int total = 1000000;
  10. public static void main(String[] args) {
  11. new KafkaProducerDemo().send();
  12. }
  13. public void send(){
  14. long start = System.currentTimeMillis();
  15. System.out.println("Kafka Producer send msg start,total msgs:"+total);
  16. // set up the producer
  17. Producer<String, String> producer = null;
  18. try {
  19. Properties props = PropertyUtils.load("producer_config.properties");
  20. producer = new KafkaProducer<>(props);
  21. for (int i = 0; i < total; i++){
  22. producer.send(new ProducerRecord<String, String>("hello",
  23. String.valueOf(i), String.format("{\"type\":\"test\", \"t\":%d, \"k\":%d}", System.currentTimeMillis(), i)));
  24. // every so often send to a different topic
  25. if (i % 1000 == 0) {
  26. producer.send(new ProducerRecord<String, String>("test", String.format("{\"type\":\"marker\", \"t\":%d, \"k\":%d}", System.currentTimeMillis(), i)));
  27. producer.send(new ProducerRecord<String, String>("hello", String.format("{\"type\":\"marker\", \"t\":%d, \"k\":%d}", System.currentTimeMillis(), i)));
  28. producer.flush();
  29. System.out.println("Sent msg number " + i);
  30. }
  31. }
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }finally{
  35. producer.close();
  36. }
  37. System.out.println("Kafka Producer send msg over,cost time:"+(System.currentTimeMillis()-start)+"ms");
  38. }
  39. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55


KafkaProducer在构造时需要传入一个Properties对象来传入Kafka相关配置信息,producer_config.properties如下:

  1. bootstrap.servers=172.18.19.206:9092,172.18.19.207:9092,172.18.19.208:9092
  2. acks=all
  3. retries=0
  4. batch.size=16384
  5. linger.ms=1
  6. buffer.memory=33554432
  7. auto.commit.interval.ms=1000
  8. key.serializer=org.apache.kafka.common.serialization.StringSerializer
  9. value.serializer=org.apache.kafka.common.serialization.StringSerializer
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3、KafkaConsumer

  1. package com.ricky.codelab.kafka;
  2. import java.io.IOException;
  3. import java.util.Arrays;
  4. import java.util.Properties;
  5. import org.apache.kafka.clients.consumer.ConsumerRecord;
  6. import org.apache.kafka.clients.consumer.ConsumerRecords;
  7. import org.apache.kafka.clients.consumer.KafkaConsumer;
  8. import com.google.gson.JsonObject;
  9. import com.google.gson.JsonParser;
  10. import com.ricky.codelab.kafka.util.PropertyUtils;
  11. public class KafkaConsumerDemo {
  12. public static void main(String[] args) {
  13. new KafkaConsumerDemo().consume();
  14. }
  15. public void consume() {
  16. JsonParser jsonParser = new JsonParser();
  17. // and the consumer
  18. KafkaConsumer<String, String> consumer = null;
  19. try {
  20. Properties props = PropertyUtils.load("consumer_config.properties");
  21. consumer = new KafkaConsumer<>(props);
  22. //subscribe topics
  23. consumer.subscribe(Arrays.asList("hello", "test"));
  24. while (true) {
  25. ConsumerRecords<String, String> records = consumer.poll(100);
  26. for (ConsumerRecord<String, String> record : records){
  27. // System.out.printf("offset -> %d, key -> %s, value -> %s",
  28. // record.offset(), record.key(), record.value());
  29. switch (record.topic()) {
  30. case "hello":
  31. JsonObject jObj = (JsonObject)jsonParser.parse(record.value());
  32. switch (jObj.get("type").getAsString()) {
  33. case "test":
  34. long latency = System.currentTimeMillis() - jObj.get("t").getAsLong();
  35. System.out.println(latency);
  36. break;
  37. case "marker":
  38. break;
  39. default:
  40. break;
  41. }
  42. break;
  43. case "test":
  44. break;
  45. default:
  46. throw new IllegalStateException("Shouldn't be possible to get message on topic " + record.topic());
  47. }
  48. }
  49. }
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }finally{
  53. if(consumer!=null){
  54. consumer.close();
  55. }
  56. }
  57. }
  58. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

KafkaConsumer在构造时也需要传入java.util.Properties对象,来告诉它Kafka相关的配置信息,consumer_config.properties如下:

  1. bootstrap.servers=172.18.19.206:9092,172.18.19.207:9092,172.18.19.208:9092
  2. group.id=test
  3. enable.auto.commit=true
  4. auto.commit.interval.ms=1000
  5. session.timeout.ms=30000
  6. key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
  7. value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在KafkaProducer和KafkaConsumer都使用到了PropertyUtils类,它的作用是将.properties文件转换为java.util.Properties对象,代码如下:

  1. package com.ricky.codelab.kafka.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.Properties;
  7. import org.apache.commons.io.IOUtils;
  8. public class PropertyUtils {
  9. private PropertyUtils(){
  10. }
  11. public static Properties load(File file) throws IOException{
  12. InputStream in = null;
  13. try {
  14. in = new FileInputStream(file);
  15. Properties props = new Properties();
  16. props.load(in);
  17. return props;
  18. }finally{
  19. IOUtils.closeQuietly(in);
  20. }
  21. }
  22. public static Properties load(String path) throws IOException{
  23. InputStream in = null;
  24. try {
  25. in = PropertyUtils.class.getClassLoader().getResourceAsStream(path);
  26. Properties props = new Properties();
  27. props.load(in);
  28. return props;
  29. }finally{
  30. IOUtils.closeQuietly(in);
  31. }
  32. }
  33. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

4、打包执行 
将KafkaProducerDemo.java、KafkaConsumerDemo.java 分别单独打成CLI jar包(KafkaProducer.jar和KafkaConsumer.jar),然后分别启动 KafkaProducer.jar和KafkaConsumer.java,就能看到控制台输出信息了。


点此下载完整项目代码


参考资料: 
KafkaProducer javadoc:http://kafka.apache.org/090/javadoc/index.html?org/apache/kafka/clients/producer/KafkaProducer.html

KafkaConsumer javadoc:http://kafka.apache.org/090/javadoc/index.html?org/apache/kafka/clients/producer/KafkaProducer.html

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FX_SKY/article/details/51290008
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号