简体   繁体   English

向 Kafka Producer 发送数据

[英]Sending Data to Kafka Producer

i am trying to read the 100k file and send it to kafka topic.我正在尝试读取 100k 文件并将其发送到 kafka 主题。 Here is my Kafka Code Which sends data to Kafka-console-consumer.这是我的 Kafka 代码,它向 Kafka-console-consumer 发送数据。 When i am sending data i am receiving the data like this当我发送数据时,我正在接收这样的数据

java.util.stream.ReferencePipeline$Head@e9e54c2

Here is the sample single record data what i am sending:这是我发送的示例单记录数据:

173|172686|548247079|837113012|0x548247079f|7|173|172686a|0|173|2059 22143|0|173|1|173|172686|||0|||7|0||7|||7|172686|allowAllServices|?20161231:22143|548247079||0|173||172686|5:2266490827:DCCInter;20160905152146;2784

Any suggestion to get the data which i had showned in above...Thanks获取我在上面显示的数据的任何建议......谢谢

Code:代码:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

@SuppressWarnings("unused")
public class HundredKRecords { 
   private static String sCurrentLine;
   public static void main(String args[]) throws InterruptedException, ExecutionException{ 
       String fileName = "/Users/sreeeedupuganti/Downloads/octfwriter.txt";

       //read file into stream, try-with-resources
       try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
           stream.forEach(System.out::println);
           kafka(stream.toString());
       } catch (IOException e) {
           e.printStackTrace();
       }
       }

   public static void kafka(String stream)  {
       Properties props = new Properties();
       props.put("metadata.broker.list", "localhost:9092");
       props.put("serializer.class", "kafka.serializer.StringEncoder");
       props.put("partitioner.class","kafka.producer.DefaultPartitioner");
       props.put("request.required.acks", "1");
       ProducerConfig config = new ProducerConfig(props);
       Producer<String, String> producer = new Producer<String, String>(config);
       producer.send(new KeyedMessage<String, String>("test",stream));
       producer.close();
   }
}

Problem is in line kafka(stream.toString());问题出在kafka(stream.toString());

Java stream class doesn't override method toString . Java 流类不会覆盖方法toString By default it returns getClass().getName() + '@' + Integer.toHexString(hashCode()) .默认情况下,它返回getClass().getName() + '@' + Integer.toHexString(hashCode()) That's exactly that you recieve.这正是你收到的。

In order to receive in kafka the whole file, you have manually convert it to one String (array of bytes).为了在 kafka 中接收整个文件,您必须手动将其转换为一个字符串(字节数组)。

Please, note, that kafka has limit for message size.请注意,kafka 对消息大小有限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM