-
Notifications
You must be signed in to change notification settings - Fork 0
/
webHDFS.xsjslib
150 lines (95 loc) · 3.92 KB
/
webHDFS.xsjslib
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
//create client
var client = new $.net.http.Client();
// Use webHDFS destination defined in webHDFS.xshttpdest [Name Node]
var destName = "webHDFS";
var destNameNode = $.net.http.readDestination("zzzzzzzzztrial.trial.HadoopHbase1.webHDFS",destName);
//Use webHDFS destination defined in webHDFSdata.xshttpdest [Data Node]
var destData = "webHDFSdata";
var destDataNode = $.net.http.readDestination("zzzzzzzzztrial.trial.HadoopHbase1.webHDFS",destData);
//Next need to build up the url string expected by webHDFS REST service
var hBaseUrl;
var request;
var response;
//get all the cookies and headers from the response
var co = [], he = [];
//get the body of the Response from webHDFS
var body = undefined;
var data;
function GetDirList (path) {
//Currently hard coding DIRECTORY LISTING, with HADOOP user name etc
var webHDFSUrl = path + "?op=LISTSTATUS&user.name=hue&doas=admin" ;
request = new $.web.WebRequest($.net.http.GET, webHDFSUrl );
request.headers.set("Accept", "application/json");
// send the request and synchronously get the response
client.request(request, destNameNode);
response = client.getResponse();
splitRepsonse(response);
var objBody;
objBody = JSON.parse(body);
return {"webHDFSUrl" : hBaseUrl, "status": response.status, "cookies": co, "headers": he, "body": objBody } ;
}
function GetDownload(path) {
//Currently hard coding DOWNLOAD, with HADOOP user name etc
var webHDFSUrl = path + "?op=OPEN&user.name=hue&doas=admin" ;
request = new $.web.WebRequest($.net.http.GET, webHDFSUrl );
request.headers.set("Content-Type", "application/octet-stream");
// send the request and synchronously get the response
client.request(request, destNameNode);
response = client.getResponse();
var objBody;
var location;
for(var c in response.headers) {
if (response.headers[c].name == "location" ) {
objBody = response.headers[c].value;
location = response.headers[c].value.substring(response.headers[c].value.indexOf(":50075")+6)
}
}
request = new $.web.WebRequest($.net.http.GET, location );
request.headers.set("Content-Type", "application/octet-stream");
// send the request and synchronously get the response
client.request(request, destDataNode);
response = client.getResponse();
//split response into components cookies, header and body
splitRepsonse(response);
return {"webHDFSUrl" : hBaseUrl, "status": response.status, "cookies": co, "headers": he, "body": body } ; //objBody
}
function PutCreate(path, data) {
//explecting to create plain TEXT
var webHDFSUrl = path + "?op=CREATE&user.name=hue&doas=admin&overwrite=true" ;
request = new $.web.WebRequest($.net.http.PUT, webHDFSUrl );
request.headers.set("Content-Type", "text/plain");
// send the request and synchronously get the response
client.request(request, destNameNode);
response = client.getResponse();
var objBody;
var location;
for(var c in response.headers) {
if (response.headers[c].name == "location" ) {
objBody = response.headers[c].value;
location = response.headers[c].value.substring(response.headers[c].value.indexOf(":50075")+6)
}
}
request = new $.web.WebRequest($.net.http.PUT, location );
request.headers.set("Content-Type", "text/plain");
request.setBody(data);
client.request(request, destDataNode);
response = client.getResponse();
splitRepsonse(response);
return {"webHDFSUrl" : hBaseUrl, "status": response.status, "cookies": co, "headers": he, "body": body } ; //objBody
//return JSON.stringify(response);
}
//Function to split response into components: cookies, header and body
function splitRepsonse(response) {
//get all the cookies and headers from the response
for(var c in response.cookies) {
co.push(response.cookies[c]);
}
for(var c in response.headers) {
he.push(response.headers[c]);
}
// get the body of the Response from Hbase
if(!response.body)
body = "EMPTY";
else
body = response.body.asString();
}