How to write a file to Kafka Producer

后端 未结 4 470
甜味超标
甜味超标 2020-12-07 20:57

I am trying to load a simple text file instead of standard input in Kafka. After downloading Kafka, I performed the following steps:

Started zookeeper:

相关标签:
4条回答
  • 2020-12-07 21:36

    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
    
    0 讨论(0)
  • 2020-12-07 21:42
    echo "Hello" | kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic
    
    0 讨论(0)
  • 2020-12-07 21:44

    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

    0 讨论(0)
  • 2020-12-07 21:46
    $ kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic < my_file.txt
    

    worked for me in Kafka-0.9.0

    0 讨论(0)
提交回复
热议问题