How to write a file to Kafka Producer

99封情书 提交于 2019-11-28 04:31:54

You can pipe it in:

kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic
--new-producer < my_file.txt

Found here.

From 0.9.0:

kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic < my_file.txt
$ kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic < my_file.txt

worked for me in Kafka-0.9.0

Here are few ways which are little more generalised but may be overkill for simple file

tail

tail -n0 -F my_file.txt | kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic

Explanation

  1. tail reads from the end of the file as it grows or logs are being added to it continuously
  2. -n0 indicates outputlast 0 lines so only new line is selected
  3. -F follows the file by name instead the descriptor, hence it works even if it is rotated

syslog-ng

options {                                                                                                                             
    flush_lines (0);                                                                                                                
    time_reopen (10);                                                                                                               
    log_fifo_size (1000);                                                                                                          
    long_hostnames (off);                                                                                                           
    use_dns (no);                                                                                                                   
    use_fqdn (no);                                                                                                                  
    create_dirs (no);                                                                                                               
    keep_hostname (no);                                                                                                             
};

source s_file {
    file("path to my-file.txt" flags(no-parse));
}


destination loghost {
    tcp("*.*.*.*" port(5140));
} 

consuming

nc -k -l 5140 | kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic

Explanation(from man nc)

-k' Forces nc to stay listening for another connection after its current connection is completed. It is an error to use this option without the -l option.

-l' Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host. It is an error to use this option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored.

Ref

Syslog-ng

echo "Hello" | kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!