SlideShare a Scribd company logo
단기 완성
Apache Kafka
월간슬라이드 - 2019년 3월
고재성
무엇?
데이터를...
어디선가 보내고… -> Producer
어디선가 받는다.... -> Consumer
이 데이터들이 거치는 Message(Data) Queue ->
Kafka
왜 나왔나?
일반적인 환경…
데이터의 흐름이 복잡하다.
단일 데이터 보는것, 얻는 것은 어렵
다.
왜 쓰나?
- 데이터를 하나로 모을수 있다.
- 비동기적으로 실시간 처리 액세스
- Low Latency
- VS HDFS
- 똑같이 데이터를 하나로 모을 수
있다
- 실시간 처리능력은 떨어진다
특징은?
- Publish / Subscribe
- Producer (메시지 생산) / Consumer (메시지 소비)
- Publish and subscribe to streams of records, similar to a message queue
or enterprise messaging system.
- Process
- 실시간 스트림을 처리 가능 (Kafka Streams)
- Process streams of records as they occur.
- Store
- 메시지를 파일시스템에 저장 (보관주기만큼 저장가능)
- Store streams of records in a fault-tolerant durable way.
Core API
메시지 생산
publish a stream of records to one or more Kafka
topics.
메시지 소비
subscribe to one or more topics and process the
stream of records produced to them.
메시지 스트림 처리
act as a stream processor,
consuming an input stream from
one or more topics and producing
an output stream to one or
more output topics, effectively
transforming the input streams to
output streams.
다른 데이터 소스를 카프카에
연결
For example, a
connector to a
relational database might
capture every change to
a table.
카프카 들여다 보기
Kafka Queue 토픽(topic)
- 프로듀서와 컨슈머들이 카프카로 보낸 자신들의
메시지를 구분하기 위한 네임으로 사용
- 0..N개의 토픽이 사용될 수 있다.
파티션(partition)
- 병렬처리가 가능하도록 토픽을 나눌수 있고,
많은 양의 메시지 처리를 위해 파티션의
수를 늘려줄 수 있습니다.
토픽 들여다 보기
토픽은 1~N개 까지의 파티션을 가질 수
있다.
특정 토픽에 프로듀서가 쓴
메시지들이 각 파티션에 골고루 쌓이게
된다.
파티션 들여다 보기
메시지를 한번 쓰고 여러번 읽을 수 있다.(Consume
할 수 있다)
Offset = 어디까지 읽었는지(Consume 했는지) 위
치
Offset은 주키퍼나 카프카 내부에 저장된다.
어떻게 하면 여러번 읽을 수 있나?
-> 컨슈머 그룹을 별도로 주면 된다.
컨슈머 들여다 보기
컨슈머 - 토픽 내 파티션에서 메시지를 하나
씩 꺼내서 소비하는 어플리케이션
마지막으로 사용한 메시지의 오프셋을 저장함으로
써 컨슈머는는 장소를 잃지 않고 중지하고 다
시 시작할 수 있다.
하나의 컨슈머는 하나의 파티션만 읽을 수 있
다. (1:1)
왼쪽과 같이 파티션 3개면..
컨슈머1은..
파티션0과 파티션1을 번갈아 소비한다.
컨슈머는 컨슈머그룹으로 묶일수 있다.
컨슈머그룹 들여다 보기
컨슈머 그룹은..
각 파티션이 컨슈머 그룹 중 하나의 구성원에
의해서만 사용된다는 것을 보장한다.
컨슈머와 프로듀서의 상관관계
c : consumer
p : producer
컨슈머가 파티션보다 많다고 해서 처리가 빨라지는것은
아니다.
간단 실습1 (for Mac, 1/2)
설치
# brew install kafka
주키퍼 구동
# zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties
카프카 구동
# kafka-server-start /usr/local/etc/kafka/server.properties
간단 실습1 (for Mac, 2/2)
토픽생성
# kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Created topic "test".
토픽 보내기(produce)
# kafka-console-producer --broker-list localhost:9092 --topic test
>aaa
>bbb
>ccc
>
토픽 수신(consume)
# kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
aaa
bbb
ccc
간단 실습2 (spring-boot, spring-
kafka)
Application.java
@SpringBootApplication
public class Application implements CommandLineRunner {
public static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args).close();
}
@Autowired
private KafkaTemplate<String, String> template;
private final CountDownLatch latch = new CountDownLatch(3);
@Override
public void run(String... args) throws Exception {
this.template.send("myTopic", "foo1");
this.template.send("myTopic", "foo2");
this.template.send("myTopic", "foo3");
latch.await(60, TimeUnit.SECONDS);
logger.info("All received");
}
@KafkaListener(topics = "myTopic")
public void listen(ConsumerRecord<?, ?> cr) throws Exception {
logger.info(cr.toString());
latch.countDown();
}
}
application.properties
spring.kafka.consumer.group-id=foo
spring.kafka.consumer.auto-offset-reset=earliest

More Related Content

Similar to [월간 슬라이드] 단기완성 Apache kafka

Apache kafka intro_20150313_springloops
Apache kafka intro_20150313_springloopsApache kafka intro_20150313_springloops
Apache kafka intro_20150313_springloops
SungMin OH
 
MSA와 infra
MSA와 infraMSA와 infra
MSA와 infra
Je Hun Kim
 
Apache kafka performance(throughput) - without data loss and guaranteeing dat...
Apache kafka performance(throughput) - without data loss and guaranteeing dat...Apache kafka performance(throughput) - without data loss and guaranteeing dat...
Apache kafka performance(throughput) - without data loss and guaranteeing dat...
SANG WON PARK
 
Start spark
Start sparkStart spark
Start spark
ssuser31a17d
 
NoSQL 간단한 소개
NoSQL 간단한 소개NoSQL 간단한 소개
NoSQL 간단한 소개
Wonchang Song
 
cassandra overview & spark to cassandra
cassandra overview & spark to cassandra cassandra overview & spark to cassandra
cassandra overview & spark to cassandra
SuseongPark
 

Similar to [월간 슬라이드] 단기완성 Apache kafka (6)

Apache kafka intro_20150313_springloops
Apache kafka intro_20150313_springloopsApache kafka intro_20150313_springloops
Apache kafka intro_20150313_springloops
 
MSA와 infra
MSA와 infraMSA와 infra
MSA와 infra
 
Apache kafka performance(throughput) - without data loss and guaranteeing dat...
Apache kafka performance(throughput) - without data loss and guaranteeing dat...Apache kafka performance(throughput) - without data loss and guaranteeing dat...
Apache kafka performance(throughput) - without data loss and guaranteeing dat...
 
Start spark
Start sparkStart spark
Start spark
 
NoSQL 간단한 소개
NoSQL 간단한 소개NoSQL 간단한 소개
NoSQL 간단한 소개
 
cassandra overview & spark to cassandra
cassandra overview & spark to cassandra cassandra overview & spark to cassandra
cassandra overview & spark to cassandra
 

[월간 슬라이드] 단기완성 Apache kafka

  • 2. 무엇? 데이터를... 어디선가 보내고… -> Producer 어디선가 받는다.... -> Consumer 이 데이터들이 거치는 Message(Data) Queue -> Kafka
  • 3. 왜 나왔나? 일반적인 환경… 데이터의 흐름이 복잡하다. 단일 데이터 보는것, 얻는 것은 어렵 다.
  • 4. 왜 쓰나? - 데이터를 하나로 모을수 있다. - 비동기적으로 실시간 처리 액세스 - Low Latency - VS HDFS - 똑같이 데이터를 하나로 모을 수 있다 - 실시간 처리능력은 떨어진다
  • 5. 특징은? - Publish / Subscribe - Producer (메시지 생산) / Consumer (메시지 소비) - Publish and subscribe to streams of records, similar to a message queue or enterprise messaging system. - Process - 실시간 스트림을 처리 가능 (Kafka Streams) - Process streams of records as they occur. - Store - 메시지를 파일시스템에 저장 (보관주기만큼 저장가능) - Store streams of records in a fault-tolerant durable way.
  • 6. Core API 메시지 생산 publish a stream of records to one or more Kafka topics. 메시지 소비 subscribe to one or more topics and process the stream of records produced to them. 메시지 스트림 처리 act as a stream processor, consuming an input stream from one or more topics and producing an output stream to one or more output topics, effectively transforming the input streams to output streams. 다른 데이터 소스를 카프카에 연결 For example, a connector to a relational database might capture every change to a table.
  • 7. 카프카 들여다 보기 Kafka Queue 토픽(topic) - 프로듀서와 컨슈머들이 카프카로 보낸 자신들의 메시지를 구분하기 위한 네임으로 사용 - 0..N개의 토픽이 사용될 수 있다. 파티션(partition) - 병렬처리가 가능하도록 토픽을 나눌수 있고, 많은 양의 메시지 처리를 위해 파티션의 수를 늘려줄 수 있습니다.
  • 8. 토픽 들여다 보기 토픽은 1~N개 까지의 파티션을 가질 수 있다. 특정 토픽에 프로듀서가 쓴 메시지들이 각 파티션에 골고루 쌓이게 된다.
  • 9. 파티션 들여다 보기 메시지를 한번 쓰고 여러번 읽을 수 있다.(Consume 할 수 있다) Offset = 어디까지 읽었는지(Consume 했는지) 위 치 Offset은 주키퍼나 카프카 내부에 저장된다. 어떻게 하면 여러번 읽을 수 있나? -> 컨슈머 그룹을 별도로 주면 된다.
  • 10. 컨슈머 들여다 보기 컨슈머 - 토픽 내 파티션에서 메시지를 하나 씩 꺼내서 소비하는 어플리케이션 마지막으로 사용한 메시지의 오프셋을 저장함으로 써 컨슈머는는 장소를 잃지 않고 중지하고 다 시 시작할 수 있다. 하나의 컨슈머는 하나의 파티션만 읽을 수 있 다. (1:1) 왼쪽과 같이 파티션 3개면.. 컨슈머1은.. 파티션0과 파티션1을 번갈아 소비한다. 컨슈머는 컨슈머그룹으로 묶일수 있다.
  • 11. 컨슈머그룹 들여다 보기 컨슈머 그룹은.. 각 파티션이 컨슈머 그룹 중 하나의 구성원에 의해서만 사용된다는 것을 보장한다.
  • 12. 컨슈머와 프로듀서의 상관관계 c : consumer p : producer 컨슈머가 파티션보다 많다고 해서 처리가 빨라지는것은 아니다.
  • 13. 간단 실습1 (for Mac, 1/2) 설치 # brew install kafka 주키퍼 구동 # zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties 카프카 구동 # kafka-server-start /usr/local/etc/kafka/server.properties
  • 14. 간단 실습1 (for Mac, 2/2) 토픽생성 # kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test Created topic "test". 토픽 보내기(produce) # kafka-console-producer --broker-list localhost:9092 --topic test >aaa >bbb >ccc > 토픽 수신(consume) # kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning aaa bbb ccc
  • 15. 간단 실습2 (spring-boot, spring- kafka) Application.java @SpringBootApplication public class Application implements CommandLineRunner { public static Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class, args).close(); } @Autowired private KafkaTemplate<String, String> template; private final CountDownLatch latch = new CountDownLatch(3); @Override public void run(String... args) throws Exception { this.template.send("myTopic", "foo1"); this.template.send("myTopic", "foo2"); this.template.send("myTopic", "foo3"); latch.await(60, TimeUnit.SECONDS); logger.info("All received"); } @KafkaListener(topics = "myTopic") public void listen(ConsumerRecord<?, ?> cr) throws Exception { logger.info(cr.toString()); latch.countDown(); } } application.properties spring.kafka.consumer.group-id=foo spring.kafka.consumer.auto-offset-reset=earliest