【发布时间】:2019-01-23 21:24:12
【问题描述】:
我有以下 Kafka 生产者代码。我想知道我是否可以发送图像文件而不是 JSON 文件?是否有任何代码参考通过 Kafka 生产者发送图像文件?
try {
URL url = getClass().getResource("test.json");
File file = new File(url.getPath());
Properties props = new Properties();
props.put("bootstrap.servers", "yupsectory.selet.com:9092");
props.put("client.id", CLIENT_ID);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
String jsonData = readFile(file.getAbsolutePath());
JSONObject jobj = new JSONObject(jsonData);
System.out.println("jarr: " + jobj.getJSONObject("data").toString());
Producer<String, String> producer = new KafkaProducer <>(props);
//Use this util to pull the context that needs to be propagated from the HttpServletRequest
Map<String, String> headermap = YupsectoryContextUtil.buildContextMap(request);
//Sending a message
ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, jobj.getJSONObject("data").toString());
producer.send(record, headermap);
producer.close();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
【问题讨论】:
-
你到底想要什么?你试过了吗?有什么问题吗?
-
对于初学者,您不会对图像文件使用任何 JSON 类或字符串...您将发送字节,并使用 ByteArraySerializer...所以您应该询问或搜索“如何在 Java 中获取图像文件的字节数"
标签: java image apache-kafka