Kafka生产者
parent
3bff2fa630
commit
dd08255e8d
|
@ -0,0 +1,44 @@
|
||||||
|
package com.muyu.car;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
|
import org.apache.kafka.clients.producer.Producer;
|
||||||
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class Kafka {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//配置kafka生产者
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.put("bootstrap.servers", "http://49.235.108.160:9092");
|
||||||
|
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
|
||||||
|
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
|
||||||
|
//创建kafka生产者
|
||||||
|
Producer<String, String> product = new KafkaProducer<>(properties);
|
||||||
|
String aa = "2345678";
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("aa", aa);
|
||||||
|
String jsonString = jsonObject.toString();
|
||||||
|
System.out.println("JSON内容是:" + jsonString);
|
||||||
|
try {
|
||||||
|
product.send(new ProducerRecord<>("nima", UUID.randomUUID().toString(), jsonString
|
||||||
|
));
|
||||||
|
System.out.println("消费的数据内容为:" + jsonString);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
product.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue