卡夫卡生产发送图像

编程入门 行业动态 更新时间:2024-10-21 22:57:47
本文介绍了卡夫卡生产发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下卡夫卡生产者代码.我想知道是否可以发送图像文件而不是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:9092"); props.put("client.id", CLIENT_ID); props.put("key.serializer", "org.apache.kafkamon.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafkamon.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.kafkamon.serialization.StringSerializer");

至:-

props.put("value.serializer", "org.apache.kafkamon.serialization.ByteArraySerializer");

3)给出图像文件的字节数组

3) give your bytesarray of your image file

而且,你很好.

更多推荐

卡夫卡生产发送图像

本文发布于:2023-11-26 00:33:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:卡夫卡   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!