1. 生产者

import java.util.Properties; 
   
import kafka.javaapi.producer.Producer; 
import kafka.producer.KeyedMessage; 
import kafka.producer.ProducerConfig; 
   
public class MyProducer {   
     
        public static void main(String[] args) {   
            Properties props = new Properties();   
            props.setProperty("metadata.broker.list","localhost:9092");   
            props.setProperty("serializer.class","kafka.serializer.StringEncoder");   
            props.put("request.required.acks","1");   
            ProducerConfig config = new ProducerConfig(props);   
            //创建生产这对象
            Producer<String, String> producer = new Producer<String, String>(config);
            //生成消息
            KeyedMessage<String, String> data = new KeyedMessage<String, String>("mykafka","test-kafka");
            try {   
                int i =1; 
                while(i < 100){    
                    //发送消息
                    producer.send(data);   
                } 
            } catch (Exception e) {   
                e.printStackTrace();   
            }   
            producer.close();   
        }   
}
View Code

相关文章:

  • 2022-02-18
  • 2022-12-23
  • 2021-11-30
  • 2021-11-26
  • 2021-12-05
  • 2022-02-19
  • 2021-11-30
  • 2021-06-07
猜你喜欢
  • 2022-12-23
  • 2021-10-25
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
相关资源
相似解决方案