-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-on-es-cli.py
executable file
·289 lines (227 loc) · 9.09 KB
/
sql-on-es-cli.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import readline
import os
import json
import httplib
import urlparse
import urllib
reload(sys)
sys.setdefaultencoding('utf-8')
def print_usage():
sys.stderr.write("Usage: {0} <endpoint> [-j]\n".format(sys.argv[0]))
sys.stderr.write(" -j: print Json Format rather than TABLE\n")
sys.stderr.write("\nEx) {0} http://localhost:9200\n".format(sys.argv[0]))
sys.exit(1)
hist_file = os.path.join(os.path.expanduser("~"), ".sql-on-es.hist")
try:
readline.read_history_file(hist_file)
except (IOError):
pass
import atexit
atexit.register(readline.write_history_file, hist_file)
import getopt
def parseopt():
try:
optlist, args = getopt.getopt(' '.join(sys.argv[1:]).split(), 'j')
if len(args) == 0:
print_usage()
sys.exit(1)
except getopt.GetoptError as err:
sys.stderr.write("Error: " + str(err) + "\n\n")
print_usage()
sys.exit(1)
output_mode = OutputMode.TABLE
if '-j' in dict(optlist):
output_mode = OutputMode.JSON
return args[0], output_mode
class HttpClient:
def __init__(self, endpoint):
o = urlparse.urlparse(endpoint)
self.protocol = o.scheme
self.hostname = o.hostname
self.port = o.port
if self.protocol == "" or self.hostname == "":
sys.stderr.write("Error: invalid url. '" + endpoint + "'\n")
sys.exit(1)
def get(self, sql):
try:
conn = httplib.HTTPConnection(self.hostname, self.port)
conn.request("GET", "/_sql?sql=" + urllib.quote(sql))
response = conn.getresponse()
return json.loads(response.read())
except Exception as e:
sys.stderr.write("Http Error: %s\n" % str(e))
sys.exit(1)
class OutputMode:
TABLE = 1
JSON = 2
class JsonOutput:
@staticmethod
def emit(json_obj):
print json.dumps(json_obj, indent = 2)
class TableOutput:
print_id_type = True
id_max_len = 0
type_max_len = 0
@staticmethod
def emit(json_obj):
output = []
field_order = []
if "aggregations" in json_obj:
(output, field_order) = TableOutput.print_aggr_output(json_obj)
else:
(output, field_order) = TableOutput.print_normal_output(json_obj)
TableOutput.print_output(output, field_order)
TableOutput.print_meta(json_obj, len(output))
@staticmethod
def print_meta(json_obj, row_count):
took = float(json_obj["took"]) / 1000
print "\n%d rows printed" % row_count
hitted_doc_count = json_obj["hits"]["total"]
print "%d docs hitted (%.3f sec)\n" % (hitted_doc_count, took)
@staticmethod
def print_aggr_output(json_obj):
aggr_output = []
field_order = []
TableOutput.agg_output2arr(json_obj["aggregations"],
aggr_output,
field_order)
return (aggr_output, field_order)
@staticmethod
# output: array of dict
def print_output(aggr_output, field_order):
field_name_len_map = TableOutput.get_field_len_map(aggr_output,
field_order)
TableOutput.print_header(field_order, field_name_len_map)
TableOutput.print_data(aggr_output, field_order, field_name_len_map)
@staticmethod
def get_field_len_map(aggr_output, field_order):
field_name_len_map = {}
for field in field_order:
field_name_len_map[field] = len(field)
TableOutput.set_field_name_len_map(aggr_output, field_name_len_map)
return field_name_len_map
@staticmethod
def print_header(field_order, field_name_len_map):
# print |field1|field2|field2|...|fieldn|
for k in field_order:
sys.stdout.write(("| %-" + str(field_name_len_map[k]) + "s ") % k)
sys.stdout.write("|\n")
# print |---|---|----|...|---|
for k in field_order:
sys.stdout.write(("|%" + str(field_name_len_map[k]) + "s") % ("-" * (field_name_len_map[k] + 2)))
sys.stdout.write("|\n")
@staticmethod
def print_data(aggr_output, field_order, field_name_len_map):
for arr in aggr_output:
for field in field_order:
sys.stdout.write(("| %"+str(field_name_len_map[field])+"s ") % str(arr[field]))
sys.stdout.write("|\n")
@staticmethod
def set_field_name_len_map(output, field_name_len_map):
for arr in output:
for k in arr:
field_name_len_map[k] = max(field_name_len_map[k],
len(str(arr[k])))
@staticmethod
def agg_output2arr(aggr, docs, field_order):
field_hash = {}
grp_by_fld_info = {} # contains group by field
TableOutput.visit_aggr_node(aggr,
docs,
grp_by_fld_info,
field_order,
field_hash)
@staticmethod
def visit_aggr_node(aggr, docs, grp_by_fld_info, field_order, field_hash):
for grp_field in aggr: # "aggregations"->"grp_field"
if grp_field == "key" or grp_field == "doc_count":
continue
for grp_doc in aggr[grp_field]["buckets"]: #"grp_field"->"buckets"[]
if grp_field not in field_hash:
field_order.append(grp_field)
field_hash[grp_field] = True
grp_by_fld_info[grp_field] = grp_doc['key']
aggr_found = False
for sub_key in grp_doc:
if sub_key != "key" and sub_key != "doc_count":
# sub level group by field or actual value
if "buckets" in grp_doc[sub_key]:
TableOutput.visit_aggr_node(grp_doc,
docs,
grp_by_fld_info,
field_order,
field_hash)
else:
aggr_found = True
if sub_key not in field_hash:
field_order.append(sub_key)
field_hash[sub_key] = True
grp_by_fld_info[sub_key] = grp_doc[sub_key]["value"]
if aggr_found:
docs.append(grp_by_fld_info.copy())
@staticmethod
def print_normal_output(json_obj):
if len(json_obj["hits"]["hits"]) == 0:
print "Empty set\n"
return
normal_output = []
field_order = [] # field 출력 순서.
field_order_made = False
for doc in json_obj["hits"]["hits"]:
row = {}
if TableOutput.print_id_type is True:
row["_id"] = doc["_id"]
row["_type"] = doc["_type"]
if field_order_made == False:
field_order.append("_id")
field_order.append("_type")
row.update(doc["_source"])
if field_order_made == False:
field_order += doc["_source"].keys()
field_order_made = True
normal_output.append(row)
return (normal_output, field_order)
@staticmethod
def _print_id_type(doc):
id_format = TableOutput.get_str_format(TableOutput.id_max_len, True)
type_format = TableOutput.get_str_format(TableOutput.type_max_len, True)
sys.stdout.write(id_format % doc["_id"])
sys.stdout.write(type_format % doc["_type"])
@staticmethod
def print_field(field_len_map, k, source):
str_format = TableOutput.get_str_format(field_len_map[k]["len"],
field_len_map[k]["is_str"])
v = ""
if k in source:
v = source[k]
sys.stdout.write(str_format % v)
class SQLExecutor:
def __init__(self, endpoint, output_mode):
self.http_client = HttpClient(endpoint)
self.output_mode = output_mode
def run(self, sql):
if sql.strip() == "": return
json_obj = self.http_client.get(sql)
if json_obj.get("status", 200) != 200:
print_es_error(json_obj)
return
if self.output_mode == OutputMode.TABLE:
TableOutput.emit(json_obj)
else:
JsonOutput.emit(json_obj)
def print_es_error(json_obj):
print "status: " + str(json_obj.get("status", 200))
print "error: " + str(json_obj.get("error", ""))
if __name__ == "__main__":
endpoint, output_mode = parseopt()
executor = SQLExecutor(endpoint, output_mode)
while True:
try:
sql = raw_input('SQL> ')
executor.run(sql)
except (EOFError):
break
print "\nexiting..."