org.apache.hadoop.fs-BlockLocation


工具类吧


  1 package org.apache.hadoop.fs;
  2 
  3 import org.apache.hadoop.io.*;
  4 //IO包下的类还没涉及到。遇到一个分析一个。
  5 import java.io.*;
  6 
  7 /*
  8  * A BlockLocation lists hosts, offset and length
  9  * of block. 
 10  * 
 11  */
 12 //记录block的元数据信息,如所在host,长度和偏移量
 13 public class BlockLocation implements Writable {
 14 //针对集群块位置的类
 15   static {               // register a ctor
 16     WritableFactories.setFactory
 17       (BlockLocation.class,
 18        new WritableFactory() {
 19          public Writable newInstance() { return new BlockLocation(); }
 20        });
 21   }
 22 //注册了一个Writable子类BlockLocation的工厂。内部类。详细可看http://book.2cto.com/201305/21915.html
 23   private String[] hosts; //hostnames of datanodes
 24   //节点的主机名数组
 25   private String[] names; //hostname:portNumber of datanodes
 26   //节点的名称数组。名称的格式。
 27   private String[] topologyPaths; // full path name in network topology
 28   //节点在网络拓扑结构中的地址
 29   private long offset;  //offset of the of the block in the file
 30   //块在文件中的偏移量
 31   private long length;
 32   //块长度
 33 
 34   /**
 35    * Default Constructor
 36    */
 37   public BlockLocation() {
 38     this(new String[0], new String[0],  0L, 0L);
 39   }
 40 //默认构造方法
 41   /**
 42    * Constructor with host, name, offset and length
 43    */
 44   public BlockLocation(String[] names, String[] hosts, long offset, 
 45                        long length) {
 46     if (names == null) {
 47       this.names = new String[0];
 48     } else {
 49       this.names = names;
 50     }
 51     if (hosts == null) {
 52       this.hosts = new String[0];
 53     } else {
 54       this.hosts = hosts;
 55     }
 56     this.offset = offset;
 57     this.length = length;
 58     this.topologyPaths = new String[0];
 59   }
 60 //根据名称主机偏移量长度初始化一个块对象
 61   /**
 62    * Constructor with host, name, network topology, offset and length
 63    */
 64   public BlockLocation(String[] names, String[] hosts, String[] topologyPaths,
 65                        long offset, long length) {
 66     this(names, hosts, offset, length);
 67     if (topologyPaths == null) {
 68       this.topologyPaths = new String[0];
 69     } else {
 70       this.topologyPaths = topologyPaths;
 71     }
 72   }
 73 //根据.........
 74   /**
 75    * Get the list of hosts (hostname) hosting this block
 76    */
 77   public String[] getHosts() throws IOException {
 78     if ((hosts == null) || (hosts.length == 0)) {
 79       return new String[0];
 80     } else {
 81       return hosts;
 82     }
 83   }
 84 //获得块的主机
 85   /**
 86    * Get the list of names (hostname:port) hosting this block
 87    */
 88   public String[] getNames() throws IOException {
 89     if ((names == null) || (names.length == 0)) {
 90       return new String[0];
 91     } else {
 92       return this.names;
 93     }
 94   }
 95 //。。。。
 96   /**
 97    * Get the list of network topology paths for each of the hosts.
 98    * The last component of the path is the host.
 99    */
100   public String[] getTopologyPaths() throws IOException {
101     if ((topologyPaths == null) || (topologyPaths.length == 0)) {
102       return new String[0];
103     } else {
104       return this.topologyPaths;
105     }
106   }
107   //。。。。。
108   /**
109    * Get the start offset of file associated with this block
110    */
111   public long getOffset() {
112     return offset;
113   }
114   //。。。。。
115   /**
116    * Get the length of the block
117    */
118   public long getLength() {
119     return length;
120   }
121   //。。。。。
122   /**
123    * Set the start offset of file associated with this block
124    */
125   public void setOffset(long offset) {
126     this.offset = offset;
127   }
128 //。。。。。
129   /**
130    * Set the length of block
131    */
132   public void setLength(long length) {
133     this.length = length;
134   }
135 //。。。。。
136   /**
137    * Set the hosts hosting this block
138    */
139   public void setHosts(String[] hosts) throws IOException {
140     if (hosts == null) {
141       this.hosts = new String[0];
142     } else {
143       this.hosts = hosts;
144     }
145   }
146 //。。。。。
147   /**
148    * Set the names (host:port) hosting this block
149    */
150   public void setNames(String[] names) throws IOException {
151     if (names == null) {
152       this.names = new String[0];
153     } else {
154       this.names = names;
155     }
156   }
157 //。。。。。
158   /**
159    * Set the network topology paths of the hosts
160    */
161   public void setTopologyPaths(String[] topologyPaths) throws IOException {
162     if (topologyPaths == null) {
163       this.topologyPaths = new String[0];
164     } else {
165       this.topologyPaths = topologyPaths;
166     }
167   }
168 //。。。。。
169   /**
170    * Implement write of Writable
171    */
172   public void write(DataOutput out) throws IOException {
173     out.writeLong(offset);
174     out.writeLong(length);
175     out.writeInt(names.length);
176     for (int i=0; i < names.length; i++) {
177       Text name = new Text(names[i]);
178       name.write(out);
179     }
180     out.writeInt(hosts.length);
181     for (int i=0; i < hosts.length; i++) {
182       Text host = new Text(hosts[i]);
183       host.write(out);
184     }
185     out.writeInt(topologyPaths.length);
186     for (int i=0; i < topologyPaths.length; i++) {
187       Text host = new Text(topologyPaths[i]);
188       host.write(out);
189     }
190   }
191   //把块信息写到输出流中。用到了Writable子类Long,Int,Text的Write方法。序列化
192   /**
193    * Implement readFields of Writable
194    */
195   public void readFields(DataInput in) throws IOException {
196     this.offset = in.readLong();
197     this.length = in.readLong();
198     int numNames = in.readInt();
199     this.names = new String[numNames];
200     for (int i = 0; i < numNames; i++) {
201       Text name = new Text();
202       name.readFields(in);
203       names[i] = name.toString();
204     }
205     int numHosts = in.readInt();
206     for (int i = 0; i < numHosts; i++) {
207       Text host = new Text();
208       host.readFields(in);
209       hosts[i] = host.toString();
210     }
211     int numTops = in.readInt();
212     Text path = new Text();
213     for (int i = 0; i < numTops; i++) {
214       path.readFields(in);
215       topologyPaths[i] = path.toString();
216     }
217   }
218   //把块信息从输入流中读出来。....。反序列化
219   public String toString() {
220     StringBuilder result = new StringBuilder();
221     result.append(offset);
222     result.append(',');
223     result.append(length);
224     for(String h: hosts) {
225       result.append(',');
226       result.append(h);
227     }
228     return result.toString();
229   }
230   //。。。。。。
231 }

 

posted @ 2014-10-13 15:01  Daem0n  阅读(887)  评论(0编辑  收藏  举报