Kafka에서 Console(터미널, CLI)에서 토픽 데이터를 확인하는 명령어를 정리했습니다.
Kafka를 운영하거나 디버깅할 때 필수적인 명령어입니다. 🚀
✅ 1. Kafka Console Producer (데이터 입력)
Kafka Producer를 실행하여 특정 토픽에 메시지를 보낼 수 있습니다.
kafka-console-producer.sh --broker-list <BROKER_IP>:<PORT> --topic <TOPIC_NAME>
예제:
kafka-console-producer.sh --broker-list localhost:9092 --topic test-topic
이후 입력 후 Enter를 치면 해당 메시지가 Kafka 토픽에 전송됩니다.
✅ 2. Kafka Console Consumer (데이터 조회)
Kafka Consumer를 실행하여 특정 토픽에서 메시지를 소비(조회)할 수 있습니다.
kafka-console-consumer.sh --bootstrap-server <BROKER_IP>:<PORT> --topic <TOPIC_NAME> --from-beginning
예제:
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning
👉 --from-beginning 옵션을 추가하면, 해당 토픽의 모든 메시지를 처음부터 조회할 수 있습니다.
👉 --from-beginning 없이 실행하면 새로 들어오는 데이터만 소비합니다.
✅ 3. Kafka Topic 목록 확인
Kafka에서 생성된 토픽 리스트(목록)를 확인하려면 다음 명령어를 실행합니다.
kafka-topics.sh --bootstrap-server <BROKER_IP>:<PORT> --list
예제:
kafka-topics.sh --bootstrap-server localhost:9092 --list
👉 실행하면 Kafka 클러스터 내의 모든 토픽 리스트가 출력됩니다.
✅ 4. 특정 Kafka Topic 정보 확인
Kafka의 특정 토픽 정보를 조회하려면 아래 명령어를 사용합니다.
kafka-topics.sh --bootstrap-server <BROKER_IP>:<PORT> --describe --topic <TOPIC_NAME>
예제:
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic test-topic
👉 출력 정보 예시:
Topic: test-topic PartitionCount: 3 ReplicationFactor: 2
Topic: test-topic Partition: 0 Leader: 1 Replicas: 1,2 Isr: 1,2
Topic: test-topic Partition: 1 Leader: 2 Replicas: 2,3 Isr: 2,3
✅ 파티션 개수, 리더 브로커, 복제(replication) 정보 등을 확인할 수 있습니다.
✅ 5. Kafka Consumer Group 목록 확인
Kafka의 Consumer Group(소비자 그룹) 리스트를 조회하는 명령어입니다.
kafka-consumer-groups.sh --bootstrap-server <BROKER_IP>:<PORT> --list
예제:
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
👉 실행하면 현재 Kafka 클러스터에서 운영 중인 Consumer Group 목록이 출력됩니다.
✅ 6. 특정 Consumer Group 상태 확인
Kafka의 특정 Consumer Group이 어떤 토픽을 구독하고 있는지 확인할 수 있습니다.
kafka-consumer-groups.sh --bootstrap-server <BROKER_IP>:<PORT> --group <GROUP_NAME> --describe
예제:
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-consumer-group --describe
👉 출력 예시:
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID
test-topic 0 1000 1200 200 consumer-1
test-topic 1 800 800 0 consumer-2
✅ LAG 값이 높다면 Consumer가 메시지를 늦게 소비하고 있다는 의미입니다.
✅ 7. 특정 Consumer Group 리셋 (Offset 초기화)
Kafka에서 특정 Consumer Group의 Offset을 리셋(초기화)할 때 사용하는 명령어입니다.
kafka-consumer-groups.sh --bootstrap-server <BROKER_IP>:<PORT> --group <GROUP_NAME> --reset-offsets --to-earliest --execute
예제:
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-consumer-group --reset-offsets --to-earliest --execute
✅ 이 명령어를 실행하면 Consumer가 처음부터 데이터를 다시 소비합니다.
📌 주의: --execute를 붙이면 즉시 적용되므로, 먼저 --dry-run으로 테스트하는 것이 좋습니다.
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-consumer-group --reset-offsets --to-earliest --dry-run
✅ 8. Kafka 메시지 개수 확인 (Consumer Lag)
Kafka에서 특정 토픽의 메시지 개수를 확인하려면 아래 명령어를 사용합니다.
kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list <BROKER_IP>:<PORT> --topic <TOPIC_NAME> --time -1
예제:
kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test-topic --time -1
👉 실행하면 토픽의 현재 메시지 개수(Offset 정보)를 확인할 수 있습니다.
✅ 9. Kafka 메시지 삭제
Kafka에서는 기본적으로 메시지를 삭제할 수 없지만, 특정 토픽의 데이터를 강제로 정리하려면 Retention Time을 변경해야 합니다.
kafka-topics.sh --bootstrap-server <BROKER_IP>:<PORT> --alter --topic <TOPIC_NAME> --config retention.ms=1000
예제:
kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic test-topic --config retention.ms=1000
👉 1초(retention.ms=1000) 후 데이터가 삭제됨.
👉 이후 기본 설정으로 복구하려면 다음 명령어 실행:
kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic test-topic --delete-config retention.ms
✅ 10. Kafka 브로커 상태 확인
Kafka 클러스터에서 브로커 상태를 확인하려면 ZooKeeper를 통해 조회할 수 있습니다.
zookeeper-shell.sh <ZOOKEEPER_IP>:<PORT> ls /brokers/ids
예제:
zookeeper-shell.sh localhost:2181 ls /brokers/ids
👉 실행하면 현재 가동 중인 Kafka 브로커 목록이 출력됩니다.
🚀 Kafka CLI 명령어 요약 정리
기능명령어
Producer 실행 (데이터 입력) | kafka-console-producer.sh --broker-list localhost:9092 --topic test-topic |
Consumer 실행 (데이터 조회) | kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning |
토픽 목록 조회 | kafka-topics.sh --bootstrap-server localhost:9092 --list |
토픽 정보 확인 | kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic test-topic |
Consumer Group 목록 조회 | kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list |
Consumer Group 상태 확인 | kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-group --describe |
Consumer Offset 초기화 | kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-group --reset-offsets --to-earliest --execute |
메시지 개수 확인 | kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test-topic --time -1 |
브로커 상태 확인 | zookeeper-shell.sh localhost:2181 ls /brokers/ids |
📌 결론
Kafka에서 Console(터미널) 명령어를 사용하면 Producer, Consumer, Topic 관리, Consumer Group 모니터링 등 다양한 작업을 수행할 수 있습니다.
👉 Kafka 운영 및 디버깅 시 자주 사용하는 CLI 명령어이므로, 꼭 익혀두세요! 🚀