-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_data_to_dynamo.py
63 lines (53 loc) · 1.49 KB
/
upload_data_to_dynamo.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
import boto3
import json
import os
import time
from botocore.exceptions import ClientError
dynamo_client = boto3.resource(service_name='dynamodb', region_name='us-east-1')
table_name='Student_data'
# delete table if it exists
try:
student_data_table = dynamo_client.Table(table_name)
student_data_table.delete()
print(f"Deleting {student_data_table.name}...")
student_data_table.wait_until_not_exists()
except ClientError as ce:
print(ce.response['Error'])
# Create the table
try:
dynamo_client.create_table(
TableName=table_name,
AttributeDefinitions=[
{
'AttributeName': 'name',
'AttributeType': 'S'
},
],
TableClass='STANDARD',
KeySchema=[
{
'AttributeName': 'name',
'KeyType': 'HASH'
},
],
BillingMode='PROVISIONED',
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
},
)
print(f'Table {table_name} successfully created')
except ClientError as ce:
print(ce.response['Error'])
exit(-1)
student_data_table = dynamo_client.Table(table_name)
student_data_table.wait_until_exists()
if os.path.exists('./student_data.json'):
data = json.load(open('./student_data.json'))
else:
print('File student.json does not exist')
exit(-1)
# Upload data to table
for d in data:
print(f'Adding {d}')
student_data_table.put_item(Item=d)