generated from sanket143/DynamoDB-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·233 lines (218 loc) · 4.91 KB
/
setup.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import boto3
import json
def batch_write(table_name, contents):
table = dynamodb.Table(table_name)
batch_start = 0
lcontent = len(contents)
while True:
if batch_start > lcontent:
break
batch_contents = contents[batch_start:batch_start + 25]
with table.batch_writer() as batch:
for item in batch_contents:
batch.put_item(Item=item)
batch_start += 25
# Add Users Data in DB
def add_users():
print("Adding Users...")
fd = open("data/devhub/users.json", "r")
fd_content = json.loads(fd.read())
batch_write("Users", fd_content)
fd.close()
# Add Repositories Data in DB
def add_repositories():
print("Adding Repositories...")
fd = open("data/devhub/repositories.json", "r")
fd_content = json.loads(fd.read())
batch_write("Repositories", fd_content)
fd.close()
# Add Commits Data in DB
def add_commits():
print("Adding Commits...")
fd = open("data/devhub/commits.json", "r")
fd_content = json.loads(fd.read())
batch_write("Commits", fd_content)
fd.close()
# Add Issues Data in DB
def add_issues():
print("Adding Issues...")
fd = open("data/devhub/issues.json", "r")
fd_content = json.loads(fd.read())
batch_write("Issues", fd_content)
fd.close()
# Creates 'Users' Table in DB
def create_users():
print("Creating 'Users' Table...")
dynamodb.create_table(
TableName='Users',
KeySchema=[
{
'AttributeName': 'username',
'KeyType': 'HASH'
},
{
'AttributeName': 'email',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'username',
'AttributeType': 'S'
},
{
'AttributeName': 'email',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
add_users()
# Creates 'Repositories' Table in DB
def create_repositories():
print("Creating 'Repositories' Table...")
dynamodb.create_table(
TableName='Repositories',
KeySchema=[
{
'AttributeName': 'name',
'KeyType': 'HASH'
},
{
'AttributeName': 'owner',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'name',
'AttributeType': 'S'
},
{
'AttributeName': 'owner',
'AttributeType': 'S'
},
{
'AttributeName': 'id',
'AttributeType': 'N'
}
],
GlobalSecondaryIndexes=[
{
'IndexName': 'id-index',
'KeySchema': [
{
'KeyType': 'HASH',
'AttributeName': 'id'
}
],
'Projection': {
'ProjectionType': 'ALL'
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
add_repositories()
# Creates 'Issues' Table in DB
def create_issues():
print("Creating 'Issues' Table...")
dynamodb.create_table(
TableName='Issues',
KeySchema=[
{
'AttributeName': 'title',
'KeyType': 'HASH'
},
{
'AttributeName': 'repo_id',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'title',
'AttributeType': 'S'
},
{
'AttributeName': 'repo_id',
'AttributeType': 'N'
},
{
'AttributeName': 'reporter',
'AttributeType': 'S'
}
],
LocalSecondaryIndexes=[
{
'IndexName': 'reporter-index',
'KeySchema': [
{
'KeyType': 'HASH',
'AttributeName': 'title'
},
{
'KeyType': 'RANGE',
'AttributeName': 'reporter'
}
],
'Projection': {
'ProjectionType': 'ALL'
}
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
add_issues()
# Creates 'Commits' Table in DB
def create_commits():
print("Creating 'Commits' Table...")
dynamodb.create_table(
TableName='Commits',
KeySchema=[
{
'AttributeName': 'project_id',
'KeyType': 'HASH'
},
{
'AttributeName': 'sha',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'project_id',
'AttributeType': 'N'
},
{
'AttributeName': 'sha',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
add_commits()
def init_setup():
create_users()
create_repositories()
create_issues()
create_commits()
if __name__ == "__main__":
dynamodb = boto3.resource('dynamodb', region_name='ap-south-1', endpoint_url="http://localhost:8000")
init_setup()