Kafka生产发送图像 [英] Kafka produce to send image

查看:34
本文介绍了Kafka生产发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Kafka 生产者代码.我想知道我是否可以发送图像文件而不是 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) 将这一行从 :-

2) change this line from :-

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

而且,你可以走了.

这篇关于Kafka生产发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆