-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB_setup.py
More file actions
146 lines (114 loc) · 4.11 KB
/
DB_setup.py
File metadata and controls
146 lines (114 loc) · 4.11 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import boto3
from decimal import *
from time import sleep
from helpers.configManager import cred_get
# Set up dinamodb client with boto3
client = boto3.client('dynamodb', region_name='eu-central-1',
aws_access_key_id=cred_get("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=cred_get("AWS_SECRET_ACCESS_KEY"))
#### Create Landmarks table #######
try:
resp = client.create_table(
TableName="Landmarks",
#Primary key
KeySchema=[
{
"AttributeName": "Name",
"KeyType": "HASH"
}
],
# Primary key type
AttributeDefinitions=[
{
"AttributeName": "Name",
"AttributeType": "S"
}
],
# Rate of access
ProvisionedThroughput={
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
}
)
print("Landmarks table created successfully!")
except Exception as e:
print("Error creating table:")
print(e)
######################################
print("Waiting for landmarks table to be ready...")
sleep(5)
#### Insert landmarks in table #######
#Set up decimal precision
getcontext().prec = 6
#Set up boto3
dynamodb = boto3.resource('dynamodb', region_name='eu-central-1',
aws_access_key_id=cred_get("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=cred_get("AWS_SECRET_ACCESS_KEY"))
table = dynamodb.Table('Landmarks')
#Recover landmarks
fd = open("landmarksComplete.csv", "r")
lines = fd.readlines()
# Write multiple items with one requests with batch writer
with table.batch_writer() as batch:
for i in range(1, len(lines)):
splitted = lines[i].strip().split(", ")
batch.put_item(Item={"Name": splitted[0], "ID": int(i-1), "Lat": Decimal(splitted[1]), "Long": Decimal(splitted[2]), "Fog1": Decimal(splitted[3]), "Fog2" : Decimal(splitted[4]), "PictureUrl" : splitted[5], "Description": "".join(splitted[6:])})
#print({"Name": splitted[0], "ID": int(i), "Lat": Decimal(splitted[1]), "Long": Decimal(splitted[2]) })
print("Landamrks are online!")
######################################
# Set up dinamodb client with boto3
client = boto3.client('dynamodb', region_name='eu-central-1',
aws_access_key_id=cred_get("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=cred_get("AWS_SECRET_ACCESS_KEY"))
#### Create Distances table #######
try:
resp = client.create_table(
TableName="Distances",
#Primary key
KeySchema=[
{
"AttributeName": "ID",
"KeyType": "HASH"
}
],
# Primary key type
AttributeDefinitions=[
{
"AttributeName": "ID",
"AttributeType": "N"
}
],
# Rate of access
ProvisionedThroughput={
"ReadCapacityUnits": 30,
"WriteCapacityUnits": 30
}
)
print("Disntaces table created successfully!")
except Exception as e:
print("Error creating table:")
print(e)
######################################
print("Waiting for distances table to be ready...")
sleep(5)
#### Insert distances in table #######
#Set up decimal precision
getcontext().prec = 6
#Set up boto3
dynamodb = boto3.resource('dynamodb', region_name='eu-central-1',
aws_access_key_id=cred_get("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=cred_get("AWS_SECRET_ACCESS_KEY"))
table = dynamodb.Table('Distances')
#Recover landmarks
fd = open("distancesDB.csv", "r")
lines = fd.readlines()
# Write multiple items with one requests with batch writer
with table.batch_writer() as batch:
for i in range(1, len(lines)):
splitted = lines[i].strip().split(", ")
batch.put_item(Item={"ID": i, "Start": splitted[0], "End": splitted[1], "Seconds": int(splitted[2]), "Transport": splitted[3], "Fog1": Decimal(splitted[4]), "Fog2" : Decimal(splitted[5])})
#print({"Name": splitted[0], "ID": int(i), "Lat": Decimal(splitted[1]), "Long": Decimal(splitted[2]) })
if (i%100 == 0):
print(i, " items inserted!")
print("Distances are online!")
######################################