-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB_Init.py
executable file
·227 lines (212 loc) · 7 KB
/
DB_Init.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
#-*- coding: utf-8 -*-
#!/usr/bin/python
#----------------------------------------------------------------------------
# Name: DB_Init.py
# Purpose: Initialize the Database and all required collections for AE_ETL
# Author: Albert1_Wu
# Version: 2.00a
# Created: 2015.04.20
# Licence: Pegartoncorp Inc. license
#----------------------------------------------------------------------------
import sys
import time
import copy
import datetime
import happybase
import collections
import AEETLClass as ae
from pymongo import MongoClient
from pymongo import Connection
from pymongo import ASCENDING, DESCENDING
from copy import deepcopy
from bson.code import Code
from parameter import (
mongoIP, mongoPort, hbaseIP, check_switch, HBaseTable_Downtime,
HBaseTable_Yield, Mongo_N71_DB, MongoCheck_Collection, MongoSubStationName_Collection,
MongoStationIP_Collection, startTime, stopsTime, BySubStationName_Shifts,
ByStationIP_Shifts, BySubStationName_Rank, ByStationIP_Rank,LastResult, LastQueryTime,
StationSummary, ByStation, ByStation_Shifts)
def createDatabase(name):
connection = Connection(mongoIP, mongoPort)
db = connection[name]
collection = db['testCollection']
collection.insert({"1": 1})
collection.remove({"1": 1})
collection.drop()
def createBySubStationName(db):
schema = {
'downtime': 0,
'line': "0",
'downtime_error': 0,
'time': "0",
'date': "0",
'station_name': "0",
'station_ip': "0",
'sub_station_name': "0"
}
args = ["date", "time", "station_ip", "sub_station_name"]
db.createIndex(MongoSubStationName_Collection, args)
db.insert(MongoSubStationName_Collection, schema)
db.remove(MongoSubStationName_Collection, schema)
def createByStationIP(db):
schema = {
'downtime': 0,
'line': "0",
'downtime_error': 0,
'time': "0",
'date': "0",
'station_name': "0",
'station_ip': "0",
}
args = ["date", "time", "station_ip"]
db.createIndex(MongoStationIP_Collection, args)
db.insert(MongoStationIP_Collection, schema)
db.remove(MongoStationIP_Collection, schema)
def createBySubStationName_Shifts(db):
schema = {
'downtime': 0,
'line': "0",
'downtime_error': 0,
'shift': "0",
'date': "0",
'station_name': "0",
'station_ip': "0",
'stations': "0"
}
args = ["date", "shift", "station_ip", "sub_station_name"]
db.createIndex(BySubStationName_Shifts, args)
db.insert(BySubStationName_Shifts, schema)
db.remove(BySubStationName_Shifts, schema)
def createByStationIP_Shifts(db):
schema = {
'downtime': 0,
'line': "0",
'downtime_error': 0,
'shift': "0",
'date': "0",
'station_name': "0",
'station_ip': "0",
}
args = ["date", "shift", "station_ip"]
db.createIndex(ByStationIP_Shifts, args)
db.insert(ByStationIP_Shifts, schema)
db.remove(ByStationIP_Shifts, schema)
def createBySubStationName_Rank(db):
schema = {
'station_ip': "0",
'rank': "0"
}
args = ["station_ip"]
db.createIndex(BySubStationName_Rank, args)
db.insert(BySubStationName_Rank, schema)
db.remove(BySubStationName_Rank, schema)
def createByStationIP_Rank(db):
schema = {
'station_name': "0",
'rank': "0",
}
args = ["station_name"]
db.createIndex(ByStationIP_Rank, args)
db.insert(ByStationIP_Rank, schema)
db.remove(ByStationIP_Rank, schema)
def createCheck(db):
schema = {
'countY': "0",
'countD': "0",
'datetime': "0"
}
args = ["datetime"]
db.createIndex(MongoCheck_Collection, args)
db.insert(MongoCheck_Collection, schema)
db.remove(MongoCheck_Collection, schema)
def createLastMode(db):
schema = {
'lastCode': "0",
'lastMode': "0",
'lastTime': "0",
'station_ip_sub': "0",
}
args = ['station_ip_sub']
db.createIndex(LastResult, args)
db.insert(LastResult, schema)
db.remove(LastResult, schema)
def createLastTime(db):
schema = {
'LastQueryTime': "0",
'LastInsertTime': "0"
}
args = ['LastQueryTime']
db.createIndex(LastQueryTime, args)
db.insert(LastQueryTime, schema)
db.remove(LastQueryTime, schema)
def createSummary(db):
schema = {
'update_time': "0",
'station_name': "0",
'line': "0"
}
args = ['station_name', 'line']
db.createIndex(StationSummary, args)
db.insert(StationSummary, schema)
db.remove(StationSummary, schema)
def createByStation(db):
schema = {
'downtime': 0,
'line': "0",
'time': "0",
'date': "0",
'station_name': "0"
}
args = ["date", "time", "station_name", "line"]
db.createIndex(ByStation, args)
db.insert(ByStation, schema)
db.remove(ByStation, schema)
def createByStation_Shifts(db):
schema = {
'downtime': 0,
'line': "0",
'shift': "0",
'date': "0",
'station_name': "0"
}
args = ["date", "shift", "station_name", "line"]
db.createIndex(ByStation_Shifts, args)
db.insert(ByStation_Shifts, schema)
db.remove(ByStation_Shifts, schema)
def DropWhole():
db.drop(MongoSubStationName_Collection)
db.drop(MongoStationIP_Collection)
db.drop(BySubStationName_Shifts)
db.drop(ByStationIP_Shifts)
db.drop(BySubStationName_Rank)
db.drop(ByStationIP_Rank)
db.drop(MongoCheck_Collection)
db.drop(StationSummary)
db.drop(ByStation)
db.drop(ByStation_Shifts)
db.drop(LastResult)
# Main Function
if __name__ == "__main__":
name = Mongo_N71_DB
#createDatabase(name)
db = ae.mongo(name, mongoIP, mongoPort)
if len(sys.argv) < 2:
create = 1
else:
create = int(sys.argv[1])
if create:
print "Initialize all collections!"
createDatabase(Mongo_N71_DB)
createBySubStationName(db)
createByStationIP(db)
createBySubStationName_Shifts(db)
createByStationIP_Shifts(db)
createCheck(db)
createLastMode(db)
createLastTime(db)
createSummary(db)
createByStation(db)
createByStation_Shifts(db)
else:
print "Drop all collections!"
DropWhole()