卡夫卡生产发送图像 [英] Kafka produce to send image
本文介绍了卡夫卡生产发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下卡夫卡生产者代码.我想知道是否可以发送图像文件而不是JSON文件吗?是否有任何代码参考通过Kafka生产者发送图像文件?
I have the following Kafka producer code. I want to know if I can send image file instead of JSON file? Is there any code reference sending an image file through Kafka producer?
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();
}
推荐答案
您可以按照以下简单步骤发送图像:
You can send an image by following these simple steps:
1)将图像转换为字节数组(通过搜索如何获取图像文件的字节")
1) convert your image into bytes array ( by search for "how to get bytes of an image file )
2)从:-
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
至:-
props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
3)给出图像文件的字节数组
3) give your bytesarray of your image file
而且,你很好.
这篇关于卡夫卡生产发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文