-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathhd.db.connect.py
executable file
·178 lines (168 loc) · 4.08 KB
/
hd.db.connect.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
#!/usr/bin/env python3
############################################################################
#
# MODULE: hd.db.connect
# AUTHOR(S): Matej Krejci ([email protected]
#
# COPYRIGHT: (C) 2016 by the GRASS Development Team
#
# This program is free software under the GNU General
# Public License (>=v2). Read the file COPYING that
# comes with GRASS for details.
#
#############################################################################
# %module
# % description: Connection manager for Hive and Hadoop database
# % keyword: database
# % keyword: hdfs
# %end
# %option
# % key: driver
# % type: string
# % required: no
# % description: Type of database driver
# % options: hiveserver2, hdfs, webhdfs, hive_cli
# % guisection: Connection
# %end
# %option
# % key: conn_id
# % type: string
# % required: no
# % description: Identificator of connection(free string)
# % guisection: Connection
# %end
# %option
# % key: host
# % description: host
# % type: string
# % required: no
# % guisection: Connection
# %end
# %option
# % key: port
# % type: integer
# % required: no
# % description: Port of db
# % guisection: Connection
# %end
# %option
# % key: login
# % type: string
# % required: no
# % description: Login
# % guisection: Connection
# %end
# %option
# % key: passwd
# % type: string
# % required: no
# % description: Password
# % guisection: Connection
# %end
# %option
# % key: schema
# % type: string
# % required: no
# % description: schema
# % guisection: Connection
# %end
# %option
# % key: authmechanism
# % type: string
# % required: no
# % options: PLAIN
# % description: Authentification mechanism type
# % guisection: Connection
# %end
# %option
# % key: connectionuri
# % type: string
# % required: no
# % description: connection uri string of database
# % guisection: Connection uri
# %end
# %option
# % key: rmid
# % type: integer
# % required: no
# % description: Remove connection by id
# % guisection: manager
# %end
# %flag
# % key: c
# % description: Print table of connection
# % guisection: manager
# %end
# %flag
# % key: p
# % description: Print active connection
# % guisection: manager
# %end
# %flag
# % key: r
# % description: Remove all connections
# % guisection: manager
# %end
# %flag
# % key: t
# % description: Test connection by conn_type
# % guisection: manager
# %end
# %flag
# % key: a
# % description: Set active connection by conn_id and driver
# % guisection: manager
# %end
import grass.script as gs
from hdfsgrass.hdfs_grass_lib import ConnectionManager
def main():
# add new connection
conn = ConnectionManager()
if options["connectionuri"]:
conn.set_connection_uri(options["connectionuri"])
conn.add_connection()
conn.test_connection()
return
if options["host"] and options["driver"] and options["conn_id"]:
conn.set_connection(
conn_type=options["driver"],
conn_id=options["conn_id"],
host=options["host"],
port=options["port"],
login=options["login"],
password=options["passwd"],
schema=options["schema"],
)
conn.add_connection()
conn.test_connection()
return
if options["rmid"]:
conn.remove_conn_Id(options["rmid"])
return
# print table of connection
elif flags["c"]:
conn.show_connections()
return
# drop table with connections
elif flags["r"]:
conn.drop_connection_table()
conn.show_connections()
return
# print active connection
elif flags["p"]:
conn.show_active_connections()
return
elif flags["t"]:
if options["driver"]:
conn.test_connection(options["driver"])
else:
print("< driver > is not set")
return
elif flags["a"]:
if not options["driver"] and options["conn_id"]:
conn.set_active_connection(options["driver"], options["conn_id"])
else:
gs.fatal("ERROR parameter < driver > and 'conn_id' must be set")
if __name__ == "__main__":
options, flags = gs.parser()
main()