-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathhd.hdfs.out.vector.py
executable file
·119 lines (99 loc) · 2.9 KB
/
hd.hdfs.out.vector.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
#!/usr/bin/env python3
############################################################################
#
# MODULE: hd.hdfs.out.vector
# 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: Module for creting map from HIVE table. Module convert esri GeoJson to Grass map
# % keyword: database
# % keyword: hdfs
# % keyword: hive
# %end
# %option
# % key: driver
# % type: string
# % required: yes
# % options: webhdfs
# % description: HDFS driver
# %end
# %option
# % key: table
# % type: string
# % description: Name of table for import
# %end
# %option
# % key: hdfs
# % type: string
# % description: Hdfs path to the table. See hive.info table -h
# %end
# %option G_OPT_V_OUTPUT
# % key: out
# % required: yes
# %end
# %flag
# % key: r
# % description: remove temporal file
# % guisection: data
# %end
# %option
# % key: attributes
# % type: string
# % description: list of attributes with datatype
# % guisection: data
# %end
import os
import sys
import grass.script as gs
from hdfsgrass.hdfs_grass_lib import (
GrassMapBuilderEsriToEsri,
GrassHdfs,
ConnectionManager,
)
from hdfsgrass.hdfs_grass_util import get_tmp_folder
import shutil
def main():
tmp_dir = os.path.join(get_tmp_folder(), options["out"])
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
transf = GrassHdfs(options["driver"])
table_path = options["hdfs"]
if options["table"]:
conn = ConnectionManager()
conn.get_current_connection("hiveserver2")
if not conn.get_current_connection("hiveserver2"):
gs.fatal(
"Cannot connet to hive for table description. "
"Use param hdfs without param table"
)
hive = conn.get_hook()
table_path = hive.find_table_location(options["table"])
tmp_dir = os.path.join(tmp_dir, options["table"])
if not transf.download(hdfs=table_path, fs=tmp_dir):
return
files = os.listdir(tmp_dir)
map_string = ""
for block in files:
map = "%s_%s" % (options["out"], block)
block = os.path.join(tmp_dir, block)
map_build = GrassMapBuilderEsriToEsri(block, map, options["attributes"])
try:
map_build.build()
map_string += "%s," % map
except Exception as e:
gs.warning("Error: %s\n Map < %s > conversion failed" % (e, block))
path, folder_name = os.path.split(tmp_dir)
gs.message(
"For merge map: v.patch output=%s -e --overwrite input=%s"
% (folder_name, map_string)
)
if __name__ == "__main__":
options, flags = gs.parser()
main()