mapreduce的缓存(addCacheFile)使用

来源:互联网 发布:centos 启动php 编辑:程序博客网 时间:2024/05/19 17:51

1.在main()方法中添加缓存路径

job.addCacheFile(new URI(args[2]));job.addCacheFile(new URI(args[3]));

2.在map或者reduce的setup方法中处理缓存文件

FileReader in = null;BufferedReader reader = null;HashMap<String, String> n_map = null;Path[] cacheFiles = context.getLocalCacheFiles();Path cacheFile = cacheFiles[0];Path cacheFile2 = cacheFiles[1];in = new FileReader(cacheFile.toUri().getPath());reader = new BufferedReader(in);n_map = new HashMap<String, String>();String line = null;while (null != (line = reader.readLine())) {    String[] fields = line.split("\001");    if (fields.length > 4) {        String f1 = fields[0];        String f2 = fields[4];        n_map.put(f1, f2);    }}IOUtils.closeStream(reader);IOUtils.closeStream(in);