Jak napisać plik do producenta Kafka
Próbuję załadować prosty plik tekstowy zamiast standardowego wejścia w Kafce. Po pobraniu Kafki wykonałem następujące kroki:
Rozpoczął zookeeper:
bin/zookeeper-server-start.sh config/zookeeper.properties
Uruchomiony Serwer
bin/kafka-server-start.sh config/server.properties
Utworzono temat o nazwie "test":
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Prowadził Producent:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
Test1
Test2
Słuchany przez Konsumenta:
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
Test1
Test2
Zamiast standardowego wejścia, chcę przekazać plik z danymi lub nawet prosty plik tekstowy do producenta, który może być widoczny bezpośrednio przez Konsumenta. Każda pomoc będzie naprawdę mile widziana. Dzięki!
4 answers
You can pipe it in:
kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic
--new-producer < my_file.txt
Znaleziono tutaj.
Od 0.9.0:
kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic < my_file.txt
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-01 15:43:09
$ kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic < my_file.txt
Pracował dla mnie w Kafka-0.9.0
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-04 09:42:57
Oto kilka sposobów, które są nieco bardziej uogólnione, ale mogą być przesadą dla prostego pliku
Ogon
tail -n0 -F my_file.txt | kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic
Wyjaśnienie
-
tail
odczytuje z końca pliku, gdy rośnie lub logi są dodawane do niego w sposób ciągły -
-n0
wskazuje outputlast 0 linii, więc wybrana jest tylko nowa linia -
-F
podąża za plikiem po nazwie zamiast deskryptora, dlatego działa nawet jeśli jest 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));
}
konsumowanie
nc -k -l 5140 | kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic
Wyjaśnienie (z 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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-17 18:52:18
echo "Hello" | kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-28 18:37:27