@@ -104,10 +104,12 @@ public List<Hdfs.BlockLocations> openFileForRead(String fileName) {
104104 try {
105105 byte [] res = nn .openFile (openRequest );
106106 Hdfs .OpenFileResponse openResponse = Hdfs .OpenFileResponse .parseFrom (res );
107- byte [] blockLocationRequest = ProtoMessage .blockLocationRequest (openResponse .getBlockNumsList ());
108- byte [] blockLocationResponse = nn .getBlockLocations (blockLocationRequest );
109- Hdfs .BlockLocationResponse response = Hdfs .BlockLocationResponse .parseFrom (blockLocationResponse );
110- blockLocations = response .getBlockLocationsList ();
107+ if (openResponse .getStatus () == 1 ) {
108+ byte [] blockLocationRequest = ProtoMessage .blockLocationRequest (openResponse .getBlockNumsList ());
109+ byte [] blockLocationResponse = nn .getBlockLocations (blockLocationRequest );
110+ Hdfs .BlockLocationResponse response = Hdfs .BlockLocationResponse .parseFrom (blockLocationResponse );
111+ blockLocations = response .getBlockLocationsList ();
112+ }
111113 } catch (Exception e ) { log (e .toString ()); }
112114 return blockLocations ;
113115 }
@@ -143,20 +145,25 @@ public void writeFile(String fileName, byte[] data) {
143145 public void readFile (String fileName ) {
144146 ByteString data = ByteString .EMPTY ;
145147 List <Hdfs .BlockLocations > blockLocations = openFileForRead (fileName );
146- for (Hdfs .BlockLocations block : blockLocations ) {
147- Random rand = new Random ();
148- Integer dataNodeInd = rand .nextInt (DN_COUNT );
149- Hdfs .DataNodeLocation dnl = block .getLocations (dataNodeInd );
150- Integer dataNodeId = dnl .getPort ();
151- log ("Pulling data from DN " + dataNodeId .toString ());
152- try {
153- byte [] request = ProtoMessage .readBlockRequest (block .getBlockNumber ());
154- byte [] response = dns .get (dataNodeId ).readBlock (request );
155- Hdfs .ReadBlockResponse readBlockResponse = Hdfs .ReadBlockResponse .parseFrom (response );
156- data = data .concat (readBlockResponse .getData (0 ));
157- } catch (Exception e ) { log (e .toString ()); }
148+ if (blockLocations != null ) {
149+ for (Hdfs .BlockLocations block : blockLocations ) {
150+ Random rand = new Random ();
151+ Integer dataNodeInd = rand .nextInt (block .getLocationsCount ());
152+ Hdfs .DataNodeLocation dnl = block .getLocations (dataNodeInd );
153+ Integer dataNodeId = dnl .getPort ();
154+ log ("Pulling data from DN " + dataNodeId .toString ());
155+ try {
156+ byte [] request = ProtoMessage .readBlockRequest (block .getBlockNumber ());
157+ byte [] response = dns .get (dataNodeId ).readBlock (request );
158+ Hdfs .ReadBlockResponse readBlockResponse = Hdfs .ReadBlockResponse .parseFrom (response );
159+ data = data .concat (readBlockResponse .getData (0 ));
160+ } catch (Exception e ) { log (e .toString ()); }
161+ }
162+ log ("File " + fileName + " has contents: " + data .toStringUtf8 ());
163+ }
164+ else {
165+ log ("File : " + fileName + " does not exist." );
158166 }
159- System .out .println ("File " + fileName + " has contents: " + data .toStringUtf8 ());
160167 }
161168
162169 public void mainloop () {
0 commit comments