问题一:请使用利用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. 自家公司关于git commit 的规范

    代码提交的commit info提个建议,fix的issue是哪个issue?都要有明确的链接.推荐方式:1.建立issue,说明问题的背景和原因.http://git.startdt.net/pay ...

  2. JavaScript--总结三(数组和函数)

    数组 数组的概念: 将多个元素(通常是同一类型)按照一定顺序排列放到一个集合中,这个集合称之为数组---简(一组有序的数据) 数组的作用:可以一次性存储多个数据 数组的定义: 1.通过构造函数创建数组 ...

  3. TurtleBot3 Waffle (tx2版华夫)(10)自主导航(A2激光雷达)

    1)[Remote PC] 启动roscore $ roscore 2)[TurBot3] 启动turbot3 $ roslaunch turbot3_bringup minimal.launch 3 ...

  4. .Net 5中Windows Forms运行时的新功能(翻译)

    本文翻译自Igor的文章,原文地址:https://devblogs.microsoft.com/dotnet/whats-new-in-windows-forms-runtime-in-net-5- ...

  5. centos 8.x系统配置chrony时间同步服务

    redhat/centos 7.x默认使用的时间同步服务为ntp服务,但是从redhat/centos 8开始在官方的仓库中移除了ntp软件,换成默认的chrony进行时间同步的服务,虽然也可以通过添 ...

  6. Spark学习进度10-DS&DF基础操作

    有类型操作 flatMap 通过 flatMap 可以将一条数据转为一个数组, 后再展开这个数组放入 Dataset val ds1=Seq("hello spark"," ...

  7. 一文彻底理解IO多路复用

    在讲解IO多路复用之前,我们需要预习一下文件以及文件描述符. 什么是文件 程序员使用I/O最终都逃不过文件. 因为这篇同属于高性能.高并发系列,讲到高性能.高并发就离不开Linux/Unix,因此这里 ...

  8. SQL注入之堆叠注入(堆查询注入)

    Stached injection -- 堆叠注入 0x00 堆叠注入的定义 ​ Stacked injection 汉语翻译过来后,称 为堆查询注入,也有称之为堆叠注入.堆叠注入为攻击者提供了很多的 ...

  9. python模块详解 | progressbar

    参考官方文档:https://pypi.org/project/progressbar/#description progressbar 安装: pip install progressbar pro ...

  10. 十三:SQL注入之MYSQL注入

    MYSQL注入中首先要明确当前注入点权限,高权限注入时有更多的攻击手法,有的能直接进行getshell操作,其中也会遇到很多的阻碍,相关防御手法也要明确,所谓知己知彼,百战不殆.作为安全开发工作者,攻 ...