-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
123 lines (93 loc) · 2.59 KB
/
main.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
import requests
import time
def call_ISS_API():
print("Calling ISS")
time.sleep(1)
r = requests.get('http://api.open-notify.org/iss-now.json')
json = r.json()
json['latitude'] = json['iss_position']['latitude']
json['longitude'] = json['iss_position']['longitude']
del json['iss_position']
del json['message']
return json
api_calls = {}
for i in range(5):
response = call_ISS_API()
api_calls[response['timestamp']] = response
#%%
import boto3
# replace the keys below
client = boto3.client(
'dynamodb',
aws_access_key_id='*****',
aws_secret_access_key='*****',
)
dynamodb = boto3.resource(
'dynamodb',
aws_access_key_id='*****',
aws_secret_access_key='*****',
)
ddb_exceptions = client.exceptions
#%%
try:
table = client.create_table(
TableName='ISS_locations',
KeySchema=[
{
'AttributeName': 'timestamp',
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{
'AttributeName': 'timestamp',
'AttributeType': 'N'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
print("Creating table")
waiter = client.get_waiter('table_exists')
waiter.wait(TableName='ISS_locations')
print("Table created")
except ddb_exceptions.ResourceInUseException:
print("Table exists")
#%%
print("Putting items")
for response in api_calls:
dynamodb.Table('ISS_locations').put_item(
Item=api_calls[response]
)
#%%
print("Scanning table")
response = dynamodb.Table('ISS_locations').scan()
for i in response['Items']:
print(i)
#%%
print("Query table")
from boto3.dynamodb.conditions import Key
k = api_calls[list(api_calls)[0]]['timestamp']
response = dynamodb.Table('ISS_locations').query(
KeyConditionExpression=Key('timestamp').eq(k)
)
for i in response['Items']:
print(i)
#%%
print("Remove item")
k = api_calls[list(api_calls)[1]]['timestamp']
response = dynamodb.Table('ISS_locations').delete_item(
Key={'timestamp':k}
)
print("Scanning table")
response = dynamodb.Table('ISS_locations').scan()
for i in response['Items']:
print(i)
#%%
print("Deleting Table")
client.delete_table(TableName='ISS_locations')
waiter = client.get_waiter('table_not_exists')
waiter.wait(TableName='ISS_locations')
print("Table deleted")