11{
22 "cells" : [
33 {
4+ "attachments" : {},
5+ "cell_type" : " markdown" ,
6+ "metadata" : {},
47 "source" : [
58 " [](https://github.com/aws/aws-sdk-pandas)\n " ,
69 " \n " ,
710 " # 26 - Amazon Timestream"
8- ],
9- "cell_type" : " markdown" ,
10- "metadata" : {
11- "pycharm" : {
12- "name" : " #%% md\n "
13- }
14- }
11+ ]
1512 },
1613 {
14+ "attachments" : {},
15+ "cell_type" : " markdown" ,
16+ "metadata" : {},
1717 "source" : [
1818 " ## Creating resources"
19- ],
20- "cell_type" : " markdown" ,
21- "metadata" : {
22- "pycharm" : {
23- "name" : " #%% md\n "
24- }
25- }
19+ ]
2620 },
2721 {
2822 "cell_type" : " code" ,
2923 "execution_count" : 10 ,
30- "metadata" : {
31- "pycharm" : {
32- "name" : " #%%\n "
33- }
34- },
24+ "metadata" : {},
3525 "outputs" : [],
3626 "source" : [
3727 " import awswrangler as wr\n " ,
3828 " import pandas as pd\n " ,
3929 " from datetime import datetime\n " ,
4030 " \n " ,
41- " wr.timestream.create_database(\" sampleDB\" )\n " ,
42- " wr.timestream.create_table(\" sampleDB\" , \" sampleTable\" , memory_retention_hours=1, magnetic_retention_days=1)"
31+ " database = \" sampleDB\"\n " ,
32+ " table_1 = \" sampleTable1\"\n " ,
33+ " table_2 = \" sampleTable2\"\n " ,
34+ " wr.timestream.create_database(database)\n " ,
35+ " wr.timestream.create_table(database, table_1, memory_retention_hours=1, magnetic_retention_days=1)\n " ,
36+ " wr.timestream.create_table(database, table_2, memory_retention_hours=1, magnetic_retention_days=1)"
4337 ]
4438 },
4539 {
40+ "attachments" : {},
4641 "cell_type" : " markdown" ,
47- "metadata" : {
48- "pycharm" : {
49- "name" : " #%% md\n "
50- }
51- },
42+ "metadata" : {},
5243 "source" : [
53- " ## Write"
44+ " ## Write\n " ,
45+ " \n " ,
46+ " ### Single measure WriteRecord"
5447 ]
5548 },
5649 {
5750 "cell_type" : " code" ,
5851 "execution_count" : 11 ,
59- "metadata" : {
60- "pycharm" : {
61- "name" : " #%%\n "
62- }
63- },
52+ "metadata" : {},
6453 "outputs" : [
6554 {
66- "output_type" : " stream" ,
6755 "name" : " stdout" ,
56+ "output_type" : " stream" ,
6857 "text" : [
6958 " Number of rejected records: 0\n "
7059 ]
7362 "source" : [
7463 " df = pd.DataFrame(\n " ,
7564 " {\n " ,
76- " \" time\" : [datetime.now(), datetime.now(), datetime.now()] ,\n " ,
65+ " \" time\" : [datetime.now()] * 3 ,\n " ,
7766 " \" dim0\" : [\" foo\" , \" boo\" , \" bar\" ],\n " ,
7867 " \" dim1\" : [1, 2, 3],\n " ,
7968 " \" measure\" : [1.0, 1.1, 1.2],\n " ,
8271 " \n " ,
8372 " rejected_records = wr.timestream.write(\n " ,
8473 " df=df,\n " ,
85- " database=\" sampleDB \" ,\n " ,
86- " table=\" sampleTable \" ,\n " ,
74+ " database=database ,\n " ,
75+ " table=table_1 ,\n " ,
8776 " time_col=\" time\" ,\n " ,
8877 " measure_col=\" measure\" ,\n " ,
8978 " dimensions_cols=[\" dim0\" , \" dim1\" ],\n " ,
9382 ]
9483 },
9584 {
85+ "attachments" : {},
9686 "cell_type" : " markdown" ,
9787 "metadata" : {
98- "pycharm" : {
99- "name" : " #%% md\n "
100- }
88+ "collapsed" : false
10189 },
10290 "source" : [
103- " ## Query "
91+ " ### Multi measure WriteRecord "
10492 ]
10593 },
10694 {
10795 "cell_type" : " code" ,
108- "execution_count" : 12 ,
96+ "execution_count" : null ,
10997 "metadata" : {
110- "pycharm" : {
111- "name" : " #%%\n "
112- }
98+ "collapsed" : false
11399 },
100+ "outputs" : [],
101+ "source" : [
102+ " df = pd.DataFrame(\n " ,
103+ " {\n " ,
104+ " \" time\" : [datetime.now()] * 3,\n " ,
105+ " \" measure_1\" : [\" 10\" , \" 20\" , \" 30\" ],\n " ,
106+ " \" measure_2\" : [\" 100\" , \" 200\" , \" 300\" ],\n " ,
107+ " \" measure_3\" : [\" 1000\" , \" 2000\" , \" 3000\" ],\n " ,
108+ " \" tag\" : [\" tag123\" , \" tag456\" , \" tag789\" ],\n " ,
109+ " }\n " ,
110+ " )\n " ,
111+ " rejected_records = wr.timestream.write(\n " ,
112+ " df=df,\n " ,
113+ " database=database,\n " ,
114+ " table=table_2,\n " ,
115+ " time_col=\" time\" ,\n " ,
116+ " measure_col=[\" measure_1\" , \" measure_2\" , \" measure_3\" ],\n " ,
117+ " dimensions_cols=[\" tag\" ]\n " ,
118+ " )\n " ,
119+ " \n " ,
120+ " print(f\" Number of rejected records: {len(rejected_records)}\" )"
121+ ]
122+ },
123+ {
124+ "attachments" : {},
125+ "cell_type" : " markdown" ,
126+ "metadata" : {},
127+ "source" : [
128+ " ## Query"
129+ ]
130+ },
131+ {
132+ "cell_type" : " code" ,
133+ "execution_count" : 12 ,
134+ "metadata" : {},
114135 "outputs" : [
115136 {
116- "output_type" : " execute_result" ,
117137 "data" : {
138+ "text/html" : [
139+ " <div>\n " ,
140+ " <style scoped>\n " ,
141+ " .dataframe tbody tr th:only-of-type {\n " ,
142+ " vertical-align: middle;\n " ,
143+ " }\n " ,
144+ " \n " ,
145+ " .dataframe tbody tr th {\n " ,
146+ " vertical-align: top;\n " ,
147+ " }\n " ,
148+ " \n " ,
149+ " .dataframe thead th {\n " ,
150+ " text-align: right;\n " ,
151+ " }\n " ,
152+ " </style>\n " ,
153+ " <table border=\" 1\" class=\" dataframe\" >\n " ,
154+ " <thead>\n " ,
155+ " <tr style=\" text-align: right;\" >\n " ,
156+ " <th></th>\n " ,
157+ " <th>time</th>\n " ,
158+ " <th>measure_value::double</th>\n " ,
159+ " <th>dim0</th>\n " ,
160+ " <th>dim1</th>\n " ,
161+ " </tr>\n " ,
162+ " </thead>\n " ,
163+ " <tbody>\n " ,
164+ " <tr>\n " ,
165+ " <th>0</th>\n " ,
166+ " <td>2020-12-08 19:15:32.468</td>\n " ,
167+ " <td>1.0</td>\n " ,
168+ " <td>foo</td>\n " ,
169+ " <td>1</td>\n " ,
170+ " </tr>\n " ,
171+ " <tr>\n " ,
172+ " <th>1</th>\n " ,
173+ " <td>2020-12-08 19:15:32.468</td>\n " ,
174+ " <td>1.2</td>\n " ,
175+ " <td>bar</td>\n " ,
176+ " <td>3</td>\n " ,
177+ " </tr>\n " ,
178+ " <tr>\n " ,
179+ " <th>2</th>\n " ,
180+ " <td>2020-12-08 19:15:32.468</td>\n " ,
181+ " <td>1.1</td>\n " ,
182+ " <td>boo</td>\n " ,
183+ " <td>2</td>\n " ,
184+ " </tr>\n " ,
185+ " </tbody>\n " ,
186+ " </table>\n " ,
187+ " </div>"
188+ ],
118189 "text/plain" : [
119190 " time measure_value::double dim0 dim1\n " ,
120191 " 0 2020-12-08 19:15:32.468 1.0 foo 1\n " ,
121192 " 1 2020-12-08 19:15:32.468 1.2 bar 3\n " ,
122193 " 2 2020-12-08 19:15:32.468 1.1 boo 2"
123- ],
124- "text/html" : " <div>\n <style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n </style>\n <table border=\" 1\" class=\" dataframe\" >\n <thead>\n <tr style=\" text-align: right;\" >\n <th></th>\n <th>time</th>\n <th>measure_value::double</th>\n <th>dim0</th>\n <th>dim1</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>2020-12-08 19:15:32.468</td>\n <td>1.0</td>\n <td>foo</td>\n <td>1</td>\n </tr>\n <tr>\n <th>1</th>\n <td>2020-12-08 19:15:32.468</td>\n <td>1.2</td>\n <td>bar</td>\n <td>3</td>\n </tr>\n <tr>\n <th>2</th>\n <td>2020-12-08 19:15:32.468</td>\n <td>1.1</td>\n <td>boo</td>\n <td>2</td>\n </tr>\n </tbody>\n </table>\n </div>"
194+ ]
125195 },
196+ "execution_count" : 12 ,
126197 "metadata" : {},
127- "execution_count " : 12
198+ "output_type " : " execute_result "
128199 }
129200 ],
130201 "source" : [
131202 " wr.timestream.query(\n " ,
132- " 'SELECT time, measure_value::double, dim0, dim1 FROM \" sampleDB \" .\" sampleTable \" ORDER BY time DESC LIMIT 3'\n " ,
203+ " f 'SELECT time, measure_value::double, dim0, dim1 FROM \" {database} \" .\" {table_1} \" ORDER BY time DESC LIMIT 3'\n " ,
133204 " )"
134205 ]
135206 },
136207 {
208+ "attachments" : {},
137209 "cell_type" : " markdown" ,
210+ "metadata" : {
211+ "collapsed" : false
212+ },
138213 "source" : [
139214 " ## Unload"
140- ],
141- "metadata" : {
142- "collapsed" : false ,
143- "pycharm" : {
144- "name" : " #%% md\n "
145- }
146- }
215+ ]
147216 },
148217 {
149218 "cell_type" : " code" ,
150219 "execution_count" : null ,
220+ "metadata" : {
221+ "collapsed" : false
222+ },
151223 "outputs" : [],
152224 "source" : [
153225 " df = wr.timestream.unload(\n " ,
154- " sql='SELECT time, measure_value, dim0, dim1 FROM \" sampleDB \" .\" sampleTable \" ',\n " ,
226+ " sql=f 'SELECT time, measure_value, dim0, dim1 FROM \" {database} \" .\" {table_1} \" ',\n " ,
155227 " path=\" s3://bucket/extracted_parquet_files/\" ,\n " ,
156228 " partition_cols=[\" dim1\" ],\n " ,
157229 " )"
158- ],
159- "metadata" : {
160- "collapsed" : false ,
161- "pycharm" : {
162- "name" : " #%%\n "
163- }
164- }
230+ ]
165231 },
166232 {
233+ "attachments" : {},
234+ "cell_type" : " markdown" ,
235+ "metadata" : {},
167236 "source" : [
168237 " ## Deleting resources"
169- ],
170- "cell_type" : " markdown" ,
171- "metadata" : {
172- "pycharm" : {
173- "name" : " #%% md\n "
174- }
175- }
238+ ]
176239 },
177240 {
178241 "cell_type" : " code" ,
179242 "execution_count" : 13 ,
180- "metadata" : {
181- "pycharm" : {
182- "name" : " #%%\n "
183- }
184- },
243+ "metadata" : {},
185244 "outputs" : [],
186245 "source" : [
187- " wr.timestream.delete_table(\" sampleDB\" , \" sampleTable\" )\n " ,
188- " wr.timestream.delete_database(\" sampleDB\" )"
246+ " wr.timestream.delete_table(database, table_1)\n " ,
247+ " wr.timestream.delete_table(database, table_2)\n " ,
248+ " wr.timestream.delete_database(database)"
189249 ]
190250 }
191251 ],
219279 },
220280 "nbformat" : 4 ,
221281 "nbformat_minor" : 4
222- }
282+ }
0 commit comments