$30 off During Our Annual Pro Sale. View Details »

Amazon Timestreamでデータ補間/2021-12-27-llt24

Amazon Timestreamでデータ補間/2021-12-27-llt24

社内のLTイベント「えるLT Vol.24 オンライン」で発表した資料です

Satoshi SAKAO

December 27, 2021
Tweet

More Decks by Satoshi SAKAO

Other Decks in Technology

Transcript

  1. 話すひと 2 🏢 インフォコム株式会社 品質マネジメント推進室 👨🔧 ソフトウェアエンジニア 🛠 Node.js /

    AWS / IoT / iOS (Swift) 💖 猫,テクテクライフ(ランク: 23) Satoshi SAKAO @ottijp
  2. 3

  3. 経緯 • 自宅IoT作る • 環境可視化 • 防犯 • 猫さん見守り •

    目標: クラウド費用を月500円以下にする 4
  4. アーキテクチャ • ストレージ • メモリストア: 高スループット • マグネティックストア: 高コスト効率 •

    テーブルごとに保持期間を設定する • クエリ • 時系列系のSQL拡張(データタイプと関数) • Scheduled Queryによる定期実行クエリ 7
  5. 書き込み $ current=$(date +%s) $ aws timestream-write write-records \ --database-name

    homeiot \ --table-name measured \ --common-attributes '{"Dimensions":[{"Name":"place", "Value":"room1"}], "MeasureName":"temperature", "MeasureValueType":"DOUBLE"}' \ --records "[{\"Time\":\"$(($current - 305))\", \"TimeUnit\":\"SECONDS\", \"MeasureValue\":\"21.5\"}, \ {\"Time\":\"$(($current - 199))\", \"TimeUnit\":\"SECONDS\", \"MeasureValue\":\"23.2\"}, \ {\"Time\":\"$(($current - 150))\", \"TimeUnit\":\"SECONDS\", \"MeasureValue\":\"22.8\"}, \ {\"Time\":\"$(($current - 133))\", \"TimeUnit\":\"SECONDS\", \"MeasureValue\":\"22.6\"}, \ {\"Time\":\"$(($current - 61))\", \"TimeUnit\":\"SECONDS\", \"MeasureValue\":\"23.5\"}, \ {\"Time\":\"$(($current - 33))\", \"TimeUnit\":\"SECONDS\", \"MeasureValue\":\"23.8\"}, \ {\"Time\":\"$(($current - 15))\", \"TimeUnit\":\"SECONDS\", \"MeasureValue\":\"24.5\"}, \ {\"Time\":\"$(($current - 5))\", \"TimeUnit\":\"SECONDS\", \"MeasureValue\":\"24.3\"}]" \ --region us-west-2 11
  6. 60秒binの作成と補間値の作成 $ aws timestream-query query \ --query-string "WITH binned_timeseries AS

    (SELECT BIN(time, 60s) AS binned_timestamp, ROUND(AVG(measure_value::double), 1) AS avg_temperature \ FROM homeiot.measured \ WHERE measure_name = 'temperature' AND time > from_iso8601_timestamp('$(date -u -r $(($current - 400)) +%FT%TZ)') \ GROUP BY place, BIN(time, 60s) \ ) \ SELECT INTERPOLATE_LINEAR( \ CREATE_TIME_SERIES(binned_timestamp, avg_temperature), \ SEQUENCE(min(binned_timestamp), max(binned_timestamp), 60s)) AS interpolated_temperature \ FROM binned_timeseries" \ --region us-west-2 12
  7. refs • Amazon Timestreamの料金計算方法 | ottijp blog • https://blog.ottijp.com/2021/12/04/timestream-pricing/ •

    What Is Amazon Timestream? - Amazon Timestream • https://docs.aws.amazon.com/timestream/latest/developerguide/what-is- timestream.html • コマンドラインオプション - AWS Command Line Interface • https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-options.html 15