Kafka -- Java API

1、导入依赖:
        
            org.apache.kafka
            kafka-clients
            1.0.0
        
2、编写代码:
public class Demo01 {
    public static void main(String[] args) {
        //使用java向flink写入数据
            Properties properties = new Properties();
            properties.setProperty("bootstrap.servers", "master:9092,node2:9092,node2:9092");

            //指定数据的格式
            properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

            //创建生产者
            KafkaProducer kafkaProducer = new KafkaProducer<>(properties);

            //生产数据
            kafkaProducer.send(new ProducerRecord<>("students", "1500100006,student,21,男,理科二班"));

            kafkaProducer.flush();

            //关闭连接
            kafkaProducer.close();
    }
}

你可能感兴趣的:(Kafka,2.11-1.0.0,kafka,java,linq)