0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Lambdaで使うKinesis Data Streamテストイベントの作成

Posted at

ちょっとメモ。LambdaでKinesis Data Streamのテストイベントを作るときは、「テストイベントの設定」画面でイベントテンプレートに「Amazon Kinesis Data Stream」を選択する。イベントの内容に、それらしいJSONデータが設定される。

image.png

ここで data に収められているのは、デコードすると "Hello, this is a test 123." となる文字列なのだけど、実際にはテストをする上でここに任意のデータを入れておきたい。その時は、Pythonでは次のようにbase64変換した文字列を生成する。

  1. データをJSON文字列などに変換する(str型になる)。
  2. データをUTF-8に変換する(bytes型になる)。
  3. データをbase64エンコードする。

例えば、以下のようにする。

>>> print(type(data), data)
<class 'dict'> {'Timestamp': '2020-03-17T09:41:29.284244', 'Name': 'Akio Asada', 'Mail': 'asada@example.com'}
>>> data_str = json.dumps(data)
>>> data_bytes = data_str.encode('utf-8')
>>> data_base64 = base64.b64encode(data_bytes)
>>> print(type(data_base64), data_base64)
<class 'bytes'> b'eyJUaW1lc3RhbXAiOiAiMjAyMC0wMy0xN1QwOTo0MToyOS4yODQyNDQiLCAiTmFtZSI6ICJBa2lvIEFzYWRhIiwgIk1haWwiOiAiYXNhZGFAZXhhbXBsZS5jb20ifQ=='

まとめてやれば、こうなる。

>>> base64.b64encode(json.dumps(data).encode('utf-8'))
b'eyJUaW1lc3RhbXAiOiAiMjAyMC0wMy0xN1QwOTo0MToyOS4yODQyNDQiLCAiTmFtZSI6ICJBa2lvIEFzYWRhIiwgIk1haWwiOiAiYXNhZGFAZXhhbXBsZS5jb20ifQ=='

この eyJUa(略)ifQ== という文字列を、テストデータ作成画面の data の値に入れる。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?