-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
1300 lines (1101 loc) · 47.4 KB
/
client.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# encoding: utf-8
"""WebHDFS API clients."""
from .util import AsyncWriter, HdfsError
from collections import deque
from contextlib import closing, contextmanager
from getpass import getuser
from itertools import repeat
from multiprocessing.pool import ThreadPool
from random import sample
from shutil import move, rmtree
from six import add_metaclass
from six.moves.urllib.parse import quote
from threading import Lock
import codecs
import logging as lg
import os
import os.path as osp
import posixpath as psp
import re
import requests as rq
import sys
import time
_logger = lg.getLogger(__name__)
def _to_error(response):
"""Callback when an API response has a non 2XX status code.
:param response: Response.
"""
if response.status_code == 401:
_logger.error(response.content)
raise HdfsError('Authentication failure. Check your credentials.')
try:
# Cf. http://hadoop.apache.org/docs/r1.0.4/webhdfs.html#Error+Responses
message = response.json()['RemoteException']['message']
except ValueError:
# No clear one thing to display, display entire message content
message = response.content
try:
exception = response.json()['RemoteException']['exception']
except ValueError:
exception = None
return HdfsError(message, exception=exception)
class _Request(object):
"""Class to define API requests.
:param verb: HTTP verb (`'GET'`, `'PUT'`, etc.).
:param kwargs: Keyword arguments passed to the request handler.
"""
webhdfs_prefix = '/webhdfs/v1'
doc_url = 'https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/WebHDFS.html'
def __init__(self, method, **kwargs):
self.method = method
self.kwargs = kwargs
def __call__(self):
pass # make pylint happy
def to_method(self, operation):
"""Returns method associated with request to attach to client.
:param operation: operation name.
This is called inside the metaclass to switch :class:`_Request` objects
with the method they represent.
"""
def api_handler(client, hdfs_path, data=None, strict=True, **params):
"""Wrapper function."""
params['op'] = operation
if client._proxy is not None:
params['doas'] = client._proxy
attempted_hosts = set()
while True:
with client._lock:
while client._urls[0] in attempted_hosts:
client._urls.rotate(-1)
host = client._urls[0]
url = '{}{}{}'.format(
host.rstrip('/'),
self.webhdfs_prefix,
quote(client.resolve(hdfs_path), '/= '),
)
err = None
try:
res = client._request(
method=self.method,
url=url,
data=data,
params=params,
**self.kwargs
)
except (rq.exceptions.ReadTimeout, rq.exceptions.ConnectTimeout,
rq.exceptions.ConnectionError) as retriable_err:
err = retriable_err # Retry.
else:
if res: # 2XX status code.
return res
err = _to_error(res)
if err.exception not in ('RetriableException', 'StandbyException'):
if strict:
raise err
return res
attempted_hosts.add(host)
if len(attempted_hosts) == len(client._urls):
if len(client._urls) > 1:
_logger.warning('No reachable host, raising last error.')
raise err
api_handler.__name__ = '{}_handler'.format(operation.lower())
api_handler.__doc__ = 'Cf. {}#{}'.format(self.doc_url, operation)
return api_handler
class _ClientType(type):
"""Metaclass that enables short and dry request definitions.
This metaclass transforms any :class:`_Request` instances into their
corresponding API handlers. Note that the operation used is determined
directly from the name of the attribute (trimming numbers and underscores and
uppercasing it).
"""
pattern = re.compile(r'_|\d')
def __new__(mcs, name, bases, attrs):
for key, value in attrs.items():
if isinstance(value, _Request):
attrs[key] = value.to_method(mcs.pattern.sub('', key).upper())
client = super(_ClientType, mcs).__new__(mcs, name, bases, attrs)
client.__registry__[client.__name__] = client
return client
@add_metaclass(_ClientType)
class Client(object):
"""Base HDFS web client.
:param url: Hostname or IP address of HDFS namenode, prefixed with protocol,
followed by WebHDFS port on namenode. You may also specify multiple URLs
separated by semicolons for High Availability support.
:param proxy: User to proxy as.
:param root: Root path, this will be prefixed to all HDFS paths passed to the
client. If the root is relative, the path will be assumed relative to the
user's home directory.
:param timeout: Connection timeouts, forwarded to the request handler. How
long to wait for the server to send data before giving up, as a float, or a
`(connect_timeout, read_timeout)` tuple. If the timeout is reached, an
appropriate exception will be raised. See the requests_ documentation for
details.
:param session: `requests.Session` instance, used to emit all requests.
In general, this client should only be used directly when its subclasses
(e.g. :class:`InsecureClient`, :class:`TokenClient`, and others provided by
extensions) do not provide enough flexibility.
.. _requests: http://docs.python-requests.org/en/latest/api/#requests.request
"""
__registry__ = {}
def __init__(self, url, root=None, proxy=None, timeout=None, session=None):
self.root = root
self.url = url
self.urls = [u for u in url.split(';') if u]
self._urls = deque(self.urls) # this is rotated and used internally
self._session = session or rq.Session()
self._proxy = proxy
self._timeout = timeout
self._lock = Lock()
_logger.info('Instantiated %r.', self)
def __repr__(self):
return '<{}(url={!r})>'.format(self.__class__.__name__, self.url)
# Generic request handler
def _request(self, method, url, **kwargs):
r"""Send request to WebHDFS API.
:param method: HTTP verb.
:param url: Url to send the request to.
:param \*\*kwargs: Extra keyword arguments forwarded to the request
handler. If any `params` are defined, these will take precedence over
the instance's defaults.
"""
return self._session.request(
method=method,
url=url,
timeout=self._timeout,
headers={'content-type': 'application/octet-stream'}, # For HttpFS.
**kwargs
)
# Raw API endpoints
_append = _Request('POST', allow_redirects=False) # cf. `read`
_create = _Request('PUT', allow_redirects=False) # cf. `write`
_delete = _Request('DELETE')
_get_acl_status = _Request('GET')
_get_content_summary = _Request('GET')
_get_file_checksum = _Request('GET')
_get_file_status = _Request('GET')
_get_home_directory = _Request('GET')
_get_trash_root = _Request('GET')
_list_status = _Request('GET')
_mkdirs = _Request('PUT')
_modify_acl_entries = _Request('PUT')
_remove_acl_entries = _Request('PUT')
_remove_default_acl = _Request('PUT')
_remove_acl = _Request('PUT')
_open = _Request('GET', stream=True)
_rename = _Request('PUT')
_set_acl = _Request('PUT')
_set_owner = _Request('PUT')
_set_permission = _Request('PUT')
_set_replication = _Request('PUT')
_set_times = _Request('PUT')
_allow_snapshot = _Request('PUT')
_disallow_snapshot = _Request('PUT')
_create_snapshot = _Request('PUT')
_delete_snapshot = _Request('DELETE')
_rename_snapshot = _Request('PUT')
# Exposed endpoints
def resolve(self, hdfs_path):
"""Return absolute, normalized path, with special markers expanded.
:param hdfs_path: Remote path.
Currently supported markers:
* `'#LATEST'`: this marker gets expanded to the most recently updated file
or folder. They can be combined using the `'{N}'` suffix. For example,
`'foo/#LATEST{2}'` is equivalent to `'foo/#LATEST/#LATEST'`.
"""
path = hdfs_path
if not psp.isabs(path):
if not self.root or not psp.isabs(self.root):
root = self._get_home_directory('/').json()['Path']
self.root = psp.join(root, self.root) if self.root else root
_logger.debug('Updated root to %r.', self.root)
path = psp.join(self.root, path)
path = psp.normpath(path)
def expand_latest(match):
"""Substitute #LATEST marker."""
prefix = match.string[:match.start()]
suffix = ''
n = match.group(1) # n as in {N} syntax
for _ in repeat(None, int(n) if n else 1):
statuses = self._list_status(psp.join(prefix, suffix)).json()
candidates = sorted(
(-status['modificationTime'], status['pathSuffix'])
for status in statuses['FileStatuses']['FileStatus']
)
if not candidates:
raise HdfsError('Cannot expand #LATEST. %r is empty.', prefix)
elif len(candidates) == 1 and candidates[0][1] == '':
raise HdfsError('Cannot expand #LATEST. %r is a file.', prefix)
suffix = psp.join(suffix, candidates[0][1])
return '/' + suffix
path = re.sub(r'/?#LATEST(?:{(\d+)})?(?=/|$)', expand_latest, path)
# #LATEST expansion (could cache the pattern, but not worth it)
_logger.debug('Resolved path %r to %r.', hdfs_path, path)
return path
def content(self, hdfs_path, strict=True):
"""Get ContentSummary_ for a file or folder on HDFS.
:param hdfs_path: Remote path.
:param strict: If `False`, return `None` rather than raise an exception if
the path doesn't exist.
.. _ContentSummary: CS_
.. _CS: http://hadoop.apache.org/docs/r1.0.4/webhdfs.html#ContentSummary
"""
_logger.info('Fetching content summary for %r.', hdfs_path)
res = self._get_content_summary(hdfs_path, strict=strict)
return res.json()['ContentSummary'] if res else None
def status(self, hdfs_path, strict=True):
"""Get FileStatus_ for a file or folder on HDFS.
:param hdfs_path: Remote path.
:param strict: If `False`, return `None` rather than raise an exception if
the path doesn't exist.
.. _FileStatus: FS_
.. _FS: http://hadoop.apache.org/docs/r1.0.4/webhdfs.html#FileStatus
"""
_logger.info('Fetching status for %r.', hdfs_path)
res = self._get_file_status(hdfs_path, strict=strict)
return res.json()['FileStatus'] if res else None
def acl_status(self, hdfs_path, strict=True):
"""Get AclStatus_ for a file or folder on HDFS.
:param hdfs_path: Remote path.
:param strict: If `False`, return `None` rather than raise an exception if
the path doesn't exist.
.. _AclStatus: https://hadoop.apache.org/docs/stable2/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Get_ACL_Status
"""
_logger.info('Fetching ACL status for %r.', hdfs_path)
res = self._get_acl_status(hdfs_path, strict=strict)
return res.json()['AclStatus'] if res else None
def set_acl(self, hdfs_path, acl_spec, clear=True):
"""SetAcl_ or ModifyAcl_ for a file or folder on HDFS.
:param hdfs_path: Path to an existing remote file or directory. An
:class:`HdfsError` will be raised if the path doesn't exist.
:param acl_spec: String representation of an ACL spec. Must be a valid
string with entries for user, group and other. For example:
`"user::rwx,user:foo:rw-,group::r--,other::---"`.
:param clear: Clear existing ACL entries. If set to false, all existing ACL
entries that are not specified in this call are retained without changes,
behaving like ModifyAcl_. For example: `"user:foo:rwx"`.
.. _SetAcl: https://hadoop.apache.org/docs/stable2/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Set_ACL
.. _ModifyAcl: https://hadoop.apache.org/docs/stable2/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Modify_ACL_Entries
"""
if clear:
_logger.info('Setting ACL spec for %r to %r.', hdfs_path, acl_spec)
self._set_acl(hdfs_path, aclspec=acl_spec)
else:
_logger.info('Modifying ACL spec for %r to %r.', hdfs_path, acl_spec)
self._modify_acl_entries(hdfs_path, aclspec=acl_spec)
def remove_acl_entries(self, hdfs_path, acl_spec):
"""RemoveAclEntries_ for a file or folder on HDFS.
:param hdfs_path: Path to an existing remote file or directory. An
:class:`HdfsError` will be raised if the path doesn't exist.
:param acl_spec: String representation of an ACL spec. Must be a valid
string with entries for user, group and other. For example:
`"user::rwx,user:foo:rw-,group::r--,other::---"`.
.. _RemoveAclEntries: https://hadoop.apache.org/docs/stable2/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Remove_ACL_Entries
"""
_logger.info('Removing ACL spec on %r for %r.', hdfs_path, acl_spec)
self._remove_acl_entries(hdfs_path, aclspec=acl_spec)
def remove_default_acl(self, hdfs_path):
"""RemoveDefaultAcl_ for a file or folder on HDFS.
:param hdfs_path: Path to an existing remote file or directory. An
:class:`HdfsError` will be raised if the path doesn't exist.
.. _RemoveDefaultAcl: https://hadoop.apache.org/docs/stable2/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Remove_Default_ACL
"""
_logger.info('Removing default acl for %r', hdfs_path)
self._remove_default_acl(hdfs_path)
def remove_acl(self, hdfs_path):
"""RemoveAcl_ for a file or folder on HDFS.
:param hdfs_path: Path to an existing remote file or directory. An
:class:`HdfsError` will be raised if the path doesn't exist.
.. _RemoveAcl: https://hadoop.apache.org/docs/stable2/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Remove_ACL
"""
_logger.info('Removing all ACL for %r', hdfs_path)
self._remove_acl(hdfs_path)
def parts(self, hdfs_path, parts=None, status=False):
"""Returns a dictionary of part-files corresponding to a path.
:param hdfs_path: Remote path. This directory should contain at most one
part file per partition (otherwise one will be picked arbitrarily).
:param parts: List of part-files numbers or total number of part-files to
select. If a number, that many partitions will be chosen at random. By
default all part-files are returned. If `parts` is a list and one of the
parts is not found or too many samples are demanded, an
:class:`~hdfs.util.HdfsError` is raised.
:param status: Also return each file's corresponding FileStatus_.
"""
_logger.debug('Fetching parts for %r.', hdfs_path)
pattern = re.compile(r'^part-(?:(?:m|r)-|)(\d+)[^/]*$')
matches = (
(name, pattern.match(name), s)
for name, s in self.list(hdfs_path, status=True)
)
part_files = {
int(match.group(1)): (name, s)
for name, match, s in matches
if match
}
if not part_files:
raise HdfsError('No part-files found in %r.', hdfs_path)
_logger.debug('Found %s part-files for %r.', len(part_files), hdfs_path)
if parts:
if isinstance(parts, int):
_logger.debug('Choosing %s parts randomly.', parts)
if parts > len(part_files):
raise HdfsError('Not enough part-files in %r.', hdfs_path)
parts = sample(part_files, parts)
try:
infos = list(part_files[p] for p in parts)
except KeyError as err:
raise HdfsError('No part-file %r in %r.', err.args[0], hdfs_path)
_logger.info(
'Returning %s of %s part-files for %r: %s.', len(infos),
len(part_files), hdfs_path, ', '.join(name for name, _ in infos)
)
else:
infos = list(sorted(part_files.values()))
_logger.info('Returning all %s part-files at %r.', len(infos), hdfs_path)
return infos if status else [name for name, _ in infos]
def write(self, hdfs_path, data=None, overwrite=False, permission=None,
blocksize=None, replication=None, buffersize=None, append=False,
encoding=None):
"""Create a file on HDFS.
:param hdfs_path: Path where to create file. The necessary directories will
be created appropriately.
:param data: Contents of file to write. Can be a string, a generator or a
file object. The last two options will allow streaming upload (i.e.
without having to load the entire contents into memory). If `None`, this
method will return a file-like object and should be called using a `with`
block (see below for examples).
:param overwrite: Overwrite any existing file or directory.
:param permission: Octal permission to set on the newly created file.
Leading zeros may be omitted.
:param blocksize: Block size of the file.
:param replication: Number of replications of the file.
:param buffersize: Size of upload buffer.
:param append: Append to a file rather than create a new one.
:param encoding: Encoding used to serialize data written.
Sample usages:
.. code-block:: python
from json import dump, dumps
records = [
{'name': 'foo', 'weight': 1},
{'name': 'bar', 'weight': 2},
]
# As a context manager:
with client.write('data/records.jsonl', encoding='utf-8') as writer:
dump(records, writer)
# Or, passing in a generator directly:
client.write('data/records.jsonl', data=dumps(records), encoding='utf-8')
"""
# TODO: Figure out why this function generates a "Connection pool is full,
# discarding connection" warning when passed a generator.
if append:
if overwrite:
raise ValueError('Cannot both overwrite and append.')
if permission or blocksize or replication:
raise ValueError('Cannot change file properties while appending.')
_logger.info('Appending to %r.', hdfs_path)
res = self._append(hdfs_path, buffersize=buffersize)
else:
_logger.info('Writing to %r.', hdfs_path)
res = self._create(
hdfs_path,
overwrite=overwrite,
permission=permission,
blocksize=blocksize,
replication=replication,
buffersize=buffersize,
)
loc = res.headers['location']
def consumer(_data):
"""Thread target."""
res = self._request(
method='POST' if append else 'PUT',
url=loc,
data=(c.encode(encoding) for c in _data) if encoding else _data,
)
if not res:
raise _to_error(res)
if data is None:
return AsyncWriter(consumer)
else:
consumer(data)
def upload(self, hdfs_path, local_path, n_threads=1, temp_dir=None,
chunk_size=2 ** 16, progress=None, cleanup=True, **kwargs):
r"""Upload a file or directory to HDFS.
:param hdfs_path: Target HDFS path. If it already exists and is a
directory, files will be uploaded inside.
:param local_path: Local path to file or folder. If a folder, all the files
inside of it will be uploaded (note that this implies that folders empty
of files will not be created remotely).
:param n_threads: Number of threads to use for parallelization. A value of
`0` (or negative) uses as many threads as there are files.
:param temp_dir: Directory under which the files will first be uploaded
when `overwrite=True` and the final remote path already exists. Once the
upload successfully completes, it will be swapped in.
:param chunk_size: Interval in bytes by which the files will be uploaded.
:param progress: Callback function to track progress, called every
`chunk_size` bytes. It will be passed two arguments, the path to the
file being uploaded and the number of bytes transferred so far. On
completion, it will be called once with `-1` as second argument.
:param cleanup: Delete any uploaded files if an error occurs during the
upload.
:param \*\*kwargs: Keyword arguments forwarded to :meth:`write`. In
particular, set `overwrite` to overwrite any existing file or directory.
On success, this method returns the remote upload path.
"""
if chunk_size <= 0:
raise ValueError('Upload chunk size must be positive.')
_logger.info('Uploading %r to %r.', local_path, hdfs_path)
def _upload(_path_tuple):
"""Upload a single file."""
_local_path, _temp_path = _path_tuple
_logger.debug('Uploading %r to %r.', _local_path, _temp_path)
def wrap(_reader, _chunk_size, _progress):
"""Generator that can track progress."""
nbytes = 0
while True:
chunk = _reader.read(_chunk_size)
if chunk:
if _progress:
nbytes += len(chunk)
_progress(_local_path, nbytes)
yield chunk
else:
break
if _progress:
_progress(_local_path, -1)
with open(_local_path, 'rb') as reader:
self.write(_temp_path, wrap(reader, chunk_size, progress), **kwargs)
# First, we gather information about remote paths.
hdfs_path = self.resolve(hdfs_path)
temp_path = None
try:
statuses = [status for _, status in self.list(hdfs_path, status=True)]
except HdfsError as err:
if 'not a directory' in err.message:
# Remote path is a normal file.
if not kwargs.get('overwrite'):
raise HdfsError('Remote path %r already exists.', hdfs_path)
elif 'does not exist' in err.message:
# Remote path doesn't exist.
temp_path = hdfs_path
else:
# An unexpected error occurred.
raise err
else:
# Remote path is a directory.
suffixes = {status['pathSuffix'] for status in statuses}
local_name = osp.basename(local_path)
hdfs_path = psp.join(hdfs_path, local_name)
if local_name in suffixes:
if not kwargs.get('overwrite'):
raise HdfsError('Remote path %r already exists.', hdfs_path)
else:
temp_path = hdfs_path
if not temp_path:
# The remote path already exists, we need to generate a temporary one.
remote_dpath, remote_name = psp.split(hdfs_path)
temp_dir = temp_dir or remote_dpath
temp_path = psp.join(
temp_dir,
'{}.temp-{}'.format(remote_name, _current_micros())
)
_logger.debug(
'Upload destination %r already exists. Using temporary path %r.',
hdfs_path, temp_path
)
# Then we figure out which files we need to upload, and where.
if osp.isdir(local_path):
local_fpaths = [
osp.join(dpath, fpath)
for dpath, _, fpaths in os.walk(local_path)
for fpath in fpaths
]
if not local_fpaths:
raise HdfsError('No files to upload found inside %r.', local_path)
offset = len(local_path.rstrip(os.sep)) + len(os.sep)
fpath_tuples = [
(fpath, psp.join(temp_path, fpath[offset:].replace(os.sep, '/')))
for fpath in local_fpaths
]
elif osp.exists(local_path):
fpath_tuples = [(local_path, temp_path)]
else:
raise HdfsError('Local path %r does not exist.', local_path)
# Finally, we upload all files (optionally, in parallel).
if n_threads <= 0:
n_threads = len(fpath_tuples)
else:
n_threads = min(n_threads, len(fpath_tuples))
_logger.debug(
'Uploading %s files using %s thread(s).', len(fpath_tuples), n_threads
)
try:
if n_threads == 1:
for path_tuple in fpath_tuples:
_upload(path_tuple)
else:
_map_async(n_threads, _upload, fpath_tuples)
except Exception as err: # pylint: disable=broad-except
if cleanup:
_logger.exception('Error while uploading. Attempting cleanup.')
try:
self.delete(temp_path, recursive=True)
except Exception:
_logger.error('Unable to cleanup temporary folder.')
finally:
raise err
else:
raise err
else:
if temp_path != hdfs_path:
_logger.debug(
'Upload of %r complete. Moving from %r to %r.',
local_path, temp_path, hdfs_path
)
self.delete(hdfs_path, recursive=True)
self.rename(temp_path, hdfs_path)
else:
_logger.debug(
'Upload of %r to %r complete.', local_path, hdfs_path
)
return hdfs_path
@contextmanager
def read(self, hdfs_path, offset=0, length=None, buffer_size=None,
encoding=None, chunk_size=0, delimiter=None, progress=None):
"""Read a file from HDFS.
:param hdfs_path: HDFS path.
:param offset: Starting byte position.
:param length: Number of bytes to be processed. `None` will read the entire
file.
:param buffer_size: Size of the buffer in bytes used for transferring the
data. Defaults the the value set in the HDFS configuration.
:param encoding: Encoding used to decode the request. By default the raw
data is returned. This is mostly helpful in python 3, for example to
deserialize JSON data (as the decoder expects unicode).
:param chunk_size: If set to a positive number, the context manager will
return a generator yielding every `chunk_size` bytes instead of a
file-like object (unless `delimiter` is also set, see below).
:param delimiter: If set, the context manager will return a generator
yielding each time the delimiter is encountered. This parameter requires
the `encoding` to be specified.
:param progress: Callback function to track progress, called every
`chunk_size` bytes (not available if the chunk size isn't specified). It
will be passed two arguments, the path to the file being uploaded and the
number of bytes transferred so far. On completion, it will be called once
with `-1` as second argument.
This method must be called using a `with` block:
.. code-block:: python
with client.read('foo') as reader:
content = reader.read()
This ensures that connections are always properly closed.
.. note::
The raw file-like object returned by this method (when called without an
encoding, chunk size, or delimiter) can have a very different performance
profile than local files. In particular, line-oriented methods are often
slower. The recommended workaround is to specify an encoding when
possible or read the entire file before splitting it.
"""
if chunk_size < 0:
raise ValueError('Read chunk size must be non-negative.')
if progress and not chunk_size:
raise ValueError('Progress callback requires a positive chunk size.')
if delimiter:
if not encoding:
raise ValueError('Delimiter splitting requires an encoding.')
if chunk_size:
raise ValueError('Delimiter splitting incompatible with chunk size.')
_logger.info('Reading file %r.', hdfs_path)
res = self._open(
hdfs_path,
offset=offset,
length=length,
buffersize=buffer_size,
)
try:
if not chunk_size and not delimiter:
yield codecs.getreader(encoding)(res.raw) if encoding else res.raw
else:
# Patch in encoding on the response object so that `iter_content` and
# `iter_lines` can pick it up. If `None`, it is ignored and no decoding
# happens (which is why we can always set `decode_unicode=True`).
res.encoding = encoding
if delimiter:
data = res.iter_lines(delimiter=delimiter, decode_unicode=True)
else:
data = res.iter_content(chunk_size=chunk_size, decode_unicode=True)
if progress:
def reader(_hdfs_path, _progress):
"""Generator that tracks progress."""
nbytes = 0
for chunk in data:
nbytes += len(chunk)
_progress(_hdfs_path, nbytes)
yield chunk
_progress(_hdfs_path, -1)
yield reader(hdfs_path, progress)
else:
yield data
finally:
res.close()
_logger.debug('Closed response for reading file %r.', hdfs_path)
def download(self, hdfs_path, local_path, overwrite=False, n_threads=1,
temp_dir=None, **kwargs):
r"""Download a file or folder from HDFS and save it locally.
:param hdfs_path: Path on HDFS of the file or folder to download. If a
folder, all the files under it will be downloaded.
:param local_path: Local path. If it already exists and is a directory,
the files will be downloaded inside of it.
:param overwrite: Overwrite any existing file or directory.
:param n_threads: Number of threads to use for parallelization. A value of
`0` (or negative) uses as many threads as there are files.
:param temp_dir: Directory under which the files will first be downloaded
when `overwrite=True` and the final destination path already exists. Once
the download successfully completes, it will be swapped in.
:param \*\*kwargs: Keyword arguments forwarded to :meth:`read`. If no
`chunk_size` argument is passed, a default value of 64 kB will be used.
If a `progress` argument is passed and threading is used, care must be
taken to ensure correct behavior.
On success, this method returns the local download path.
"""
_logger.info('Downloading %r to %r.', hdfs_path, local_path)
kwargs.setdefault('chunk_size', 2 ** 16)
lock = Lock()
def _download(_path_tuple):
"""Download a single file."""
_remote_path, _temp_path = _path_tuple
_logger.debug('Downloading %r to %r.', _remote_path, _temp_path)
_dpath = osp.dirname(_temp_path)
with lock:
# Prevent race condition when using multiple threads.
if not osp.exists(_dpath):
os.makedirs(_dpath)
with open(_temp_path, 'wb') as _writer:
with self.read(_remote_path, **kwargs) as reader:
for chunk in reader:
_writer.write(chunk)
# First, we figure out where we will download the files to.
hdfs_path = self.resolve(hdfs_path)
local_path = osp.realpath(local_path)
if osp.isdir(local_path):
local_path = osp.join(local_path, psp.basename(hdfs_path))
if osp.exists(local_path):
if not overwrite:
raise HdfsError('Path %r already exists.', local_path)
local_dpath, local_name = osp.split(local_path)
temp_dir = temp_dir or local_dpath
temp_path = osp.join(
temp_dir,
'{}.temp-{}'.format(local_name, _current_micros())
)
_logger.debug(
'Download destination %r already exists. Using temporary path %r.',
local_path, temp_path
)
else:
if not osp.isdir(osp.dirname(local_path)):
raise HdfsError('Parent directory of %r does not exist.', local_path)
temp_path = local_path
# Then we figure out which files we need to download and where.
remote_paths = list(self.walk(hdfs_path, depth=0, status=False))
if not remote_paths:
# This is a single file.
remote_fpaths = [hdfs_path]
else:
remote_fpaths = [
psp.join(dpath, fname)
for dpath, _, fnames in remote_paths
for fname in fnames
]
if not remote_fpaths:
raise HdfsError('No files to download found inside %r.', hdfs_path)
offset = len(hdfs_path) + 1 # Prefix length.
fpath_tuples = [
(
fpath,
osp.join(temp_path, fpath[offset:].replace('/', os.sep)).rstrip(os.sep)
)
for fpath in remote_fpaths
]
# Finally, we download all of them.
if n_threads <= 0:
n_threads = len(fpath_tuples)
else:
n_threads = min(n_threads, len(fpath_tuples))
_logger.debug(
'Downloading %s files using %s thread(s).', len(fpath_tuples), n_threads
)
try:
if n_threads == 1:
for fpath_tuple in fpath_tuples:
_download(fpath_tuple)
else:
_map_async(n_threads, _download, fpath_tuples)
except Exception as err: # pylint: disable=broad-except
_logger.exception('Error while downloading. Attempting cleanup.')
try:
if osp.isdir(temp_path):
rmtree(temp_path)
else:
os.remove(temp_path)
except Exception:
_logger.error('Unable to cleanup temporary folder.')
finally:
raise err
else:
if temp_path != local_path:
_logger.debug(
'Download of %r complete. Moving from %r to %r.',
hdfs_path, temp_path, local_path
)
if osp.isdir(local_path):
rmtree(local_path)
else:
os.remove(local_path)
move(temp_path, local_path)
else:
_logger.debug(
'Download of %s to %r complete.', hdfs_path, local_path
)
return local_path
def delete(self, hdfs_path, recursive=False, skip_trash=True):
"""Remove a file or directory from HDFS.
:param hdfs_path: HDFS path.
:param recursive: Recursively delete files and directories. By default,
this method will raise an :class:`HdfsError` if trying to delete a
non-empty directory.
:param skip_trash: When false, the deleted path will be moved to an
appropriate trash folder rather than deleted. This requires Hadoop 2.9+
and trash to be enabled on the cluster.
This function returns `True` if the deletion was successful and `False` if
no file or directory previously existed at `hdfs_path`.
"""
verb = 'Deleting' if skip_trash else 'Trashing'
_logger.info(
'%s %r%s.', verb, hdfs_path, ' recursively' if recursive else ''
)
if skip_trash:
return self._delete(hdfs_path, recursive=recursive).json()['boolean']
hdfs_path = self.resolve(hdfs_path)
status = self.status(hdfs_path, strict=False)
if not status:
return False
if status['type'] == 'DIRECTORY' and not recursive:
raise HdfsError('Non-recursive trashing of directory %r.', hdfs_path)
_logger.info('Fetching trash root for %r.', hdfs_path)
trash_path = self._get_trash_root(hdfs_path).json()['Path']
# The default trash policy (http://mtth.xyz/_9lc9t3hjtz276rx) expects
# folders to be under a `"Current"` subfolder. We also add a timestamped
# folder as a simple safeguard against path conflicts (note that the above
# policy implements a more involved variant).
dst_path = psp.join(trash_path, 'Current', _current_micros())
self.makedirs(dst_path)
# Note that there is a (hopefully small) race condition here: the path might
# have been deleted between the status call above and the rename here.
self.rename(hdfs_path, dst_path)
_logger.info('%r moved to trash at %r.', hdfs_path, dst_path)
return True
def rename(self, hdfs_src_path, hdfs_dst_path, overwrite=False):
"""Move a file or folder.
:param hdfs_src_path: Source path.
:param hdfs_dst_path: Destination path. If the path already exists and is
a directory, the source will be moved into it. If the path exists and is
a file, or if a parent destination directory is missing, this method will
raise an :class:`HdfsError`.
:param overwrite: overwrite the hdfs_dst_path if it already exists, rather
than raising an :class:`HdfsError`.
"""
_logger.info('Renaming %r to %r.', hdfs_src_path, hdfs_dst_path)
hdfs_dst_path = self.resolve(hdfs_dst_path)
kwargs = {"renameoptions": "overwrite"} if overwrite else {}
res = self._rename(hdfs_src_path, destination=hdfs_dst_path, **kwargs)
if not res.json()['boolean']:
raise HdfsError(
'Unable to rename %r to %r.',
self.resolve(hdfs_src_path), hdfs_dst_path
)
def set_owner(self, hdfs_path, owner=None, group=None):
"""Change the owner of file.
:param hdfs_path: HDFS path.
:param owner: Optional, new owner for file.
:param group: Optional, new group for file.
At least one of `owner` and `group` must be specified.
"""
if not owner and not group:
raise ValueError('Must set at least one of owner or group.')
messages = []
if owner:
messages.append('owner to {!r}'.format(owner))
if group:
messages.append('group to {!r}'.format(group))
_logger.info('Changing %s of %r.', ', and'.join(messages), hdfs_path)
self._set_owner(hdfs_path, owner=owner, group=group)
def set_permission(self, hdfs_path, permission):
"""Change the permissions of file.
:param hdfs_path: HDFS path.
:param permission: New octal permissions string of file.
"""
_logger.info(
'Changing permissions of %r to %r.', hdfs_path, permission
)
self._set_permission(hdfs_path, permission=permission)
def set_times(self, hdfs_path, access_time=None, modification_time=None):
"""Change remote timestamps.
:param hdfs_path: HDFS path.
:param access_time: Timestamp of last file access.
:param modification_time: Timestamps of last file access.
"""
if not access_time and not modification_time:
raise ValueError('At least one of time must be specified.')
msgs = []
if access_time:
msgs.append('access time to {!r}'.format(access_time))
if modification_time:
msgs.append('modification time to {!r}'.format(modification_time))
_logger.info('Updating %s of %r.', ' and '.join(msgs), hdfs_path)
self._set_times(
hdfs_path,
accesstime=access_time,
modificationtime=modification_time,