#title Kafka Quick Start - Python Consumer [[TableOfContents]] Windows 10에서 테스트 함 ==== 설치 및 실행 ==== [https://kafka.apache.org/quickstart 여기]를 참고했다. JRE가 설치되어 있어야 한다. 환경변수에 JAVA_HOME 세팅되어야 한다. 나는 JAVA_HOME이 C:\Program Files\Java\jdk-13.0.2다. 1. 다운로드 및 압축 해제(나는 D:\kafka_2.12-2.5.0) 2. 명령창 하나 띄워 다음과 같이 주키퍼 실행 {{{ d: cd D:\kafka_2.12-2.5.0\bin\windows zookeeper-server-start.bat D:\kafka_2.12-2.5.0\config\zookeeper.properties }}} 3. 명령창 하나를 더 띄우고, 다음과 같이 카프카 실행 {{{ d: cd D:\kafka_2.12-2.5.0\bin\windows kafka-server-start.bat D:\kafka_2.12-2.5.0\config/server.properties }}} 4. 명령창 하나를 더 띄우고, 토픽을 하나 만든다. {{{ d: cd D:\kafka_2.12-2.5.0\bin\windows kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test kafka-topics.bat --list --bootstrap-server localhost:9092 }}} 5. 명령창 하나를 더 띄우고, 다음 실행 producer {{{ d: cd D:\kafka_2.12-2.5.0\bin\windows kafka-console-producer.bat --bootstrap-server localhost:9092 --topic test }}} ctrl+c를 누르면 나가는데, 그냥 둔다. 6. 명령창 하나를 더 띄우고, 다음 실행 consumer {{{ d: cd D:\kafka_2.12-2.5.0\bin\windows kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning }}} 아래 그림 * 상단 명령창은 producer * 하단 명령창은 consumer 상단 명령창에서 메시지를 이력하면 하단 명령창에 프린트 되는 것을 볼 수 있다. attachment:KafkaQuickStart/kafka_test.png ==== 파이썬 예제 ==== {{{ pip install kafka-python }}} {{{ from kafka import KafkaConsumer import sys bootstrap_servers = ["localhost:9092"] topicName = 'test' consumer = KafkaConsumer(topicName, group_id='group1', bootstrap_servers=bootstrap_servers, auto_offset_reset='earliest') for message in consumer: print(message.value) ''' try: for message in consumer: #print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,message.offset, message.key,message.value)) print(message.value) except KeyboardInterrupt: sys.exit() ''' }}} 위 소스를 실행하면 producer가 메시지를 던질 때마다 출력되는 것을 볼 수 있다. ==== 참고 ==== * [https://blog.voidmainvoid.net/305 카프카 auto.offset.reset 종류 및 사용방법] * [https://hahahia.tistory.com/158 kafka consumer의 commitSync/seek를 이용한 offset 관리 예제] * [https://jhleed.tistory.com/180 consumer group 이해] * [http://activisiongamescience.github.io/2016/06/15/Kafka-Client-Benchmarking/ Python Kafka Client Benchmarking] * [https://anaconda.org/ActivisionGameScience/confluent-kafka confluent-kafka]가 제일 빠르다 * https://docs.confluent.io/current/clients/python.html * [https://dzone.com/articles/introducing-hive-kafka-integration-for-real-time-k Introducing Hive-Kafka Integration for Real-Time Kafka SQL Queries] ---- ewrqwrwe -- qwer 2021-04-23 10:18:44