forked from sanketmarkan/HDFS-MapReduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
250 lines (215 loc) · 8.42 KB
/
Client.java
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
import java.io.*;
import java.util.*;
import Protobuf.HDFS.*;
import Utils.*;
import IDataNode.*;
import INameNode.*;
import com.google.protobuf.ByteString;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class Client {
private static INameNode nameNode;
public Client() {
try {
Registry registry = LocateRegistry.getRegistry(/*Constants.nnIp,Constants.nnPort*/);
nameNode = (INameNode) registry.lookup("namenode");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
if (nameNode == null) {
try {
Registry registry = LocateRegistry.getRegistry(/*Constants.nnIp,Constants.nnPort*/);
nameNode = (INameNode) registry.lookup("namenode");
} catch (Exception e) {
e.printStackTrace();
}
}
Scanner in = new Scanner(System.in);
String line = "";
String command = "";
String fileName = "";
String outFile = "";
while (true) {
line = in.nextLine();
String[] arguments = line.trim().split("\\s+");
try {
command = arguments[0];
fileName = arguments[1];
// outFile =
} catch (Exception e) {
if (command.equals("get") || command.equals("put")) {
System.out.println("<command> <filename>");
continue;
}
}
//System.out.println(command);
//System.out.println(fileName);
if (command.equals("get")) {
get_file(fileName);
} else if (command.equals("put")) {
outFile = arguments[2];
String data = getFileContent(fileName);
put_file(outFile, data);
} else if (command.equals("list")) {
list_files();
} else if (command.equals("debug")) {
debug();
}
}
}
public static String get_file(String fileName) {
String fileContent = "";
System.out.println(fileName);
OpenFileRequest.Builder openFileRequest = OpenFileRequest.newBuilder();
openFileRequest.setFileName(fileName);
openFileRequest.setForRead(true);
try {
byte[] oFileRespose = nameNode.openFile(Utils.serialize(openFileRequest.build()));
OpenFileResponse openFileResponse = (OpenFileResponse) Utils.deserialize(oFileRespose);
List<Integer> blockList = openFileResponse.getBlockNumsList();
BlockLocationRequest.Builder blockLocationRequest = BlockLocationRequest.newBuilder();
for (Integer block : blockList) {
blockLocationRequest.addBlockNums(block);
System.out.println(block);
}
byte[] bLocationResponse = nameNode.getBlockLocations(Utils.serialize(blockLocationRequest.build()));
BlockLocationResponse blockLocationResponse = (BlockLocationResponse) Utils.deserialize(bLocationResponse);
int locationStatus = blockLocationResponse.getStatus();
System.out.println("block location status: " + locationStatus);
List<BlockLocations> blockLocations = blockLocationResponse.getBlockLocationsList();
for (BlockLocations location : blockLocations) {
List<DataNodeLocation> dataNodeLocationList = location.getLocationsList();
for(DataNodeLocation dataNodeLocation : dataNodeLocationList) {
String dnIP = dataNodeLocation.getIp();
int dnPort = dataNodeLocation.getPort();
int blockNumber = location.getBlockNumber();
Registry registry = LocateRegistry.getRegistry(dnIP,dnPort);
IDataNode dataNode = (IDataNode) registry.lookup("datanode");
ReadBlockRequest.Builder readBlockRequest = ReadBlockRequest.newBuilder();
readBlockRequest.setBlockNumber(blockNumber);
byte[] rBlockResponse = dataNode.readBlock(Utils.serialize(readBlockRequest.build()));
ReadBlockResponse readBlockResponse = (ReadBlockResponse) Utils.deserialize(rBlockResponse);
int readBlockStatus = readBlockResponse.getStatus();
if(readBlockStatus == 1) {
List<ByteString> data = readBlockResponse.getDataList();
for (ByteString str : data) {
fileContent += str.toStringUtf8();
System.out.println(str.toStringUtf8());
}
break;
}
}
}
int status = openFileResponse.getStatus();
int handle = openFileResponse.getHandle();
} catch (Exception e) {
e.printStackTrace();
}
return fileContent;
}
public static void put_file(String fileName, String data) {
//System.out.println(data);
int status;
int fileHandle = -1;
System.out.println("Opening file...");
OpenFileRequest.Builder openFileRequest = OpenFileRequest.newBuilder();
openFileRequest.setFileName(fileName);
openFileRequest.setForRead(false);
int loop = ((data.length())/(1024)) + 1;
int lastIdx = 0;
System.out.println(loop);
try {
byte[] oFileRespose = nameNode.openFile(Utils.serialize(openFileRequest.build()));
OpenFileResponse openFileResponse = (OpenFileResponse) Utils.deserialize(oFileRespose);
List<Integer> blockList = openFileResponse.getBlockNumsList();
status = openFileResponse.getStatus();
fileHandle = openFileResponse.getHandle();
System.out.println("file open status: " + status);
System.out.println("file handle: " + fileHandle);
//nameNode.test();
for (int i=0; i<loop; i++) {
String sub;
if (i == loop-1)
sub = data.substring(lastIdx);
else
sub = data.substring(lastIdx, lastIdx+(1024));
System.out.println(sub);
AssignBlockRequest.Builder assignBlockRequest = AssignBlockRequest.newBuilder();
assignBlockRequest.setHandle(fileHandle);
byte[] aBlockResponse = nameNode.assignBlock(Utils.serialize(assignBlockRequest.build()));
AssignBlockResponse assignBlockResponse = (AssignBlockResponse) Utils.deserialize(aBlockResponse);
int assignBlockStatus = assignBlockResponse.getStatus();
BlockLocations blockLocations = assignBlockResponse.getNewBlock();
System.out.println("block assign status: " + assignBlockStatus);
List<DataNodeLocation> dataNodeLocationList = blockLocations.getLocationsList();
// System.out.println(dataNodeLocationList.)
for(DataNodeLocation dataNodeLocation : dataNodeLocationList) {
String dnIP = dataNodeLocation.getIp();
int dnPort = dataNodeLocation.getPort();
int blockNumber = blockLocations.getBlockNumber();
Registry registry = LocateRegistry.getRegistry(dnIP,dnPort);
IDataNode dataNode = (IDataNode) registry.lookup("datanode");
WriteBlockRequest.Builder writeBlockRequest = WriteBlockRequest.newBuilder();
writeBlockRequest.setBlockNumber(blockNumber);
writeBlockRequest.addData(ByteString.copyFromUtf8(sub));
byte[] wBlockResponse = dataNode.writeBlock(Utils.serialize(writeBlockRequest.build()));
WriteBlockResponse writeBlockResponse = (WriteBlockResponse) Utils.deserialize(wBlockResponse);
int writeBlockStatus = writeBlockResponse.getStatus();
System.out.println("block write status: " + writeBlockStatus);
}
}} catch (Exception e) {
e.printStackTrace();
}
try {
if (fileHandle != -1) {
System.out.println("Closing file...");
CloseFileRequest.Builder closeFileRequest = CloseFileRequest.newBuilder();
closeFileRequest.setHandle(fileHandle);
byte[] cFileRequest = nameNode.closeFile(Utils.serialize(closeFileRequest.build()));
CloseFileResponse closeFileResponse = (CloseFileResponse) Utils.deserialize(cFileRequest);
int closeStatus = closeFileResponse.getStatus();
System.out.println("close file status: " + closeStatus);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void list_files() {
try {
ListFilesRequest.Builder listFilesRequest = ListFilesRequest.newBuilder();
// listFilesRequest.setDirName(".");
byte[] lFilesResponse = nameNode.list(Utils.serialize(listFilesRequest.build()));
ListFilesResponse listFilesResponse = (ListFilesResponse) Utils.deserialize(lFilesResponse);
List<String> fileList = listFilesResponse.getFileNamesList();
if (fileList != null) {
for (String file : fileList) {
System.out.println(file);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void debug() {
//nameNode.test();
}
private static String getFileContent(String fileName) {
File file = new File(fileName);
String content = "";
ReadBlockResponse.Builder response = ReadBlockResponse.newBuilder();
if (file.exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String currLine;
while ((currLine = br.readLine()) != null) {
content += currLine;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return content;
}
}