forked from singer-io/tap-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboto3_spike.py
executable file
·292 lines (254 loc) · 9.66 KB
/
boto3_spike.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
import boto3
import json
import decimal
import pprint
import string
import random
import datetime
import singer
import os
def random_string_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
def clear_tables(dynamodb):
try:
table = dynamodb.Table('simple_table')
table.delete()
except:
pass
try:
table = dynamodb.Table('movies')
table.delete()
except:
pass
def create_simple_table(dynamodb):
print('\nWaiting until table is deleted')
table = dynamodb.Table('simple_table')
table.wait_until_not_exists()
print('\nCreating table: simple_table')
table = dynamodb.create_table(
TableName='simple_table',
KeySchema=[
{
'AttributeName': 'id',
'KeyType': 'HASH' #Partition key
},
{
'AttributeName': 'string_field',
'KeyType': 'RANGE' #Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'N'
},
{
'AttributeName': 'string_field',
'AttributeType': 'S'
},
{
'AttributeName': 'date_field',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
},
GlobalSecondaryIndexes=[
{
'IndexName': 'date_index',
'KeySchema': [
{
'AttributeName': 'date_field',
'KeyType': 'HASH'
},
],
'Projection': {
'ProjectionType': 'ALL'
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
}
],
StreamSpecification={
'StreamEnabled': True,
'StreamViewType': 'NEW_IMAGE'
}
)
print('Finished creating table: simple_table')
def populate_simple_table(dynamodb):
print('\nPopulating table: simple_table')
num_items = 50
table = dynamodb.Table('simple_table')
table.wait_until_exists()
start_datetime = datetime.datetime(2018, 1, 1, 0, 0, 0, 0,
tzinfo=datetime.timezone.utc)
for int_value in range(num_items):
item_dt = start_datetime + datetime.timedelta(days=(5*int_value))
table.put_item(
Item={
"id": int_value,
"string_field": random_string_generator(),
"date_field": singer.strftime(item_dt)
}
)
# wait for global secondary index to be backfilled
while True:
if not table.global_secondary_indexes or table.global_secondary_indexes[0]['IndexStatus'] != 'ACTIVE':
print('Waiting for index to backfill...')
time.sleep(5)
table.reload()
else:
break
print('Added {} items to table: simple_table'.format(num_items))
def query_simple_table(dynamodb):
table = dynamodb.Table('simple_table')
print('\nRetrieving items with date_field=1/1/2018')
response = table.query(
KeyConditionExpression=boto3.dynamodb.conditions.Key('id').eq(0))
for simple_item in response.get('Items', []):
print(' {}, {}, {} '.format(simple_item['id'],
simple_item['string_field'],
simple_item['date_field']))
def scan_simple_table(dynamodb):
table = dynamodb.Table('simple_table')
print('\nRetrieving items with date_field > 2018-03-01T00:00:00.000000Z')
response = table.scan()
for simple_item in response.get('Items', []):
print(' {}, {}, {} '.format(simple_item['id'],
simple_item['string_field'],
simple_item['date_field']))
def get_stream(dynamodb):
table = dynamodb.Table('simple_table')
stream_arn = table.latest_stream_arn
client = boto3.client('dynamodbstreams',
endpoint_url='http://localhost:8000',
region_name='us-east-1')
selected_tables = set(['simple_table'])
stream_list = client.list_streams()
for streamarn in (x['StreamArn'] for x in stream_list['Streams'] if x['TableName'] in selected_tables):
stream_info = client.describe_stream(StreamArn=streamarn)
for shard in stream_info['StreamDescription']['Shards']:
shard_iterator = client.get_shard_iterator(StreamArn = streamarn,
ShardId = shard['ShardId'],
ShardIteratorType = 'TRIM_HORIZON')
records = handle_shard_iterator(client, shard_iterator['ShardIterator'])
def get_latest_seq_number(dynamodb):
table = dynamodb.Table('simple_table')
stream_arn = table.latest_stream_arn
client = boto3.client('dynamodbstreams',
endpoint_url='http://localhost:8000',
region_name='us-east-1')
selected_tables = set(['simple_table'])
stream_list = client.list_streams()
for streamarn in (x['StreamArn'] for x in stream_list['Streams'] if x['TableName'] in selected_tables):
stream_info = client.describe_stream(StreamArn=streamarn)
last_shard = stream_info['StreamDescription']['Shards'][-1]
shard_iterator = client.get_shard_iterator(StreamArn = streamarn,
ShardId = last_shard['ShardId'],
ShardIteratorType = 'TRIM_HORIZON')
records = handle_shard_iterator(client, shard_iterator['ShardIterator'])
import ipdb; ipdb.set_trace()
1+1
def handle_shard_iterator(client, shard_iterator):
records = client.get_records(ShardIterator=shard_iterator)
record_values = [x['dynamodb'] for x in records['Records']]
if len(records['Records']) == 1000 and records.get('NextShardIterator'):
record_values += handle_shard_iterator(client, records.get('NextShardIterator'))
return record_values
def create_movies(dynamodb):
print('\nCreating table: movies')
table = dynamodb.create_table(
TableName='movies',
KeySchema=[
{
'AttributeName': 'year',
'KeyType': 'HASH' #Partition key
},
{
'AttributeName': 'title',
'KeyType': 'RANGE' #Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'year',
'AttributeType': 'N'
},
{
'AttributeName': 'title',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
)
print('Finished creating table: movies')
def populate_movies(dynamodb):
print('\nPopulating table: movies')
table = dynamodb.Table('movies')
movie_count = 0
with open("moviedata.json") as json_file:
movies = json.load(json_file, parse_float = decimal.Decimal)
for movie in movies:
movie_count += 1
year = int(movie['year'])
title = movie['title']
info = movie['info']
table.put_item(
Item={
'year': year,
'title': title,
'info': info,
}
)
print('Added {} items to table: movies'.format(movie_count))
def query_movies(dynamodb):
table = dynamodb.Table('movies')
print('\nRetrieving Moby Dick from table...')
response = table.get_item(Key={'year': 1956, 'title': 'Moby Dick'})
if response.get('Item'):
pp = pprint.PrettyPrinter(depth=3)
pp.pprint(response['Item'])
print('\nRetrieving movies made in 1985')
response = table.query(KeyConditionExpression=boto3.dynamodb.conditions.Key('year').eq(1985))
for mov in response.get('Items', []):
print(' {}, {} '.format(mov['year'],
mov['title']))
def scan_movies(dynamodb):
table = dynamodb.Table('movies')
print('\nRetrieving movies made after 2015')
response = table.scan(FilterExpression=boto3.dynamodb.conditions.Attr('year').gt(2015))
for mov in response.get('Items', []):
print(' {}, {} '.format(mov['year'],
mov['title']))
def main():
dynamodb = boto3.resource('dynamodb',
endpoint_url='http://localhost:8000',
region_name='us-east-1')
#clear_tables(dynamodb)
#create_simple_table(dynamodb)
populate_simple_table(dynamodb)
populate_simple_table(dynamodb)
populate_simple_table(dynamodb)
populate_simple_table(dynamodb)
#get_stream(dynamodb)
get_latest_seq_number(dynamodb)
#query_simple_table(dynamodb)
# for i in range(100):
# scan_simple_table(dynamodb)
# if os.path.exists('moviedata.json'):
# create_movies(dynamodb)
# populate_movies(dynamodb)
# query_movies(dynamodb)
# scan_movies(dynamodb)
# else:
# print('\nError: did not find moviedata.json file to load, will not create movies table. Can download moviedata.json at https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/moviedata.zip')
if __name__ == "__main__":
main()