Old Branch

Python - kinesis 데이터 주고받기

woolbro 2020. 5. 27. 13:00
반응형

Amazone Kinesis의 데이터 스트림에 데이터를 전달하고, 꺼내오는 일을 하게 되었습니다.

정말 간단한 얘기지만, 이렇게 저렇게 조사하고 서비스에 적용 했던 내용을 토대로 정리 해 보려고 합니다.

 

먼저, 글에서 얘기 할 producer, consumer 를 정리합니다.

- producer : 데이터를 생성하는 친구입니다.

- consumer : 데이터를 소비하는(가져오는) 친구입니다.

 

AWS 에서 제공하는 boto3 의 credential 순서는 

1. python 코드에 적용 된 credential

2. 로컬 내부 home( ~/  위치)에 존재하는 .aws폴더의 내용

입니다.

 

Python에서 Kinesis Datastream에 연동하여 데이터를 주고/받는 과정

Kinesis로 데이터를 전달할 때, byte혹은 bytearray로 전달 해야 함
(encode('utf-8')로 전달 해 주어야 함)

  1. Requirements

    • Install boto3, kinesis

        $ pip3 install -U pip
        $ python3 -m pip install boto3 kinesis
  2. Setting Credential and config

    아래 두개의 파일을 생성한다.

    credentials, config 두개의 파일을 ~/.aws에 위치시킨다

    ~/.aws/

    • credentials —> setting access_key, sercret_key

        [default]
        aws_access_key_id=
        aws_secret_access_key=
    • config —> setting region, type

        [default]
        region=region_info
        output=json
  3. Test setting

    • test consumer

        import boto3
        from kinesis.producer import KinesisProducer
        from kinesis.consumer import KinesisConsumer
      
        consumer = KinesisConsumer(stream_name=KINESIS_STREAM_NAME)
        for message in consumer:
            print("############")
            print(message)
            print("############")
    • test producer

        from kinesis.producer import KinesisProducer
      
        producer = KinesisProducer(stream_name='my-stream')
        producer.put('Hello World from Python')

       

    • 실행 순서

      test producer -> test consumer

REFERENCE

python-kinesis document (Link)
python-kinesis document github (Link)
aws boto3/kinesis configure documentation (Link)
python-kinesis & python-s3 example Blod(Link)
aws boto3 data stream document(Link)
boto3 - how to credential —> credential process (Link)