DistributedCache的使用方法(hadoop0.20.2)
来源:互联网 发布:mac连校园网 编辑:程序博客网 时间:2024/06/07 23:05
public class ProcedureJob { public static class MapClass extends Mapper<Object, Text, Text, Text>{ private Path[] localFiles; private URI[] uris; @Override protected void setup(Context context) throws IOException, InterruptedException { localFiles = DistributedCache.getLocalCacheFiles(context.getConfiguration()); uris = DistributedCache.getCacheFiles(context.getConfiguration()); System.out.println(uris[0].toString()+"+++++++++++++++"+localFiles+"----------------"); if(uris==null || uris.length==0){ throw new IOException("Error reading file from distributed cache. No URIs found."); } FileSystem fs = FileSystem.get(URI.create("hdfs://namenode:9000"),context.getConfiguration()); Path localPath = new Path("~/abc.txt"); fs.copyToLocalFile(new Path(uris[0].toString()),localPath ); BufferedReader br = new BufferedReader(new FileReader(localPath.toString())); String str=""; while((str=br.readLine())!=null){ System.out.println(str+"---------------------------------------------"); } br.close(); } protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { context.write(new Text(key.toString()), new Text("1")); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); DistributedCache.addCacheFile(new Path(args[0]).toUri(), conf); Job job = new Job(conf,"Test"); job.setJarByClass(ProcedureJob.class); FileInputFormat.setInputPaths(job,new Path(args[1])); FileOutputFormat.setOutputPath(job, new Path(args[2])); job.setJobName("Test"); job.setMapperClass(MapClass.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); job.setNumReduceTasks(0); job.waitForCompletion(true); }}
说明:事先将本地文件上传到HDFS中,/data/pubic/test.txt。
注意点:
1.Job对象必须在使用configuration对象把缓存文件添加之后才能new,否则会无法添加,出现java.lang.NullPointerException。
2.在setup中获取Path[] localFiles = DistributedCache.getLocalCacheFiles(context.getConfiguration());得到的对象是null,可能版本问题。
3.在setup方法中获取文件系统,FileSystem fs = FileSystem.get(URI.create("hdfs://namenode:9000"),context.getConfiguration());
如果直接是FileSystem fs = FileSystem.get(context.getConfiguration());会出现java.lang.IllegalArgumentException: Wrong FS: hdfs://localhost:54310/user/Hadoop/b, expected: file:/// 异常。
- DistributedCache的使用方法(hadoop0.20.2)
- Hadoop的分布式缓存DistributedCache使用方法
- 【Hadoop】Hadoop0.20.2的安装
- Pig-0.9.2的安装过程(Hadoop0.20.2)
- Hadoop DistributedCache使用方法及原理
- eclipse indigo的hadoop0.20.2插件
- 基于hadoop0.20.2的wordcount实例
- 基于hadoop0.20.2只有map的 实例
- 基于hadoop0.20.2mapreducer的实例
- hadoop DistributedCache的使用
- hadoop的DistributedCache
- DistributedCache.Version #的作用
- DistributedCache的使用
- DistributedCache的使用方法 制定文件到链接不用关心实际路径
- hadoop0.20.2中的KeyValueInputFormat
- Hadoop DistributedCache的使用解释
- 如何使用Hadoop的DistributedCache
- 基于hadoop0.20.2的列出文件夹下所有文件
- 电子商务物流模式分类
- SQL模糊查询
- CSS相框效果
- 如何获取数据集的某些字段值,并赋值给文本控件
- socket实现进程间通信
- DistributedCache的使用方法(hadoop0.20.2)
- IOS应⽤用程序⽣生命周期 UIViewController的⽣生命周期(1)
- 视图切换
- CMMI的级别和CMMI的过程域
- View 控件增加响应位置
- Quartz 1.8.5 集群(jboss4.2.3 )
- c++创建多线程的三种方式的比较 和 多线程通信
- 讲述js算法中的排序、数组去重
- 一个测试ok的popupwindow demo