-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclickhouse.py
More file actions
46 lines (39 loc) · 1.17 KB
/
clickhouse.py
File metadata and controls
46 lines (39 loc) · 1.17 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
import subprocess
import settings
def __build_command():
cmd = [\
settings.CLICKHOUSE['bin'], \
'--host', settings.CLICKHOUSE['host'], \
'--port', settings.CLICKHOUSE['port'], \
'--multiquery' \
]
return cmd
def __build_command_query(query):
cmd = __build_command()
cmd.extend([ \
'--query', query \
])
return cmd
def __execute_command(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return { 'returncode': p.returncode, 'out': out, 'err': err }
def query(query):
cmd = __build_command_query(query)
return __execute_command(cmd)
def query_to_file(query, path):
cmd = __build_command_query(query)
exitcode=0
with open(path, 'w+') as f:
p = subprocess.Popen(cmd, stdout=f, stderr=subprocess.PIPE)
p.wait()
exitcode = p.returncode
return exitcode
def file_to_table(path, query):
cmd = __build_command_query(query)
exitcode=0
with open(path, 'r') as f:
p = subprocess.Popen(cmd, stdin=f, stderr=subprocess.PIPE)
p.wait()
exitcode = p.returncode
return exitcode