-
Notifications
You must be signed in to change notification settings - Fork 0
/
courses.py
67 lines (55 loc) · 1.63 KB
/
courses.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
# Filename: courses.py
# Author: Shawn R Moses
# Date: July 9, 2018
# Current Version: 1.4
# Description: Import the JSON file course information to AWS Dynamodb table "info".
import boto3
import json
dynamodb = boto3.resource('dynamodb')
client = boto3.client('dynamodb')
# Creates a new table
def __createTable():
print('Creating table...')
table = dynamodb.create_table(
TableName='courses',
KeySchema=[
{
'AttributeName': 'subj',
'KeyType': 'HASH'
},
{
'AttributeName': 'classNum',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'classNum',
'AttributeType': 'N'
},
{
'AttributeName': 'subj',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
)
table.meta.client.get_waiter('table_exists').wait(TableName='courses')
print('Table created!')
# Adds courses from JSON
def update():
# If the table does not exist, create it
if 'courses' not in client.list_tables()['TableNames']:
__createTable()
table = dynamodb.Table('courses')
with open('courses.json', 'r') as file:
data = json.load(file)
# Write courses to DynamoDB
with table.batch_writer() as batch:
for course in data['courses']:
print('Adding %s %s' % (course['subj'], course['num']))
batch.put_item(Item=course)
update()