-
Notifications
You must be signed in to change notification settings - Fork 44
/
hdfs.py
163 lines (131 loc) · 5.43 KB
/
hdfs.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
from __future__ import absolute_import, unicode_literals
from fnmatch import fnmatch
import logging
from io import BytesIO, StringIO
from ...exceptions import FileSystemNotSupported
from ...utils import parse_file_uri, format_file_uri
from .file_system import FileSystem
log = logging.getLogger(__name__)
try:
import hdfs
except ImportError:
hdfs = None
class Hdfs(FileSystem):
""":class:`.FileSystem` implementation for HDFS."""
_conn = {}
def __init__(self, file_name):
if hdfs is None:
raise FileSystemNotSupported(
'hdfs not supported. Install the python package "hdfs".'
)
super(Hdfs, self).__init__(file_name)
@staticmethod
def client_and_path(path):
_, domain, folder_path, file_pattern = parse_file_uri(path)
if ':' not in domain:
port = 50070
else:
domain, port = domain.split(':')
port = int(port)
cache_id = domain + '__' + str(port)
if cache_id not in Hdfs._conn:
if hdfs is None:
raise FileSystemNotSupported(
'hdfs not supported. Install the python package "hdfs".'
)
Hdfs._conn[cache_id] = hdfs.InsecureClient( # pylint: disable=no-member
'http://{0}:{1}'.format(domain, port)
)
return Hdfs._conn[cache_id], folder_path + file_pattern
def exists(self):
c, p = Hdfs.client_and_path(self.file_name)
try:
c.status(p)
except hdfs.util.HdfsError: # pylint: disable=no-member
return False
return True
@staticmethod
def resolve_filenames(expr):
c, _ = Hdfs.client_and_path(expr)
scheme, domain, folder_path, _ = parse_file_uri(expr)
files = []
for fn, file_status in c.list(folder_path, status=True):
file_local_path = '{0}{1}'.format(folder_path, fn)
file_path = format_file_uri(scheme, domain, file_local_path)
part_file_expr = expr + ("" if expr.endswith("/") else "/") + 'part*'
if fnmatch(file_path, expr):
if file_status["type"] != "DIRECTORY":
files.append(file_path)
else:
files += Hdfs._get_folder_part_files(
c,
scheme,
domain,
file_local_path,
part_file_expr
)
elif fnmatch(file_path, part_file_expr):
files.append(file_path)
return files
@staticmethod
def _get_folder_part_files(c, scheme, domain, folder_local_path, expr_with_part):
files = []
for fn, file_status in c.list(folder_local_path, status=True):
sub_file_path = format_file_uri(scheme, domain, folder_local_path, fn)
if fnmatch(sub_file_path, expr_with_part) and file_status["type"] != "DIRECTORY":
files.append(sub_file_path)
return files
@classmethod
def _get_folder_files_by_expr(cls, c, scheme, domain, folder_path, expr=None):
"""
Using client c, retrieves all files located in the folder `folder_path` that matches `expr`
:param c: An HDFS client
:param scheme: a scheme such as hdfs
:param domain: a DFS web server
:param folder_path: a folder path without patterns
:param expr: a pattern
:return: list of matching files absolute paths prefixed with the scheme and domain
"""
file_paths = []
for fn, file_status in c.list(folder_path, status=True):
file_local_path = '{0}{1}'.format(folder_path, fn)
if expr is None or fnmatch(file_local_path, expr):
if file_status["type"] == "DIRECTORY":
file_paths += cls._get_folder_files_by_expr(
c,
scheme,
domain,
file_local_path + "/",
expr=None
)
else:
file_path = format_file_uri(scheme, domain, file_local_path)
file_paths.append(file_path)
elif file_status["type"] == "DIRECTORY":
file_paths += cls._get_folder_files_by_expr(
c, scheme, domain, file_local_path + "/", expr
)
return file_paths
@classmethod
def resolve_content(cls, expr):
c, _ = cls.client_and_path(expr)
scheme, domain, folder_path, pattern = parse_file_uri(expr)
expr = folder_path + pattern
return cls._get_folder_files_by_expr(c, scheme, domain, folder_path, expr)
def load(self):
log.debug('Hdfs read for {0}.'.format(self.file_name))
c, path = Hdfs.client_and_path(self.file_name)
with c.read(path) as reader:
r = BytesIO(reader.read())
return r
def load_text(self, encoding='utf8', encoding_errors='ignore'):
log.debug('Hdfs text read for {0}.'.format(self.file_name))
c, path = Hdfs.client_and_path(self.file_name)
with c.read(path) as reader:
r = StringIO(reader.read().decode(encoding, encoding_errors))
return r
def dump(self, stream):
log.debug('Dump to {0} with hdfs write.'.format(self.file_name))
c, path = Hdfs.client_and_path(self.file_name)
c.write(path, stream)
return self