将数据发送给Kafka Producer

时间:2016-09-22 10:45:51

标签: java streaming apache-kafka kafka-producer-api

我正在尝试阅读100k文件并将其发送到kafka主题。这是我的Kafka代码,它将数据发送给Kafka-console-consumer。当我发送数据时,我正在接收这样的数据

java.util.stream.ReferencePipeline$Head@e9e54c2

以下是我发送的单个记录数据示例:

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

有任何关于获取我在上面展示过的数据的建议......谢谢

代码:

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();
   }
}

1 个答案:

答案 0 :(得分:1)

问题在行kafka(stream.toString());

Java流类不会覆盖方法toString。默认情况下,它返回getClass().getName() + '@' + Integer.toHexString(hashCode())。这就是你收到的。

为了在kafka中接收整个文件,您已手动将其转换为一个String(字节数组)。

请注意,kafka对邮件大小有限制。