-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_hdfs_client.py
248 lines (179 loc) · 6.2 KB
/
web_hdfs_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
"""
WebHDFS Client for Stand-Alone Hadoop System (Runned on the same machine)
Property dfs.webhdfs.enabled in hdfs-site.xml must be true
conf.json incapsulates all cofiguration data such as ip, port, user
Author : Sinev Max
"""
import requests
import json
import sys
import os
class WebHDFSClient:
"""
Reading data from config file and creating some constant variables
"""
def __init__(self):
with open('conf.json') as json_file:
self.CONFIG = json.load(json_file)
self.HOST = self.CONFIG["HOST"]
self.PORT = self.CONFIG["PORT"]
self.USER = self.CONFIG["USER"]
self.DIRECTORY_ROOT = "webhdfs/v1/"
self.current_directory = self.DIRECTORY_ROOT
"""
Creating infinite loop where user commands are readed
"""
def run(self):
_command = ''
while _command != "exit":
_command = input(f"{self.USER}:~/{self.current_directory}$ ")
splitted_command = _command.split(" ")
command = splitted_command[0]
args = {}
if len(splitted_command) > 1:
args["file"] = splitted_command[1]
args["directory"] = splitted_command[1]
if len(splitted_command) > 2:
args["options"] = splitted_command[2:]
if command == "ls":
self.ls(args)
if command == "rm":
self.rm(args)
if command == "cd":
self.cd(args)
if command == "mkdir":
self.mkdir(args)
if command == "put":
self.put(args)
if command == "get":
self.get(args)
if command == "lls":
self.lls(args)
if command == "lcd":
self.lcd(args)
"""
Get all files/directories in current directory in HDFS
"""
def ls(self, args):
params = {
'user.name': self.USER,
'op': "LISTSTATUS"
}
url = f"http://{self.HOST}:{self.PORT}/{self.current_directory}"
response = requests.get(url, params=params)
_data = json.loads(response.text)["FileStatuses"]["FileStatus"]
if len(_data) > 0:
for file in _data:
print(file["pathSuffix"])
"""
Change directory in HDFS
"""
def cd(self, args):
params = {
'user.name': self.USER,
'op': "LISTSTATUS"
}
_directories = args["directory"].strip("/")
directories = _directories.split("/")
for directory in directories:
if directory == "..":
directories_list = self.current_directory.split('/')
if directories_list[-2] == "v1":
break
self.current_directory = "/".join(
directories_list[0:-2]) + "/"
flag = True
url = f"http://{self.HOST}:{self.PORT}/{self.current_directory}"
response = requests.get(url, params=params)
_data = json.loads(response.text)[
"FileStatuses"]["FileStatus"] if response.status_code != 404 else []
flag = True
if len(_data) > 0:
for data in _data:
if data["pathSuffix"] == directory and data["type"] == "DIRECTORY":
self.current_directory = self.current_directory + directory + "/"
flag = False
if flag:
print("No such directory")
"""
Create directory in current directory in HDFS
"""
def mkdir(self, args):
params = {
'user.name': self.USER,
'op': "MKDIRS"
}
url = f"http://{self.HOST}:{self.PORT}/{self.current_directory}{args['directory'].strip('/')}"
requests.put(url, params=params)
"""
Create file in current directory in HDFS
"""
def put(self, args):
params = {
'user.name': self.USER,
'op': "CREATE"
}
url = f"http://{self.HOST}:{self.PORT}/{self.current_directory}{args['file']}"
print(url)
response = requests.put(url, params=params)
if response.status_code == 201:
self.append(args=args)
"""
Download file from current directory in HDFS
"""
def get(self, args):
params = {
'user.name': self.USER,
'op': "OPEN"
}
# self.current_directory = self.DIRECTORY_ROOT + self.current_directory
url = f"http://{self.HOST}:{self.PORT}/{self.current_directory}{args['file']}"
print(url)
response = requests.get(url, params=params)
if response.status_code == 200:
file = open(args['file'], "w")
file.write(response.text)
file.close()
"""
Appending data to created file in HDFS
"""
def append(self, args):
params = {
'user.name': self.USER,
'op': "APPEND"
}
url = f"http://{self.HOST}:{self.PORT}/{self.current_directory}{args['file']}"
files = {'upload_file': open(args['file'], 'rb')}
request = requests.post(url, files=files, params=params, timeout=5)
"""
Remove file/directory from current directory in HDFS
"""
def rm(self, args):
params = {
'user.name': self.USER,
'op': "DELETE"
}
# self.current_directory = self.DIRECTORY_ROOT + self.current_directory
url = f"http://{self.HOST}:{self.PORT}/{self.current_directory}{args['file']}"
response = requests.delete(url, params=params)
"""
List of files/directories in current local directory
"""
def lls(self, args):
local_directories = os.listdir(os.getcwd())
for file in local_directories:
print(file)
"""
Change local directory
"""
def lcd(self, args):
try:
os.chdir(args["directory"])
except FileNotFoundError:
print(f"> Error: {sys.exc_info()}")
if __name__ == "__main__":
"""
Initalizing WebHDFS Client
"""
web_hdfs_client = WebHDFSClient()
web_hdfs_client.run()