Amazon Kinesis編~KCL for Pythonを使ってみる02~

前回は『 Amazon Kinesis編~KCL for Pythonを使ってみる01~ 』と題して、Kinesis Client Library for Pythonを試してみました。

今回は『Amazon Kinesis編~KCL for Pythonを使ってみる02~』と題して、Kinesis Client Library for Pythonでレコード処理を試してみたいと思います。

sample_kclpy_app.py

サンプルのsamplekclpyapp.pyでは各種処理が実装されており、

データ処理部分を記述するだけで動作確認が可能です。

1.レコード処理の記述

samplekclpyapp.pyのprocess_record関数に処理を追記します。

今回は単純にログにデータを出力するようにしてみます。

def processrecord(self, data, partitionkey, sequence_number):

”’

Called for each record that is passed to process_records.

:type data: str

:param data: The blob of data that was contained in the record.

:type partition_key: str

:param partition_key: The key associated with this recod.

:type sequence_number: int

:param sequence_number: The sequence number associated with this record.

”’

with open(“./test.log”, “a”) as f:

f.write(data)

return

2.KCLの実行

sample.propertiesで該当のスクリプトを使用するに指定します。

1$ vim sample.properties2executableName = /home/ec2-user/amazon-kinesis-client-python/samples/sample_kclpy_app.py

実行権限を付与しておきます。

1$ chmod 755 sample_kclpy_app.py

KCLを実行しておきます。

1$ `amazon_kclpy_helper.py --print_command --java /usr/bin/java --properties /home/ec2-user/amazon-kinesis-client-python/sample.properties`

3.データのプット

samplekinesiswordputter.pyを利用してデータを入れてみます。

1$ sample_kinesis_wordputter.py --stream words -w cat -w dog -w bird -w lobster2Connecting to stream: words in us-east-13Put word: cat into stream: words4Put word: dog into stream: words5Put word: bird into stream: words6Put word: lobster into stream: words

3.確認

実際に処理されたかを確認してみます。

出力されているかをtailで確認してみます。

1$ tail test.log2catdogbirdlobster

簡単に使えましたね!

いかがでしたでしょうか?

次回もお楽しみに!!!

この記事を書いた人

aws-recipe-user