-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
62 lines (49 loc) · 1.56 KB
/
database.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
import boto3
dynamodb = boto3.resource('dynamodb', endpoint_url = "http://localhost:4566")
def show_all_table(table):
scanResponse = table.scan(TableName='tracking_id')
items = scanResponse['Items']
print('Scan all items in datatable: ')
for item in items:
print(item)
def create_table(name, key):
table = dynamodb.create_table(
TableName=name,
KeySchema=[
{
'AttributeName': key,
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
table.meta.client.get_waiter('table_exists').wait(TableName=name)
print("Done!")
def delete_table(table):
table.delete()
if __name__ == "__main__":
#table = dynamodb.Table('tracking_data')
create_table("Popo.user", "id")
user = {"id": 1, "username": "Dan", "password": "DanD"}
table1 = dynamodb.Table('Popo.user')
table1.put_item(Item=user)
create_table("Popo.books", "id")
books = [[1,"Cha giàu, cha nghèo","Kinh tế"],
[2,"Từ tốt đến vĩ đại","Kinh tế"],
[3,"Chí Phèo","Văn học"],
[4,"Bình Ngô đại cáo","Lịch sử"]]
table2 = dynamodb.Table('Popo.books')
for book in books:
data = {"id": book[0], "name": book[1], "category": book[2]}
table2.put_item(Item=data)
#show_all_table(table)
#delete_table(table)