-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataimport.py
executable file
·56 lines (48 loc) · 1.72 KB
/
dataimport.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
#!/usr/bin/env python
"""
Imports data from CSV URL to DynamoDB table
"""
import boto3
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key, Attr
from urllib import urlopen
import csv
import sys
dataurl = 'https://fred.stlouisfed.org/graph/fredgraph.csv?id=DCOILWTICO&fgsnd=2009-06-01&fq=Daily'
ddbtable = 'FREDdata'
def get_table(name):
dynamodb = boto3.resource('dynamodb')
# use an existing table or create one
try:
table = dynamodb.Table(name)
status = table.table_status
except ClientError as err:
if err.response['Error']['Code'] == 'ResourceNotFoundException':
print "Creating table"
table = dynamodb.create_table(
TableName=name,
KeySchema=[
{'AttributeName': 'date', 'KeyType': 'HASH'},
{'AttributeName': 'DCOILWTICO', 'KeyType': 'RANGE'},
],
AttributeDefinitions=[
{'AttributeName': 'date', 'AttributeType': 'S'},
{'AttributeName': 'DCOILWTICO', 'AttributeType': 'S'},
],
ProvisionedThroughput={'ReadCapacityUnits': 5, 'WriteCapacityUnits': 100}
)
table.meta.client.get_waiter('table_exists').wait(TableName=name)
return table
def main():
table = get_table(ddbtable)
csvfile = urlopen(dataurl)
data = csv.reader(csvfile, delimiter=',', skipinitialspace=True)
with table.batch_writer() as batch:
for row in data:
batch.put_item(Item={'date': row[0], 'DCOILWTICO': row[1],})
print "Done"
return 0
def lambda_handler(event, context):
return main()
if __name__ == '__main__':
sys.exit(main())