我是靠谱客的博主 玩命大船,这篇文章主要介绍hadoop多路径输入方法,现在分享给大家,希望可以做个参考。

先给一个最简单的wordCount的例子:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cn.hx.test; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCountApp { //自定义的mapper,继承org.apache.hadoop.mapreduce.Mapper public static class MyMapper extends org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, LongWritable>{ @Override protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException { String line = value.toString();//split  函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回,这里按照"t"来分割text文件中字符,即一个制表符,这就是为什么我在文本中用了空格分割,导致最后的结果有很大的出入。 String[] splited = line.split("t");       //foreach 就是 for(元素类型t 元素变量x:遍历对象obj){引用x的java语句} for (String word : splited) { context.write(new Text(word), new LongWritable(1)); } } } public static class MyReducer extends org.apache.hadoop.mapreduce.Reducer<Text, LongWritable, Text, LongWritable>{ @Override protected void reduce(Text k2, Iterable<LongWritable> v2s, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException { long count = 0L; for (LongWritable v2 : v2s) { count += v2.get(); } LongWritable v3 = new LongWritable(count); context.write(k2, v3); } } //客户端代码,写完交给ResourceManager框架去执行 public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, WordCountApp.class.getSimpleName()); //打成jar执行 job.setJarByClass(WordCountApp.class); //数据在哪里? FileInputFormat.setInputPaths(job, args[0]); //使用哪个mapper处理输入的数据? job.setMapperClass(MyMapper.class); //map输出的数据类型是什么? job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); //使用哪个reducer处理输入的数据? job.setReducerClass(MyReducer.class); //reduce输出的数据类型是什么? job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); //数据输出到哪里? FileOutputFormat.setOutputPath(job, new Path(args[1])); //交给yarn去执行,直到执行结束才退出本程序 job.waitForCompletion(true); } }

可以看到,java代码中分别通过FileInputFormat和FileOutputFormat来控制输入的路径,对于多路径输入有如下方法:

1.FileInputFormat.addInputPath(job, new Path(args[0]));的方法可以添加各种路径,可以通过不断调用FileInputFormat.addInputPath函数添加各种路径;

2.多路径用逗号分隔:String Paths= strings[0]+','+strings[1],FileInputFormat.addInputPaths(job,Paths);

3.FileInputFormat.setInputPaths(job,Paths);与addInputPaths,但这里直接替换而不是添加。

最后

以上就是玩命大船最近收集整理的关于hadoop多路径输入方法的全部内容,更多相关hadoop多路径输入方法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(103)

评论列表共有 0 条评论

立即
投稿
返回
顶部