设为首页 收藏本站
查看: 1176|回复: 0

[经验分享] Hadoop MapReduce编程 API入门系列之MapReduce多种输出格式分析(十九)

[复制链接]

尚未签到

发表于 2017-12-18 12:03:02 | 显示全部楼层 |阅读模式
1 package zhouls.bigdata.myMapReduce.Email;  

  2  
  3 import java.io.IOException;
  
  4 import org.apache.hadoop.conf.Configuration;
  
  5 import org.apache.hadoop.conf.Configured;
  
  6 import org.apache.hadoop.fs.FileSystem;
  
  7 import org.apache.hadoop.fs.Path;
  
  8 import org.apache.hadoop.io.IntWritable;
  
  9 import org.apache.hadoop.io.LongWritable;
  
10 import org.apache.hadoop.io.Text;
  
11 import org.apache.hadoop.mapreduce.Job;
  
12 import org.apache.hadoop.mapreduce.Mapper;
  
13 import org.apache.hadoop.mapreduce.Reducer;
  
14 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  
15 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  
16 import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
  
17 import org.apache.hadoop.util.Tool;
  
18 import org.apache.hadoop.util.ToolRunner;
  
19
  
20 //通过MultipleOutputs写到多个文件:参考博客http://www.cnblogs.com/codeOfLife/p/5452902.html
  
21
  
22 //    MultipleOutputs 类可以将数据写到多个文件,这些文件的名称源于输出的键和值或者任意字符串。
  
23 //  这允许每个 reducer(或者只有 map 作业的 mapper)创建多个文件。 采用name-m-nnnnn 形式的文件名用于 map 输出,name-r-nnnnn 形式的文件名用于 reduce 输出,
  
24 //  其中 name 是由程序设定的任意名字, nnnnn 是一个指明块号的整数(从 0 开始)。块号保证从不同块(mapper 或 reducer)输出在相同名字情况下不会冲突。
  
25
  
26
  
27

  
28 public>
  
29     public static>  
30         private final static IntWritable one = new IntWritable(1);
  
31
  
32         @Override
  
33         protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  
34             context.write(value, one);
  
35         }
  
36     }
  
37
  
38     

  
39     public static>  
40         private IntWritable result = new IntWritable();
  
41         private MultipleOutputs< Text, IntWritable> multipleOutputs;
  
42
  
43         @Override
  
44         protected void setup(Context context) throws IOException ,InterruptedException{
  
45             multipleOutputs = new MultipleOutputs<Text, IntWritable>(context);
  
46         }
  
47         
  
48         protected void reduce(Text Key, Iterable<IntWritable> Values,Context context) throws IOException, InterruptedException {
  
49             int begin = Key.toString().indexOf("@");//indexOf方法返回一个整数值,指出 String 对象内子字符串的开始位置。
  
50             int end = Key.toString().indexOf(".");//indexOf方法返回一个整数值,指出 String 对象内子字符串的开始位置。只不过我们自己写出个end变量而已
  
51 //            Key.toString().indexOf(ch)
  
52 //            Key.toString().indexOf(str)
  
53 //            Key.toString().indexOf(ch, fromIndex)
  
54 //            Key.toString().indexOf(str, fromIndex)
  
55 //            Key.toString().intern()
  
56            
  
57 //            Java中字符串中子串的查找共有四种方法,如下:
  
58 //            1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。
  
59 //            2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
  
60 //            3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。
  
61 //            4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
  
62            
  
63            
  
64             if(begin>=end){
  
65                 return;
  
66             }
  
67            
  
68             //获取邮箱类别,比如 qq
  
69             String name = Key.toString().substring(begin+1, end);
  
70 //                        String.subString(start,end)截取的字符串包括起点所在的字符串,不包括终点所在的字符串
  
71            
  
72             int sum = 0;
  
73            
  
74             for (IntWritable value : Values) {
  
75                 sum += value.get();
  
76             }
  
77            
  
78             result.set(sum);
  
79             multipleOutputs.write(Key, result, name);
  
80                         //这里,我们用到的是multipleOutputs.write(Text key, IntWritable value, String baseOutputPath);
  
81            
  
82 //            multipleOutputs.write默认有3种构造方法:
  
83 //            multipleOutputs.write(String namedOutput, K key, V value);
  
84 //            multipleOutputs.write(Text key, IntWritable value, String baseOutputPath);
  
85 //            multipleOutputs.write(String namedOutput, K key, V value,String  baseOutputPath);
  
86               
  
87 //            MultipleOutputs 类可以将数据写到多个文件,这些文件的名称源于输出的键和值或者任意字符串。
  
88 //            这允许每个 reducer(或者只有 map 作业的 mapper)创建多个文件。
  
89 //             采用name-m-nnnnn 形式的文件名用于 map 输出,name-r-nnnnn 形式的文件名用于 reduce 输出,
  
90 //             其中 name 是由程序设定的任意名字,
  
91 //            nnnnn 是一个指明块号的整数(从 0 开始)。
  
92 //             块号保证从不同块(mapper 或 reducer)写的输出在相同名字情况下不会冲突。
  
93            
  
94         }
  
95         
  
96         @Override
  
97         protected void cleanup(Context context) throws IOException ,InterruptedException{
  
98             multipleOutputs.close();
  
99         }
  
100         
  
101     }
  
102
  
103     public int run(String[] args) throws Exception {
  
104         Configuration conf = new Configuration();// 读取配置文件
  
105         
  
106         Path mypath = new Path(args[1]);
  
107         FileSystem hdfs = mypath.getFileSystem(conf);//创建输出路径
  
108         if (hdfs.isDirectory(mypath)) {
  
109             hdfs.delete(mypath, true);
  
110         }
  
111         Job job = Job.getInstance();// 新建一个任务
  
112         job.setJarByClass(Email.class);// 主类
  
113         
  
114         FileInputFormat.addInputPath(job, new Path(args[0]));// 输入路径
  
115         FileOutputFormat.setOutputPath(job, new Path(args[1]));// 输出路径
  
116
  
117         job.setMapperClass(MailMapper.class);// Mapper
  
118         job.setReducerClass(MailReducer.class);// Reducer
  
119         
  
120         job.setOutputKeyClass(Text.class);// key输出类型
  
121         job.setOutputValueClass(IntWritable.class);// value输出类型
  
122         
  
123         job.waitForCompletion(true);
  
124         return 0;
  
125     }
  
126
  
127     public static void main(String[] args) throws Exception {
  
128         String[] args0 = {
  
129                 "hdfs://HadoopMaster:9000/inputData/multipleOutputFormats/mail.txt",
  
130                 "hdfs://HadoopMaster:9000/outData/MultipleOutputFormats/" };
  
131         int ec = ToolRunner.run(new Configuration(), new Email(), args0);
  
132         System.exit(ec);
  
133     }
  
134 }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-425354-1-1.html 上篇帖子: Hadoop HDFS编程 API入门系列之HDFS 下篇帖子: Hadoop MapReduce编程 API入门系列之最短路径(十五)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表