This repository has been archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathstudentcourseregistration-withindex.py
84 lines (74 loc) · 3.09 KB
/
studentcourseregistration-withindex.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
# Find students within a country - using a GSI index
# A Cloud Guru - Dynamo DB Course\
# USES V3 DATA MODEL
import boto3, botocore, tqdm, random, time
from time import gmtime, strftime
from boto3.dynamodb.conditions import Attr
from botocore.exceptions import ClientError
from sys import version_info
py3 = version_info[0] > 2 #creates boolean value for test that Python major version > 2
lesson = '4-014' ## UDPATE ME
loadmin_session = boto3.Session(profile_name='loadmin')
db_c = loadmin_session.client('dynamodb')
db_r = loadmin_session.resource('dynamodb')
## PREREQS - images/ folder in same directory as this scripts
## configured AWS tools
## installed python2.7+
## installed boto3 (pip install boto3)
## Installed faker module
## Installed tqdm module
## 'loadmin' AWS configuration profile - with admin rights
## Data Model v2 loaded.
## _retry.json file from lesson files - adjusted for 1mil+ auto retries
## for retryable operations
if __name__ == "__main__":
student_id=""
capacityconsumed = 0
scannedcount = 0
while True:
if py3:
student_id = str(input('Enter a studentid: '))
else:
student_id = str(raw_input('Enter a studentid: '))
if student_id !="":
break
## END WHILE LOOP
starttime = time.time()
response=db_r.Table("lo_courseregistration").query(\
ReturnConsumedCapacity='TOTAL', \
ExpressionAttributeValues={':studentid' : student_id}, \
KeyConditionExpression="s_id = :studentid", \
IndexName='studentcourses' \
)
capacityconsumed = response['ConsumedCapacity']['CapacityUnits']
data=response['Items']
while 'LastEvaluatedKey' in response:
#print "hi"
response=db_r.Table("lo_students").query(\
ReturnConsumedCapacity='TOTAL', \
ExpressionAttributeValues={':studentid' : student_id}, \
ExclusiveStartKey=response['LastEvaluatedKey'], \
KeyConditionExpression="s_id = :studentid", \
IndexName='studentcourses' \
)
#print response
capacityconsumed = capacityconsumed + response['ConsumedCapacity']['CapacityUnits']
data.extend(response['Items'])
finishtime=time.time()
timetocomplete = finishtime - starttime
print "Script Started on %s, finished on %s, took %0.2fs to complete without indexes" % (time.ctime(starttime),\
time.ctime(finishtime), timetocomplete)
print "Operation required %.1f capacity units to complete" % capacityconsumed
print "Total number of items returned [%i]" % len(data)
print "Located [%i] courses for student [%s]" % (len(data), student_id)
print "Press Q to quit... or any other key to list courses for student in %s" % student_id
if py3:
entry = str(input()).lower()
else:
entry = str(raw_input()).lower()
if entry == 'q':
exit(0)
print "#\tS_ID\tCourse Instance"
for i, s_instance in enumerate(data):
print "%d\t%s\t%s" % (i+1, \
s_instance['s_id'], s_instance['ci_id'])