[Kafka]카프카 시작하기 예제


이 글은 카프카, 데이터 플랫폼의 최강자 고승범/공용준 님의 책을 공부하며
정리하는 글입니다.

[Kafka]카프카 시작하기 예제

카프카 명령어를 이용해 토픽을 생성
만들어진 토픽에 메시지를 보냄
토픽에서 메시지를 가져오기

카프카 토픽 생성

토픽생성 명령어
topic name: test-topic
토픽 생성 명령어 : kafka-topic.sh
주피커 정보 추가 옵션 명령어 : –zookeeper
–replicaition-factoer 옵션은 1
–partitions 옵션은 1
토픽생성: –topic 옵션(=토픽네임)
생성하는 옵션 : –create

  • 서버 한대(카프카 클러스터)에 접속
$ /home/shalee729/kafka/bin/kafka-topics.sh \  
--zookeeper zookeeper-01:2181,zookeeper-02:2181,zookeeper-03:2181/shalee729-kafka \  
--replication-factor 1 --partitions 1 --topic test-topic --create

출력: Created topic "test-topic".
  • /home/shalee729/kafka/bin/kafka-topics.sh //토픽 생성
  • zookeeper-01:2181,zookeeper-02:2181,zookeeper-03:2181/shalee729-kafka //주키퍼정보 + 지노드 설정디렉토리
  • –replication-factor 1 –partitions 1 –topic test-topic –create //복제1 파티션1 토픽생성 토픽네임 생성옵션


토픽삭제 명령어
브로커 환경설정에서 delete.topic.enable=true 로 설정
replication, partitions 옵션을 제거
제거하는 옵션 : –delete

  • /home/shalee729/kafka/bin/kafka-topics.sh //토픽 생성
  • zookeeper-01:2181,zookeeper-02:2181,zookeeper-03:2181/shalee729-kafka //주키퍼정보 + 지노드 설정디렉토리
  • –topic test-topic –delete //토픽생성 토픽네임 생성옵션



메시지를 보내보자

현재 카프카에 토픽이 만들어졌다
프로듀서로부터 메시지를 받을 준비가 된 상태
메시지를 퍼블리싱하는 명령어도 카프카에서 제공한다
프로듀서라고 생각하고 메세지를 카프카에 보내보자 메시지 퍼블리싱 명령 : kafka-console-producer.sh 카프카 서버정보 입력 : –broker-list 토픽 생성 : –topic 토픽네임

/home/shalee729/kafka/bin/kafka-console-producer.sh \  
--broker-list zookeeper-01:9092,zookeeper-02:9092,zookeeper-03:9092 \  
--topic test-topic
출력: > (이곳에 메세지를 넣는다)
>This is a message
>This is another message
>Ctrl +c 로 빠져나온다



메시지를 가져오자

메시지를 가져오는 명령 : kafka-console-consumer
카프카 호스트 정보입력 : –bootstrap-server
토픽네임 : –topic 토픽 네임
실행 : –from-beginning <- 해당 토픽을 처음부터 가져오는 옵션

/home/shalee729/kafka/bin/kafka-console-consumer.sh \
--bootstrap-server zookeeper-01:9092,zookeeper-02:9092,zookeeper-03:9092 \
--topic test-topic --from-beginning

출력:
This is a message
This is another message