Docker

+번외) Kafka 명령어 정리

hwanguu 2024. 5. 18. 00:55

토픽

토픽 생성

kafka-topics --create --topic test-topic --bootstrap-server host.docker.internal:9092 --partitions 2 --replication-factor 3

 


토픽에 대한 정보 확인

kafka-topics --describe --topic test-topic --bootstrap-server host.docker.internal:9093

 

 

 

토픽 설정

토픽 파티션 개수 변경

kafka-topics --alter --topic test-topic --partitions 2 --bootstrap-server host.docker.internal:9093

토픽 메세지 보관 시간 설정
kafka-configs --bootstrap-server host.docker.internal:9092 --entity-type topics --entity-name test-topic --alter --add-config retention.ms=8460000

토픽 메세지가 삭제된후 영구 삭제 이전까지 얼마나 보관할것인지 시간 설정
kafka-configs --bootstrap-server host.docker.internal:9092 --entity-type topics --entity-name test-topic --alter --add-config delete.retention.ms=8460000

 


Consumer

ex.) kafka-console-consumer --topic test-topic --bootstrap-server host.docker.internal:9094 host.docker.internal:9092 host.docker.internal:9093 --from-beginning --group group1

 

option

--from-beginning : 토픽의 처음부터 모든 메세지를 가져오도록 하는 옵션
    ex.) kafka-console-consumer --topic test-topic --bootstrap-server host.docker.internal:9094 host.docker.internal:9092 host.docker.internal:9093 --from-beginning

--group : group
    ex.) kafka-console-consumer --topic test-topic --bootstrap-server host.docker.internal:9094 host.docker.internal:9092 host.docker.internal:9093 --from-beginning --group group1

--property print.key=true --property key.separator="-" : 메세지의 키값 같이 표시하기
    ex.) kafka-console-consumer --topic test-topic --bootstrap-server host.docker.internal:9094 host.docker.internal:9092 host.docker.internal:9093 --from-beginning --group group2 --property print.key=true --property key.separator="-"


Producer

kafka-console-producer --bootstrap-server host.docker.internal:9093 host.docker.internal:9092 host.docker.internal:9094  --topic test-topic --request-required-acks 0

 

option

--request-required-acks : 옵션을 준경우 프로듀서가 메세지를 전송한 후 요청하는 확인 수준을 지정한다.
acks 0 인경우 : 프로듀서는 메세지를 전송한 후 브로커로 부터 어떤 확인을 받지 않는다. (속도가 가장 빠르며, 메세지가 유실될수 있는 경우에는 사용 X)
ex.) kafka-console-producer --bootstrap-server host.docker.internal:9093 host.docker.internal:9092 host.docker.internal:9094  --topic test-topic --request-required-acks 0

acks 1 인경우 : 메세지를 전송한 후 브로커에서 최소한 한 번의 확인을 받을 때까지 기다린다. (acks 0 보단 속도가 느리며, 메세지가 유실될 가능성이 매우 낮다)
ex.) kafka-console-producer --bootstrap-server host.docker.internal:9093 host.docker.internal:9092 host.docker.internal:9094  --topic test-topic --request-required-acks 1

acks all 또는 -1 인경우 : 메세지를 전송한 후 ISR의 모든 복제본에서 확인을 받을 때까지 대기한다. (acks 1 보다 속도가 느리며, 메세지가 유실될 가능성은 거의 없다)
ex.) kafka-console-producer --bootstrap-server host.docker.internal:9093 host.docker.internal:9092 host.docker.internal:9094  --topic test-topic --request-required-acks all

--message-send-max-retries : 옵션을 준 경우 메세지전송이 실패할 경우 재시도할 최대 횟수를 지정한다.
ex.) kafka-console-producer --bootstrap-server host.docker.internal:9093 host.docker.internal:9092 host.docker.internal:9094 --topic test-topic --message-send-max-retries 50

--> 아래와 같이 조합해서 사용할수 있을 듯
kafka-console-producer --bootstrap-server host.docker.internal:9093 host.docker.internal:9092 host.docker.internal:9094  --topic test-topic --request-required-acks 1 --message-send-max-retries 5


테스트 및 검증

--max-messages : 옵션을 줄경우 해당 횟수 만큼 토픽에 메세지를 전송한다.
ex.) kafka-verifiable-producer --bootstrap-server host.docker.internal:9092 --topic test-topic --max-messages 100