-
Notifications
You must be signed in to change notification settings - Fork 5
/
httpfs_utils.py
559 lines (469 loc) · 17.4 KB
/
httpfs_utils.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/usr/bin/python2
# httpfs_utils.py
#
# Provides HDFS access via httpfs using Python's requests package.
import datetime
import requests
try:
import simplejson as json
except ImportError:
import json
###################################################################################################
# Helper functions #
###################################################################################################
def _get_max_str_len_(filestatuses, key):
""" Returns the max string value length for a list of dictionaries with 'field' as key.
This is used to pretty print directory listings.
INPUT
-----
filestatus : list of dicts
The FileStatuses dictionary returned by the liststatus method.
key : str
The key for which we wish to find the maximum length value.
OUTPUT
------
int : The length of the longest value.
"""
return max([len(str(B[key])) for B in filestatuses['FileStatuses']['FileStatus']])
def _perm_long_str_(type_str, perm_str):
""" Forms the long string version of the permission string.
INPUT
-----
type_str : str
The type of object as given by list, e.g., 'FILE' or 'DIRECTORY'.
perm_str : str
The short form (numeric) version of the permission string.
OUTPUT
------
str : The long form version of the permission string.
"""
# Determine if a directory is represented.
if type_str == 'DIRECTORY':
perm_str_long = 'd'
else:
perm_str_long = '-'
# Convert the permission string to long letter form.
for n in perm_str:
L = [int(i) for i in list(bin(int(n)).split('0b')[1].zfill(3))]
if L[0]:
perm_str_long += 'r'
else:
perm_str_long += '-'
if L[1]:
perm_str_long += 'w'
else:
perm_str_long += '-'
if L[2]:
perm_str_long += 'x'
else:
perm_str_long += '-'
return perm_str_long
def make_httpfs_url(host, user, hdfs_path, op, port=14000):
""" Forms the URL for httpfs requests.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file or directory being checked.
op : str
The httpfs operation string. E.g., 'GETFILESTATUS'.
port : int
The port to use for httpfs connections.
OUTPUT
------
str : The string to use for an HTTP request to httpfs.
"""
url = 'http://' + user + '@' + host + ':' + str(port) + '/webhdfs/v1'
url += hdfs_path + '?user.name=' + user + '&op=' + op
return url
###################################################################################################
# Functions #
###################################################################################################
def append(host, user, hdfs_path, filename, port=14000):
""" Appends contents of 'filename' to 'hdfs_path' on 'user'@'host':'port'.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file to be appended to in HDFS.
filename : str
The file with contents being appended to hdfs_path. Can be a local file or a full path.
port : int : default=14000
The port to use for httpfs connections.
"""
# Form the URL.
url = make_httpfs_url(
host=host,
user=user,
hdfs_path=hdfs_path,
op='APPEND&data=true',
port=port
)
headers = {
'Content-Type':'application/octet-stream'
}
resp = requests.post(url, data=open(filename,'rb'), headers=headers)
if resp.status_code != 200:
resp.raise_for_status
def appends(host, user, hdfs_path, content, port=14000):
""" Appends 'content' to 'hdfs_path' on 'user'@'host':'port'.
This method is like 'append', but takes a string as input instead of a file name.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file to be appended to in HDFS.
content : str
The contents being appended to hdfs_path.
port : int : default=14000
The port to use for httpfs connections.
"""
# Form the URL.
url = make_httpfs_url(
host=host,
user=user,
hdfs_path=hdfs_path,
op='APPEND&data=true',
port=port
)
headers = {
'Content-Type':'application/octet-stream'
}
resp = requests.post(url, data=content, headers=headers)
if resp.status_code != 200:
resp.raise_for_status
def copy_to_local(host, user, hdfs_path, filename, port=14000):
""" Copies the file at 'hdfs_path' on 'user'@'host':'port' to 'filename' locally.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file in HDFS.
port : int : default=14000
The port to use for httpfs connections.
perms : str or int : default=775
The permissions to use for the uploaded file in HDFS.
"""
# Form the URL.
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op='OPEN', port=port)
# Form and issue the request.
resp = requests.get(url, stream=True)
if resp.status_code == 200:
with open(filename, 'wb') as f_p:
for chunk in resp:
f_p.write(chunk)
else:
resp.raise_for_status
def exists(host, user, hdfs_path, port=14000):
""" Returns True if 'hdfs_path' (full path) exists in HDFS at user@host:port via httpfs.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file or directory being checked.
port : int
The port to use for httpfs connections.
OUTPUT
------
Boolean : True if 'hdfs_path' exists and can be accessed by 'user'; False otherwise.
"""
op = 'GETFILESTATUS'
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op=op, port=port)
# Get the JSON response using httpfs; stores as a Python dict
resp = requests.get(url)
# If a 404 was returned, the file/path does not exist
if resp.status_code == 404:
return False
# If a 200 was returned, the file/path does exist
elif resp.status_code == 200:
return True
# Something else - raise status, or if all else fails return None
else:
resp.raise_for_status()
return None
def get_blocksize(host, user, hdfs_path, port=14000):
""" Returns the HDFS block size (bytes) of 'hdfs_path' in HDFS at user@host:port via httpfs.
The returned block size is in bytes. For MiB, divide this value by 2**20=1048576.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file or directory being checked.
port : int
The port to use for httpfs connections.
OUTPUT
------
int/long : The block size in bytes.
"""
op = 'GETFILESTATUS'
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op=op, port=port)
# Get the JSON response using httpfs; stores as a Python dict
resp = requests.get(url)
# If a 200 was returned, the file/path exists
if resp.status_code == 200:
return resp.json()['FileStatus']['blockSize']
# Something else - raise status, or if all else fails return None
else:
resp.raise_for_status()
def get_size(host, user, hdfs_path, port=14000):
""" Returns the size (bytes) of 'hdfs_path' in HDFS at user@host:port via httpfs.
The returned block size is in bytes. For MiB, divide this value by 2**20=1048576.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file or directory being checked.
port : int
The port to use for httpfs connections.
OUTPUT
------
int/long : The size in bytes.
"""
op = 'GETFILESTATUS'
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op=op, port=port)
# Get the JSON response using httpfs; stores as a Python dict
resp = requests.get(url)
# If a 200 was returned, the file/path exists
if resp.status_code == 200:
return resp.json()['FileStatus']['length']
# Something else - raise status, or if all else fails return None
else:
resp.raise_for_status()
def info(host, user, hdfs_path, port=14000):
""" Returns a dictionary of info for 'hdfs_path' in HDFS at user@host:port via httpfs.
This method is similar to 'liststatus', but only displays top-level information. If you need
info about all of the files and subdirectories of a directory, use 'liststatus'.
The returned dictionary contains keys: group, permission, blockSize, accessTime, pathSuffix,
modificationTime, replication, length, ownder, type.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file or directory being checked.
port : int
The port to use for httpfs connections.
OUTPUT
------
Dictionary : Information about 'hdfs_path'
"""
op = 'GETFILESTATUS'
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op=op, port=port)
# Get the JSON response using httpfs; stores as a Python dict
resp = requests.get(url)
# If a 200 was returned, the file/path exists
if resp.status_code == 200:
return resp.json()
# Something else - raise status, or if all else fails return None
else:
resp.raise_for_status()
def liststatus(host, user, hdfs_path, port=14000):
""" Returns a dictionary of info for 'hdfs_path' in HDFS at user@host:port via httpfs.
Returns a dictionary of information. When used on a file, the returned dictionary contains a
copy of the dictionary returned by 'info.' When used on a directory, the returned dictionary
contains a list of such dictionaries.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file or directory being checked.
port : int
The port to use for httpfs connections.
OUTPUT
------
Dictionary : Information about 'hdfs_path'
"""
op = 'LISTSTATUS'
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op=op, port=port)
# Get the JSON response using httpfs; stores as a Python dict
resp = requests.get(url)
# If a 200 was returned, the file/path exists
if resp.status_code == 200:
return resp.json()
# Something else - raise status, or if all else fails return None
else:
resp.raise_for_status()
def ls(host, user, hdfs_path, port=14000):
""" Print info for 'hdfs_path' in HDFS at user@host:port via httpfs.
A print function intended for interactive usage. Similar to 'ls -l' or 'hdfs dfs -ls'.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file or directory being checked.
port : int
The port to use for httpfs connections.
"""
op = 'LISTSTATUS'
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op=op, port=port)
# Get the JSON response using httpfs; stores as a Python dict
resp = requests.get(url)
# If a 200 was returned, the file/path exists. Otherwise, raise error or exit.
if resp.status_code != 200:
resp.raise_for_status()
else:
filestatuses = resp.json()
for obj in filestatuses['FileStatuses']['FileStatus']:
obj_str = _perm_long_str_(type_str=obj['type'],perm_str=obj['permission'])
obj_str += '%*s' % (
_get_max_str_len_(filestatuses, 'replication')+3,
obj['replication']
)
obj_str += '%*s' % (
_get_max_str_len_(filestatuses, 'owner')+3,
obj['owner']
)
obj_str += '%*s' % (
_get_max_str_len_(filestatuses, 'group')+2,
obj['group']
)
obj_str += '%*s' % (
_get_max_str_len_(filestatuses, 'length')+4,
obj['length']
)
obj_str += '%21s' % (
datetime.datetime.utcfromtimestamp(
obj['modificationTime']/1000
).isoformat().replace('T',' ')
)
obj_str += ' ' + hdfs_path + '/' + obj['pathSuffix']
print "%s" % obj_str
def mkdir(host, user, hdfs_path, port=14000):
""" Creates the directory 'hdfs_path' on 'user'@'host':'port'.
Directories are created recursively.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The path of the directory to create in HDFS.
port : int : default=14000
The port to use for httpfs connections.
"""
op = 'MKDIRS'
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op=op, port=port)
# Make the request
resp = requests.put(url)
# If a 200 was returned, the file/path exists
if resp.status_code == 200:
return resp.json()
# Something else - raise status, or if all else fails return None
else:
resp.raise_for_status()
def put(host, user, hdfs_path, filename, port=14000, perms=775):
""" Puts 'filename' into 'hdfs_path' on 'user'@'host':'port'.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the location to place the file in HDFS.
filename : str
The file to upload. Can be a local file or a full path.
port : int : default=14000
The port to use for httpfs connections.
perms : str or int : default=775
The permissions to use for the uploaded file in HDFS.
"""
# Get the file name without base path.
filename_short = filename.split('/')[-1]
# Form the URL.
url = make_httpfs_url(
host=host,
user=user,
hdfs_path=hdfs_path + '/' + filename_short,
op='CREATE&data=true&overwrite=true&permission=' + str(perms),
port=port
)
headers = {
'Content-Type':'application/octet-stream'
}
#files = {'file': open(filename,'rb')}
resp = requests.put(url, data=open(filename,'rb'), headers=headers)
if resp.status_code != 200:
resp.raise_for_status()
def read(host, user, hdfs_path, port=14000):
""" Reads file at 'hdfs_path' on 'user'@'host':'port'.
This method allows the contents of a file in HDFS to be read into memory in Python.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file in HDFS.
port : int : default=14000
The port to use for httpfs connections.
perms : str or int : default=775
The permissions to use for the uploaded file in HDFS.
OUTPUT
------
Text of the file.
"""
# Form the URL.
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op='OPEN', port=port)
# Form and issue the request.
resp = requests.get(url)
if resp.status_code != 200:
resp.raise_for_status
return resp.text
def read_json(host, user, hdfs_path, port=14000):
""" Reads JSON file at 'hdfs_path' on 'user'@'host':'port' and returns a Python dict.
This method reads the contents of a JSON file in HDFS into Python as a dictionary.
INPUT
-----
host : str
The host to connect to for httpfs access to HDFS. (Can be 'localhost'.)
user : str
The user to use for httpfs connections.
hdfs_path : str
The full path of the file in HDFS.
port : int : default=14000
The port to use for httpfs connections.
perms : str or int : default=775
The permissions to use for the uploaded file in HDFS.
OUTPUT
------
Text of the file interpreted in JSON as a Python dict.
"""
# Form the URL.
url = make_httpfs_url(host=host, user=user, hdfs_path=hdfs_path, op='OPEN', port=port)
# Form and issue the request.
resp = requests.get(url)
if resp.status_code != 200:
resp.raise_for_status
return json.loads(requests.get(url).text)