-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
83 lines (62 loc) · 2.14 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
import json
from couchbase.admin import Admin
from couchbase.exceptions import HTTPError
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
from couchbase.analytics import AnalyticsQuery
# The terraform script will out put the IP address
ip_f = open('terraform/ip_address.txt')
ip = str(ip_f.read()).strip('\n')
print('Connecting to Couchbase instance at %s' % (ip))
adm = Admin('Administrator', 'password', host=ip, port=8091)
try:
adm.bucket_create('commerce')
except HTTPError as err:
v = err.__dict__['all_results'][None]
if 'already exists' in v.value['errors']['name']:
print('Bucket already exists, not creating.')
adm.wait_ready('commerce', timeout=10)
# connect w/ SDK
cluster = Cluster('couchbase://' + ip)
auth = PasswordAuthenticator('Administrator', 'password')
cluster.authenticate(auth)
cb = cluster.open_bucket('commerce')
# create anlytics indexes
queries = [
'CREATE DATASET ships ON `commerce` WHERE `type` = "ship"',
'CREATE DATASET voyages ON `commerce` WHERE `type` = "voyage"',
'CREATE DATASET customers ON `commerce` WHERE `type` = "customer"',
'CREATE DATASET reservations ON `commerce` WHERE `type` = "reservation"',
'CREATE DATASET credits ON `commerce` WHERE `type` = "credit"',
'CONNECT LINK Local'
]
for q in queries:
try:
resp = cb.analytics_query(AnalyticsQuery(q), ip)
for row in resp:
print(row)
except Exception as err:
print(err)
'''
# Load data
# credit.json
credit_j = json.loads(open('data/credit.json').read())
for doc in credit_j:
pk = doc['commerce']['metaData']['simpleKey']
cb.upsert(pk, doc)
# creditUsage.json
docs = json.loads(open('data/creditUsage.json').read())
for doc in docs:
pk = str(doc['commerce']['passengerId'])
cb.upsert(pk, doc)
# customer.json
docs = json.loads(open('data/customer.json').read())
for doc in docs:
pk = doc['commerce']['metaData']['simpleKey']
cb.upsert(pk, doc)
# reservation.json
docs = json.loads(open('data/reservation.json').read())
for doc in docs:
pk = doc['commerce']['metaData']['simpleKey']
cb.upsert(pk, doc)
'''