问题一:请使用利用Combiner的方式:根据图示内容编写maprdeuce程序

示例程序

package com.greate.learn;

import java.io.IOException;
import java.net.URI;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class GetFile_Statistics extends Configured implements Tool { public static class CountMapper extends Mapper<LongWritable, Text, Text, Text>{
private Text word = new Text();
private Text one = new Text(1+""); @Override
protected void map(LongWritable key,Text value,Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException,InterruptedException{
System.out.println("line pos:" + key.toString());
String line = value.toString();
String fileName = ((FileSplit) context.getInputSplit()).getPath().getName();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreElements()) {
word.set(tokenizer.nextToken()+" : "+fileName);
context.write(word, one);
}
}
} public static class Combiner extends Reducer<Text, Text, Text, Text>{ @Override
protected void reduce(Text key, Iterable<Text> values,
Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
int sum = 0;
for(Text v : values){
sum += Integer.parseInt(v.toString());
}
System.out.println("sum:" + sum);
String[] valueString = key.toString().split(" : ");
context.write(new Text(valueString[0]), new Text(valueString[1]+":" + sum));
}
} public static class CountReducer extends Reducer<Text, Text, Text, Text>{
static String beforeKey = "";
static String beforeValue ="";
@Override
protected void reduce(Text key, Iterable<Text> values,
Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
String key2 = key.toString();
String value = "";
for(Text text: values){
value = text.toString();
if(key2.equals(beforeKey)){
beforeKey = key2;
beforeValue = beforeValue +";"+value;
}else{
beforeKey = key2;
beforeValue = value;
}
} context.write(new Text(beforeKey), new Text(beforeValue));
}
} static FileSystem fs = null;
static Configuration conf=null;
public static void init() throws Exception{
conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000/");
fs = FileSystem.get(new URI("hdfs://localhost:9000/"),conf,"hadoop");
} public int run(String[] args) throws Exception {
Job job = Job.getInstance(getConf(),"WordCount");
job.setJarByClass(GetFile_Statistics.class); job.setMapperClass(CountMapper.class);
job.setCombinerClass(Combiner.class);
job.setReducerClass(CountReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
Path in = new Path("/GetFile_Statistics/input");
if(fs.exists(in)){
FileInputFormat.addInputPath(job, in);
}else{
System.out.println("文件夹不存在,需要创建!");
}
Path os = new Path("/GetFile_Statistics/output");
int flage = 0;
if(fs.exists(os)){
System.out.println("文件夹存在!不再创建!");
fs.delete(os, true);
FileOutputFormat.setOutputPath(job, os);
flage = job.waitForCompletion(false) ? 0:1;
}else{
FileOutputFormat.setOutputPath(job, os);
flage = job.waitForCompletion(false) ? 0:1;
}
return flage;
} public static void main(String[] args) throws Exception {
init();
int res = ToolRunner.run(new GetFile_Statistics(), args);
System.exit(res);
}
}

问题二:现有一批电话通信清单,记录了用户A拨打某些特殊号码(如120,10086,13800138000等)的记录。需要做一个统计结果,记录拨打给用户B的所有用户A。

示例程序


package com.greate.learn;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class PhoneNumber_Statistic extends Configured implements Tool{
public static void main (String[] args) throws Exception{
ToolRunner.run(new PhoneNumber_Statistic(), args);
}
public int run(String[] arg0) throws Exception{
Configuration conf = getConf();
Job job = new Job(conf);
job.setJarByClass(getClass());
FileSystem fs = FileSystem.get(conf);
FileInputFormat.setInputPaths(job, new Path("/PhoneNumber_Statistics/input/"));
FileOutputFormat.setOutputPath(job, new Path("/PhoneNumber_Statistics/output/"));
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(numberMap.class);
job.setReducerClass(numberReduce.class);
job.waitForCompletion(true); return 0;
}
}
class numberMap extends Mapper<LongWritable, Text, Text, Text>{
protected void map(LongWritable key, Text value, Context context)
throws IOException,InterruptedException{
String[] list = value.toString().split(" ");
String keyy = list[1];
String valuee = list[0];
context.write(new Text(keyy), new Text(valuee));
}
}
class numberReduce extends Reducer<Text, Text, Text, Text>{ //��������
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException,InterruptedException{
String valuee;
String out = "";
for(Text value:values){
valuee = value.toString() + " | ";
out +=valuee;
}
context.write(key,new Text(out));
}
}



mapreduce编程练习(二)倒排索引 Combiner的使用以及练习的更多相关文章

  1. hadoop2.2编程:mapreduce编程之二次排序

    mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...

  2. Hadoop MapReduce编程 API入门系列之倒排索引(二十四)

    不多说,直接上代码. 2016-12-12 21:54:04,509 INFO [org.apache.hadoop.metrics.jvm.JvmMetrics] - Initializing JV ...

  3. 《Data-Intensive Text Processing with mapReduce》读书笔记之二:mapreduce编程、框架及运行

    搜狐视频的屌丝男士第二季大结局了,惊现波多野老师,怀揣着无比鸡冻的心情啊,可惜随着剧情的推进发展,并没有出现期待中的屌丝奇遇,大鹏还是没敢冲破尺度的界线.想百度些种子吧,又不想让电脑留下污点证据,要知 ...

  4. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

  5. Hadoop MapReduce编程学习

    一直在搞spark,也没时间弄hadoop,不过Hadoop基本的编程我觉得我还是要会吧,看到一篇不错的文章,不过应该应用于hadoop2.0以前,因为代码中有  conf.set("map ...

  6. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  7. MapReduce编程实例4

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  8. 批处理引擎MapReduce编程模型

    批处理引擎MapReduce编程模型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. MapReduce是一个经典的分布式批处理计算引擎,被广泛应用于搜索引擎索引构建,大规模数据处理 ...

  9. 大数据笔记(十)——Shuffle与MapReduce编程案例(A)

    一.什么是Shuffle yarn-site.xml文件配置的时候有这个参数:yarn.nodemanage.aux-services:mapreduce_shuffle 因为mapreduce程序运 ...

  10. Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)

    不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...

随机推荐

  1. 技术基础 | 监测Apache Cassandra的简明方式——MCAC

    点击这里在GitHub上访问我们,以便深入了解DataStax的开源项目--Apache Cassandra指标收集器(Metric Collector for Apache Cassandra, o ...

  2. 500 份源码合集——GitHub 热点速览 v.21.02

    作者:HelloGitHub-小鱼干 GitHub 项目名,如同变量命名,一个好的项目名能让你一眼就知道它是什么.500-AI-Machine-learning-Deep-learning-Compu ...

  3. 谈谈你不知道的gist

    1.Gist是什么关于Gist的详细介绍,请阅读官方文档About gists,下面只简略介绍部分功能: Gist是一种与其他人共享代码片段和粘贴的简单方法. 当您需要与同事或朋友共享示例代码或技术时 ...

  4. Java 使用 commons-fileupload 实现文件上传工具类

    依赖包 文件上传可以使用 Apache 文件上传组件, commons-fileupload, 它依赖于 commons-io commons-io.jar: https://repo1.maven. ...

  5. Mongodb 安装和副本集集群搭建

    通用步骤,适用于所有你需要用的软件. 总结为5大步骤: 找到官网-->下载包-->解压-->修改配置-->启动 不懂的,首选官网api,次选百度 1.安装mongodb mon ...

  6. element el-table表格的vue组件二次封装(附表格高度自适应)

    基于vue的el-table表格二次封装组件方法 前言 在公司实习使用vue+element-ui框架进行前端开发,使用表格el-table较为多,有些业务逻辑比较相似,有些地方使用的重复性高,如果多 ...

  7. EnvironmentPostProcessor怎么做单元测试?阿里P7解答

    简介 从Spring Boot 1.3开始,我们可以在应用程序上下文刷新之前使用EnvironmentPostProcessor来自定义应用程序的Environment.Environment表示当前 ...

  8. PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)

    说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...

  9. 入门OJ:郭嘉的消息传递

    题目描述 我们的郭嘉大大在曹操这过得逍遥自在,但是有一天曹操给了他一个任务,在建邺城内有N(<=1000)个袁绍的奸细 将他们从1到N进行编号,同时他们之间存在一种传递关系,即若C[i,j]=1 ...

  10. 阿里云VOD(三)

    一.视频播放器 参考文档:https://help.aliyun.com/document_detail/125570.html?spm=a2c4g.11186623.6.1083.1c53448bl ...