-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathhdfsbrowser.py
412 lines (334 loc) · 13.4 KB
/
hdfsbrowser.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of HDFSBrowser.
#
# Copyright (C) 2016, Mahdi Braik <braik.mahdi at gmail.com>
# All rights reserved.
#
# HDFSBrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HDFSBrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with HDFSBrowser. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import requests
import json
from lxml import etree
from StringIO import StringIO
import logging
import re
import argparse
from multiprocessing import Pool
import time
# Script version
VERSION = '0.1'
# Global vars
API = "/webhdfs/v1"
option = "LISTSTATUS"
HOST = "localhost"
PORT = 50070
HTTP = "http"
HTTPS = "https"
# Logger definition
LOGLEVELS = {0 : 'ERROR', 1 : 'INFO', 2 : 'DEBUG'}
logger_output = logging.StreamHandler(sys.stdout)
logger_output.setFormatter(logging.Formatter('[%(levelname)s][%(name)s] %(message)s'))
logger_gen = logging.getLogger("General")
logger_gen.addHandler(logger_output)
# HDFS's file class definition
class File_HDFS():
"""
Class representation of HDFS file
"""
def __init__(self, filetype, permission, owner, group, pathSuffix, modificationTime, path):
self.filetype = filetype
self.perm = permission
self.owner = owner
self.group = group
self.pathSuffix = pathSuffix
self.lastModif = modificationTime
self.path = path
self.children = []
def is_directory(self):
return True if self.filetype == "DIRECTORY" else False
def print_HDFS(self):
# t = "d" if self.is_directory() else "-"
print self.perm + " " + self.owner + ":" + self.group + " " + str(self.lastModif) + " " + self.pathSuffix + " " + self.path
def get_url(self):
return "http://" + HOST + ":" + str(PORT) + API + self.path + "?op=" + option + "&user.name=" + self.owner
def print_HDFS_csv(self):
return ";".join([str(self.lastModif), self.filetype, self.perm, self.owner, self.group, self.pathSuffix, self.get_url()]) + "\n"
def add_child(self, child):
self.children.append(child)
# Useful functions
f = lambda x : [int(i) for i in bin(x)[2:].zfill(3)]
g = lambda tuplet_list : tuplet_list[0] if tuplet_list[1] else '-'
h = lambda l1, l2 : [t for t in zip(l1,l2)]
def perm_to_str(num):
"""
Integer to UNIX permission
"""
s = ['r', 'w', 'x']
num = [int(i) for i in str(num)]
if all(map(lambda x : x < 8, num)) and len(num) == 3:
return "".join(map(lambda x : "".join(map(g, h(s, f(x)))), num))
else:
return "--ERROR--"
def parse_hdfs_json(json_string, path):
#print repr(json_string)
data_json = json.loads(json_string)
files_HDFS = []
for f in data_json['FileStatuses']['FileStatus']:
tmp_path = path + f["pathSuffix"] + ("/" if f["type"] == "DIRECTORY" else "")
permission = perm_to_str(int(f["permission"]))
files_HDFS.append(File_HDFS(f["type"], permission, f["owner"], f["group"], f["pathSuffix"], f["modificationTime"], tmp_path))
return files_HDFS
def parse_hdfs_xml(xml_string):
tree = etree.parse(StringIO(xml_string))
files_HDFS = []
for e in tree.xpath("/listing")[0].getchildren():
f = {
"permission" : None,
"owner" : None,
"group" : None,
"pathSuffix" : None,
"modified" : None,
"path" : None
}
if e.tag == "directory" or e.tag == "file":
f["filetype"] = e.tag
for attribut in e.keys():
f[attribut] = e.get(attribut)
f["pathSuffix"] = f["path"].split("/")[-1]
files_HDFS.append(File_HDFS(f["filetype"], f["permission"], f["owner"], f["group"], f["pathSuffix"], f["modified"], f["path"]))
return files_HDFS
def request_namenode(path, user):
"""
Request HDFS via HTTP(S) to get file list
using the REST API : /webhdfs/v1
"""
op = "op=LISTSTATUS"
URL = "http://" + HOST + ":" + str(PORT) + API + path + "?" + op + "&user.name=" + user
#print 'URL %s' % URL
response = requests.get(URL)
data = None
if response.ok:
data = response.text
else:
error("Unable to access the following resource : %s" % (URL))
return data
def request_namenode_multi(file_HDFS):
return request_namenode(file_HDFS.path, file_HDFS.owner)
def request_namenode_multi2(file_HDFS, q):
return request_namenode(file_HDFS.path, file_HDFS.owner)
def hdfs_listpaths(user, path, use_recursion, output):
"""
Request HDFS to get the native list
using the SOAP API : /listPaths
"""
rec = "yes" if use_recursion else "no"
URL = "http://" + HOST + ":" + str(PORT) + "/listPaths" + path + "?recursive=" + rec
try:
response = requests.get(URL)
if response.ok:
files_HDFS = parse_hdfs_xml(response.content)
if output:
with open(output, 'w') as f:
for e in files_HDFS: f.write(e.print_HDFS_csv())
f.close()
else:
for e in files_HDFS: e.print_HDFS()
except:
#print error("Unable to access the following resource : %s" % (URL))
print sys.exc_info()
return False
return True
def hdfs_ls(path, user, output):
json_string = request_namenode(path, user)
t = []
if json_string:
files_HDFS = parse_hdfs_json(json_string, path)
jobs = []
for e in files_HDFS:
#e.print_HDFS()
if e.is_directory():
p = Process(target=hdfs_ls, args=(e.path, e.owner, output))
for child in hdfs_ls(e.path, e.owner, output):
e.add_child(child)
return files_HDFS
def init_ls(path, user, port, depth, output):
json_string = request_namenode(path, user)
files_HDFS = parse_hdfs_json(json_string, path)
if depth:
hdfs_ls_multi(files_HDFS, depth - 1)
return files_HDFS
def hdfs_ls_multi(files_HDFS, depth):
p = Pool(10)
#t_deb = time.time()
json_strings = p.map(request_namenode_multi, files_HDFS)
p.close()
p.join()
#t_fin = time.time()
#duration = t_fin - t_deb
#print duration
for parent, json_string in zip(files_HDFS, json_strings):
#print "yolo json_string '%s', parent '%s'" % (json_string, parent.print_HDFS())
tt = parse_hdfs_json(json_string, parent.path)
if depth:
output = hdfs_ls_multi([e for e in tt if e.is_directory()], depth - 1)
parent.children = tt
def print_arbo(file_HDFS):
if file_HDFS.children != []:
print file_HDFS.path + " : "
for child in file_HDFS.children:
child.print_HDFS()
print
for child in file_HDFS.children:
print_arbo(child)
def print_list(files_HDFS):
for e in files_HDFS:
e.print_HDFS()
print
for e in files_HDFS:
print_arbo(e)
service_names = {
"whdfs" : "WebHDFS",
"httpfs" : "HttpFS"
}
service_ports = {
"whdfs" : 50070,
"httpfs" : 14000
}
def build_URL(protocol, host, port, rp_path, root_path, api_path, option):
"""
Build the URL to request
"""
if rp_path:
return "%s://%s:%s%s%s%s?op=%s" % (protocol, host, port, rp_path, api_path, root_path, option)
else:
return "%s://%s:%s%s%s?op=%s" % (protocol, host, port, api_path, root_path, option)
def test_service(method, port):
"""
Only testing if the service is up and responding
"""
info("Testing service %s" % (service_names[method]))
try:
URL = build_URL(PROTOCOL, HOST, port, RP_PATH , "/", API, option)
#print URL
response = requests.get(URL)
if response.ok:
success("Service %s is available\n" % (service_names[method]))
return True
else:
print error("Service %s seems to be down\n")
return False
except:
error("Exception during requesting the service\n")
return False
def test_all_services():
"""
Test the default services : Web HDFS / HttpFS
"""
info("Beginning to test services accessibility using default ports ...")
found_services = {}
counter = 0
for service in service_names:
if test_service(service, service_ports[service]):
found_services.update({service : True})
counter += 1
else:
found_services.update({service : False})
if counter:
success("Sucessfully retrieved %d services" % (counter))
else:
error("No service found.")
return found_services, counter
def warning(message):
print "\044[93m[!] \033[0m" + message
def success(message):
print "\033[92m[+] \033[0m" + message
def error(message):
print "\033[91m[-] \033[0m" + message
def info(message):
print message
def main():
VERSION = '0.1-dev'
parser = argparse.ArgumentParser(description="""
_ _____________ ___________
| | | | _ \ ___/ ___| ___ \
| |_| | | | | |_ \ `--.| |_/ /_ __ _____ _____ ___ _ __
| _ | | | | _| `--. \ ___ \ '__/ _ \ \ /\ / / __|/ _ \ '__|
| | | | |/ /| | /\__/ / |_/ / | | (_) \ V V /\__ \ __/ |
\_| |_/___/ \_| \____/\____/|_| \___/ \_/\_/ |___/\___|_|
""",
prog='hdfsbrowser.py',
formatter_class=argparse.RawTextHelpFormatter)
# Positional args
parser.add_argument('host', action='store', help='host')
# Optional args
parser.add_argument('--port', type=int, action='store', default=None, help='HDFS port')
parser.add_argument('-o', '--output-file', type=str, action='store', help='Write to file instead of stdout')
parser.add_argument('--ssl', action='store_true', default=False, help='Try SSL/TLS')
parser.add_argument('--target-uri', type=str, action='store', default=None, help='Target URI of the Hadoop installation')
parser.add_argument('-r', '--recursive', action='store_true', default=False, help='Be recursive')
parser.add_argument('--root-path', type=str, action='store', default="/", help='Root path for the directory listing')
parser.add_argument('-m', '--method', choices={"whdfs", "httpfs", "all"}, action='store', default="all", help='Method to use [')
parser.add_argument('--time-out', type=int, action='store', default=5, help='Timeout for request')
parser.add_argument( '-d', '--depth', type=int, action='store', default=-1, help='Recursion depth while listing directories')
parser.add_argument( '--threads', type=int, action='store', default=2, help='Number of threads')
parser.add_argument( '--proxy', type=str, action='store', default=None, help='Use proxy on given port')
parser.add_argument( '--header', type=str, action='store', default=None, help='Pass custom header to server')
global LOGLEVELS, HOST, PORT, PROTOCOL, URL, RP_PATH, ROOT_PATH
if len(sys.argv) == 1:
print "\033[91" + "[!] Missing host ... Exiting"
parser.print_help()
sys.exit(1)
else:
args = parser.parse_args()
HOST = args.host
if args.port:
PORT = args.port
else:
if args.method == "httpfs": PORT = 14000
else: PORT = 50070
PROTOCOL = HTTPS if args.ssl else HTTP
ROOT_PATH = args.root_path
RP_PATH = args.target_uri
THREAD = args.threads
if args.method == "all":
if args.port: warning("Method all does not support --port option : test will be performed using default ports (14000, 50070)")
services, counter = test_all_services()
if counter:
if services.has_key("whdfs"):
PORT = service_ports["whdfs"]
result = hdfs_listpaths("user", ROOT_PATH, args.recursive, args.output_file)
if not result:
files_HDFS = init_ls(ROOT_PATH, "hdfs", service_ports["whdfs"], args.depth, args.output_file)
print_list(files_HDFS)
else:
PORT = service_ports["httpfs"]
files_HDFS = init_ls(ROOT_PATH, "hdfs", service_ports["whdfs"], args.depth, args.output_file)
print_list(files_HDFS)
elif args.method == "whdfs":
info("Trying using method WebHDFS (default port : 50070), trying on port %s" % (PORT))
if test_service(args.method, PORT):
files_HDFS = init_ls(ROOT_PATH, "hdfs", PORT, args.depth, args.output_file)
print_list(files_HDFS)
elif args.method == "httpfs":
info("Trying using method HttpFS (default port : 14000), trying on port %s" % (PORT))
if test_service(args.method, PORT):
files_HDFS = init_ls(ROOT_PATH, "hdfs", PORT, args.depth, args.output_file)
print_list(files_HDFS)
else:
error("Method %s does not exist, you can use (whdfs, httpfs)" % (args.method))
return None
if __name__ == "__main__":
main()