Java执行hadoop的基本操作实例代码

  

<强> Java执行hadoop的基本操作实例代码

  

<强>向HDFS上传本地文件

        公共静态孔隙uploadInputFile(字符串localFile)抛出IOException {   配置配置=new配置();   字符串hdfsPath=" hdfs://localhost: 9000/";   字符串hdfsInput=" hdfs://localhost: 9000/user/hadoop/输入”;   文件系统fs=FileSystem.get (URI.create (hdfsPath),配置);   fs。copyFromLocalFile(新路径(localFile)、新路径(hdfsInput));   fs.close ();   System.out.println(“已经上传文件到输入文件夹啦”);   }   之前      

<强>将输出文件下载到本地

        公共静态孔隙getOutput(字符串outputfile)抛出IOException {   字符串remoteFile=" hdfs://localhost: 9000/user/hadoop/输出/部分- r - 00000”;   路径路径=new路径(remoteFile);   配置配置=new配置();   字符串hdfsPath=" hdfs://localhost: 9000/";   文件系统fs=FileSystem.get (URI.create (hdfsPath),配置);   fs。copyToLocalFile(路径,新的路径(outputfile));   System.out.println(“已经将输出文件保留到本地文件”);   fs.close ();   }   之前      

<>强删除hdfs中的文件

        公共静态孔隙deleteOutput()抛出IOException {   配置配置=new配置();   字符串hdfsOutput=" hdfs://localhost: 9000/user/hadoop/输出”;   字符串hdfsPath=" hdfs://localhost: 9000/";   路径路径=new路径(hdfsOutput);   文件系统fs=FileSystem.get (URI.create (hdfsPath),配置);   fs.deleteOnExit(路径);   fs.close ();   System.out.println(“输出文件已经删除”);   }   之前      

<>强执行mapReduce程序

  

创建Mapper类和减速机类

        公共静态类TokenizerMapper Mapper<延伸;对象,文本,文本,IntWritable> {      私人最终静态IntWritable>   公共静态孔隙runMapReduce (String [] args)抛出异常{   配置配置=new配置();   String [] otherArgs=new GenericOptionsParser(参看,args) .getRemainingArgs ();   如果(otherArgs。长度!=2){   System.err。println(“用法:wordcount& lt; out>”);   system . exit (2);   }   工作工作=新工作(参看“字数”);   job.setJarByClass (WordCount.class);   job.setMapperClass (TokenizerMapper.class);   job.setCombinerClass (IntSumReducer.class);   job.setReducerClass (IntSumReducer.class);   job.setOutputKeyClass (Text.class);   job.setOutputValueClass (IntWritable.class);   FileInputFormat。addInputPath(工作,新的路径(otherArgs [0]));   FileOutputFormat。setOutputPath(工作,新的路径(otherArgs [1]));   system . out。println (“mapReduce执行完毕!”);   system . exit (job.waitForCompletion(真正的)& # 63;0:1);      }      之前      

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Java执行hadoop的基本操作实例代码