Mapreduce设置多路径输入输出
生活随笔
收集整理的這篇文章主要介紹了
Mapreduce设置多路径输入输出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載請注明:http://www.cnblogs.com/demievil/p/4058279.html
我的github博客:http://demievil.github.io/
最近寫Mapreduce程序時,想用到多路徑輸入,一次輸入多個文件夾下的數據。并且希望輸出路徑也可以區分,修改輸出文件的名稱。查了相關資料,已實現。
- 多路徑輸入
設置Mapreduce的輸入是HDFS上多個文件夾下的數據,在main函數下稍作配置即可,示例代碼如下:
public static void main(String[] args) throws Exception { String ioPath[] = {"hdfs://10.1.2.3:8020/user/me/input/folder1","hdfs://10.1.2.3:8020/user/me/input/folder2","hdfs://10.1.2.3:8020/user/me/output"};Configuration conf = new Configuration();conf.set("fs.defaultFS", "hdfs://10.1.2.3:8020");conf.set("mapreduce.jobtracker.address", "10.1.2.3:8021");Job job = Job.getInstance(conf, "Job-Name"); job.setJarByClass(TestMain.class);job.setReducerClass(TestReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);FileSystem fs = FileSystem.get(conf);fs.delete(new Path(ioPath[2]),true);MultipleInputs.addInputPath(job, new Path(ioPath[0]), TextInputFormat.class, TagUrlMapper.class);MultipleInputs.addInputPath(job, new Path(ioPath[1]), TextInputFormat.class, TagUrlMapper.class);FileOutputFormat.setOutputPath(job, new Path(ioPath[2]));System.exit(job.waitForCompletion(true) ? 0 : 1);}使用MultipleInputs.addInputPath()方法添加輸入路徑,輸入類型和Mapper類。
- 多路徑輸出
在reducer中配置多路徑輸出及輸出文件名的開頭,示例代碼:
public class TestReducer extends Reducer<Text, Text, Text, Text> {private MultipleOutputs<Text, Text> mos;@Overrideprotected void setup(Context context) throws IOException, InterruptedException {super.setup(context);mos = new MultipleOutputs<Text, Text>(context);}@Overrideprotected void cleanup(Context context) throws IOException, InterruptedException {super.cleanup(context);mos.close();} @Overridepublic void reduce(Text key, Iterable<Text> values,Context context)throws IOException, InterruptedException {while(values.iterator().hasNext()){tag = values.iterator().next();if (......){mos.write(key, new Text("taged"), "taged/taged");}else{mos.write(key, new Text("untaged"), "untaged/untaged");}} }使用MultipleOutputs類來控制輸出路徑。重寫Reducer的setup()和cleanup()方法,如示例代碼所示。
輸出路徑示例如下:
?
我的github博客:http://demievil.github.io/
轉載于:https://www.cnblogs.com/kodyan/p/4058279.html
總結
以上是生活随笔為你收集整理的Mapreduce设置多路径输入输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 法定公积金和任意公积金的区别
- 下一篇: 索尼 ZV-1 II 相机爆料:1 英寸