Apache Kafka 简单生产者示例

SimpleProducer 应用程序
在创建应用程序之前,首先启动ZooKeeper和Kafka代理,然后使用create topic命令在Kafka代理中创建自己的主题。 之后,创建一个名为 SimpleProducer .java 的java类,然后键入以下代码。

//import util.properties packages
import java.util.Properties;

//import simple producer packages
import org.apache.kafka.clients.producer.Producer;

//import KafkaProducer packages
import org.apache.kafka.clients.producer.KafkaProducer;

//import ProducerRecord packages
import org.apache.kafka.clients.producer.ProducerRecord;

//Create java class named “SimpleProducer"
public class SimpleProducer {
   
   public static void main(String[] args) throws Exception{
      
      // Check arguments length value
      if(args.length == 0){
         System.out.println("Enter topic name");
         return;
      }
      
      //Assign topicName to string variable
      String topicName = args[0].toString();

      // create instance for properties to access producer configs   
      Properties props = new Properties();
      
      //Assign localhost id
      props.put("bootstrap.servers", "localhost:9092");
      
      //Set acknowledgements for producer requests.      
      props.put("acks", "all");
      
      //If the request fails, the producer can automatically retry,
      props.put("retries", 0);
      
      //Specify buffer size in config
      props.put("batch.size", 16384);
      
      //Reduce the no of requests less than 0   
      props.put("linger.ms", 1);
      
      //The buffer.memory controls the total amount of memory available to the producer for buffering.   
      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> producer = new KafkaProducer<String, String>(props);
            
      for(int i = 0; i < 10; i++) {

         producer.send(new ProducerRecord<String, String>(topicName, 
            Integer.toString(i), Integer.toString(i)));
               System.out.println("Message sent successfully");
      }
      producer.close();
   }
}

编译 - 可以使用以下命令编译应用程序。

javac -cp "/opt/kafka_2.11-1.0.0/libs/*" *.java

执行 - 可以使用以下命令执行应用程序。

java -cp "/opt/kafka_2.11-1.0.0/libs/*":. SimpleProducer test
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
0
1
2
3
4
5
6
7
8
9

SimpleConsumer应用程序
生产者应用程序步骤在此保持不变。 首先,启动你的ZooKeeper和Kafka代理。 然后使用名为 SimpleCon-sumer.java 的Java类创建一个 SimpleConsumer 应用程序,并键入以下代码。

import java.util.Properties;
import java.util.Arrays;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.ConsumerRecord;

public class SimpleConsumer {
   public static void main(String[] args) throws Exception {
      if(args.length == 0){
         System.out.println("Enter topic name");
         return;
      }
      //Kafka consumer configuration settings
      String topicName = args[0].toString();
      Properties props = new Properties();
      
      props.put("bootstrap.servers", "localhost: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<String, String> consumer = new KafkaConsumer<String, String>(props);
      
      //Kafka Consumer subscribes list of topics here.
      consumer.subscribe(Arrays.asList(topicName));
      
      //print the topic name
      System.out.println("Subscribed to topic " + topicName);
      int i = 0;
      
      while (true) {
         ConsumerRecords<String, String> records = consumer.poll(100);
         for (ConsumerRecord<String, String> record : records)
         
         // print the offset,key and value for the consumer records.
         System.out.printf("offset = %d, key = %s, value = %s\n", 
            record.offset(), record.key(), record.value());
      }
   }
}

编译 - 可以使用以下命令编译应用程序。

javac -cp "/opt/kafka_2.11-1.0.0/libs/*" *.java

执行 - 可以使用以下命令执行应用程序

java -cp "/opt/kafka_2.11-1.0.0/libs/*":. SimpleConsumer test

输入 -执行刚才的生产者代码

java -cp "/opt/kafka_2.11-1.0.0/libs/*":. SimpleProducer test

输出 - 以下是输出。

offset = 70, key = 0, value = 0
offset = 71, key = 1, value = 1
offset = 72, key = 2, value = 2
offset = 73, key = 3, value = 3
offset = 74, key = 4, value = 4
offset = 75, key = 5, value = 5
offset = 76, key = 6, value = 6
offset = 77, key = 7, value = 7
offset = 78, key = 8, value = 8
offset = 79, key = 9, value = 9

或者
输入 - 打开生成器CLI并向主题发送一些消息。 你可以输入为 this is a message。
输出 - 以下是输出。

offset = 80, key = null, value = this is a message

猜你喜欢

转载自blog.csdn.net/qq_26249609/article/details/85215968