java deleteonexit_Java FileSystem.deleteOnExit方法代碼示例

import org.apache.hadoop.fs.FileSystem; //導入方法依賴的package包/類

/**

* Write the list of distributed cache files in the decreasing order of

* file sizes into the sequence file. This file will be input to the job

* {@link GenerateDistCacheData}.

* Also validates if -generate option is missing and distributed cache files

* are missing.

* @return exit code

* @throws IOException

*/

private int writeDistCacheFilesList()

throws IOException {

// Sort the distributed cache files in the decreasing order of file sizes.

List dcFiles = new ArrayList(distCacheFiles.entrySet());

Collections.sort(dcFiles, new Comparator() {

public int compare(Object dc1, Object dc2) {

return ((Comparable) ((Map.Entry) (dc2)).getValue())

.compareTo(((Map.Entry) (dc1)).getValue());

}

});

// write the sorted distributed cache files to the sequence file

FileSystem fs = FileSystem.get(conf);

Path distCacheFilesList = new Path(distCachePath, "_distCacheFiles.txt");

conf.set(GenerateDistCacheData.GRIDMIX_DISTCACHE_FILE_LIST,

distCacheFilesList.toString());

SequenceFile.Writer src_writer = SequenceFile.createWriter(fs, conf,

distCacheFilesList, LongWritable.class, BytesWritable.class,

SequenceFile.CompressionType.NONE);

// Total number of unique distributed cache files

int fileCount = dcFiles.size();

long byteCount = 0;// Total size of all distributed cache files

long bytesSync = 0;// Bytes after previous sync;used to add sync marker

for (Iterator it = dcFiles.iterator(); it.hasNext();) {

Map.Entry entry = (Map.Entry)it.next();

LongWritable fileSize =

new LongWritable(Long.parseLong(entry.getValue().toString()));

BytesWritable filePath =

new BytesWritable(

entry.getKey().toString().getBytes(charsetUTF8));

byteCount += fileSize.get();

bytesSync += fileSize.get();

if (bytesSync > AVG_BYTES_PER_MAP) {

src_writer.sync();

bytesSync = fileSize.get();

}

src_writer.append(fileSize, filePath);

}

if (src_writer != null) {

src_writer.close();

}

// Set delete on exit for 'dist cache files list' as it is not needed later.

fs.deleteOnExit(distCacheFilesList);

conf.setInt(GenerateDistCacheData.GRIDMIX_DISTCACHE_FILE_COUNT, fileCount);

conf.setLong(GenerateDistCacheData.GRIDMIX_DISTCACHE_BYTE_COUNT, byteCount);

LOG.info("Number of HDFS based distributed cache files to be generated is "

+ fileCount + ". Total size of HDFS based distributed cache files "

+ "to be generated is " + byteCount);

if (!shouldGenerateDistCacheData() && fileCount > 0) {

LOG.error("Missing " + fileCount + " distributed cache files under the "

+ " directory\n" + distCachePath + "\nthat are needed for gridmix"

+ " to emulate distributed cache load. Either use -generate\noption"

+ " to generate distributed cache data along with input data OR "

+ "disable\ndistributed cache emulation by configuring '"

+ DistributedCacheEmulator.GRIDMIX_EMULATE_DISTRIBUTEDCACHE

+ "' to false.");

return Gridmix.MISSING_DIST_CACHE_FILES_ERROR;

}

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值