hadoop FileSplit

/** A section of an input file. Returned by {@link
* InputFormat#getSplits(JobContext)} and passed to
* {@link InputFormat#createRecordReader(InputSplit,TaskAttemptContext)}. 
* 
* 文件的一部分,通过InputFormat#getSplits(JobContext)生成
* 作为参数生产RecordReader:InputFormat#createRecordReader(InputSplit,TaskAttemptContext)
* 实现了InputSplit接口
*/
@InterfaceAudience.Public
@InterfaceStability.Stable
public class FileSplit extends InputSplit implements Writable {
private Path file;
private long start;
private long length;
private String[] hosts;
private SplitLocationInfo[] hostInfos;

public FileSplit() {}

/** Constructs a split with host information
*
* @param file the file name。 文件名称
* @param start the position of the first byte in the file to process。第一个byte的偏移量
* @param length the number of bytes in the file to process。 split的长度
* @param hosts the list of hosts containing the block, possibly null。 split所在的主机列表
*/
public FileSplit(Path file, long start, long length, String[] hosts) {
this.file = file;
this.start = start;
this.length = length;
this.hosts = hosts;
}

/** Constructs a split with host and cached-blocks information
*
* @param file the file name。 文件名称
* @param start the position of the first byte in the file to process。第一个byte的偏移量
* @param length the number of bytes in the file to process split的长度
* @param hosts the list of hosts containing the block split所在的主机列表
* @param inMemoryHosts the list of hosts containing the block in memory 在内存中保存block的机器列表
*/
public FileSplit(Path file, long start, long length, String[] hosts,
String[] inMemoryHosts) {
this(file, start, length, hosts);
hostInfos = new SplitLocationInfo[hosts.length];
for (int i = 0; i < hosts.length; i++) {
// because N will be tiny, scanning is probably faster than a HashSet
boolean inMemory = false;
for (String inMemoryHost : inMemoryHosts) {
if (inMemoryHost.equals(hosts[i])) {
inMemory = true;
break;
}
}
hostInfos[i] = new SplitLocationInfo(hosts[i], inMemory);
}
}

/** The file containing this split's data. */
public Path getPath() { return file; }

/** The position of the first byte in the file to process. */
public long getStart() { return start; }

/** The number of bytes in the file to process. */
@Override
public long getLength() { return length; }

@Override
public String toString() { return file + ":" + start + "+" + length; }


// Writable methods


@Override
public void write(DataOutput out) throws IOException {
Text.writeString(out, file.toString());
out.writeLong(start);
out.writeLong(length);
}

@Override
public void readFields(DataInput in) throws IOException {
file = new Path(Text.readString(in));
start = in.readLong();
length = in.readLong();
hosts = null;
}

@Override
public String[] getLocations() throws IOException {
if (this.hosts == null) {
return new String[]{};
} else {
return this.hosts;
}
}

@Override
@Evolving
public SplitLocationInfo[] getLocationInfo() throws IOException {
return hostInfos;
}
}

 

转载于:https://www.cnblogs.com/fantiantian/p/9340239.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值