-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdbfuns.py
More file actions
55 lines (45 loc) · 1.29 KB
/
dbfuns.py
File metadata and controls
55 lines (45 loc) · 1.29 KB
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
import boto3
with open('aws.creds') as f:
creds = f.read().strip().split(',')
session = boto3.Session(aws_access_key_id=creds[0], aws_secret_access_key=creds[1])
dynamodb = dynamodb = session.resource('dynamodb', region_name='us-east-1')
# ['address', 'username', 'tweet', 'timestamp', 'region']
def init_table():
table = dynamodb.create_table(
TableName='disasterdata',
KeySchema=[
{
'AttributeName': 'address',
'KeyType': 'HASH' #Partition key
},
],
AttributeDefinitions=[
{
'AttributeName': 'address',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
class DBWriter(object):
def __init__(self):
self.items = []
def add_item(self, address, username, fullname, tweet, timestamp, region):
item = {}
item['address'] = address
item['username'] = username
item['fullname'] = fullname
item['tweet'] = tweet
item['timestamp'] = timestamp
item['region'] = region
self.items.append(item)
def write_to_db(self):
table = dynamodb.Table('disasterdata')
with table.batch_writer() as batch:
for item in self.items:
batch.put_item(
item
)