-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdynamodb.py
86 lines (78 loc) · 1.69 KB
/
dynamodb.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
import boto3
import time
epoch_time = epoch_time = int(time.time())
client = boto3.client('dynamodb')
#Create a DynamoDB table
response = client.create_table(
TableName='customers',
AttributeDefinitions=[
#setting username as primary key (unique key)
{
'AttributeName': 'username',
'AttributeType': 'S'
}
],
KeySchema=[
{
'AttributeName': 'username',
'KeyType': 'HASH'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
print("Creating table... " + str(response))
print("\nWaiting 15 seconds for the the table to be created...")
time.sleep(15)
#put item in the table
put_item = client.put_item(
TableName='customers',
Item={
'username':
{
'S':'john123'
},
'time_stamp':
{
'S': str(epoch_time)
},
'first_name':
{
'S':'John'
},
'last_name':
{
'S':'Gates'
},
},
)
print(put_item)
#get item from the table
get_item = client.get_item(
TableName='customers',
Key={
'username':
{
'S':'john123'
},
}
)
print("\nGetting the item..." + str(get_item))
#delete table
delete_item = client.delete_item(
TableName='customers',
Key={
'username':
{
'S':'john123'
}
}
)
print("\nDeleting the item..." + str(delete_item))
#delete table
delete_table = client.delete_table(
TableName='customers'
)
print("\nDeleting the table..." + str(delete_table))