/
Dynamo.py
67 lines (61 loc) · 2.01 KB
/
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
64
65
66
67
import numpy as np
import time,json,sys
import boto3
from boto3.dynamodb.conditions import Key,Attr
import aws
DYNAMO_TABLE_NAME = "mindWave"
class Dynamo:
def __init__(self):
dynamo = aws.getResource('dynamodb')
# First try and create the table
try:
table = dynamo.create_table(
TableName=DYNAMO_TABLE_NAME,
KeySchema=[
{
'AttributeName': 'userName',
'KeyType': 'HASH' # Partition Key
},
{
'AttributeName' : 'time',
'KeyType' : 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'userName',
'AttributeType': 'S'
},
{
'AttributeName': 'time',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
while table.table_status != 'ACTIVE':
table.reload()
print ('Table ' + DYNAMO_TABLE_NAME + ' has been created.')
except Exception as e:
# print e
table = dynamo.Table(DYNAMO_TABLE_NAME)
print ('Table ' + DYNAMO_TABLE_NAME + ' has been retrieved.')
self.table_dynamo = table
def dynamoAdd(self, userName, time, data, result):
self.table_dynamo.put_item(
Item = {
'userName' : userName,
'time' : time,
'data' : data,
'result': result
}
)
def dynamoQuery(self, userName):
response = self.table_dynamo.query(
KeyConditionExpression = Key('userName').eq(userName)
)
items = response['Items']
return items