hadoop参数传递实例
要求:
根据输入文件中的信息,计算出某几个字符串出现的个数
输入文件格式:xxx,xxx,xxx,xx,x,x,xxx,x,x,xx,x,x,x,x,x,x,x,
输出文件:xx 10
xx 4
.....
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 java.io.IOException; /**
* Created by hadoop on 17-3-28.
*/
public class main { public static class mapper extends Mapper<LongWritable,Text,Text,IntWritable>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String str = value.toString();
String[] strs = str.split(","); String s1 = context.getConfiguration().get("flag1");
String s2 = context.getConfiguration().get("flag2");
String s3 = context.getConfiguration().get("flag3");
String s4 = context.getConfiguration().get("flag4"); for(String s:strs){
if(s.equals(s1) || s.equals(s2) || s.equals(s3) || s.equals(s4)){
context.write(new Text(s),new IntWritable(1));
}
} }
} public static class reducer extends Reducer<Text,IntWritable,Text,IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable v:values){
sum += v.get();
} context.write(key,new IntWritable(sum));
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration();
conf.set("flag1",args[2]);
conf.set("flag2",args[3]);
conf.set("flag3",args[4]);
conf.set("flag4",args[5]); Job job = Job.getInstance(conf); job.setJarByClass(main.class);
job.setMapperClass(mapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); job.setReducerClass(reducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1])); boolean b = job.waitForCompletion(true);
System.exit(b?0:1);
}
}
hadoop参数传递实例的更多相关文章
- Jsp与servlet之间页面跳转及参数传递实例(转)
原网址:http://blog.csdn.net/ssy_shandong/article/details/9328985 11. jsp与servlet之间页面跳转及参数传递实例 分类: Java ...
- Hadoop实战实例
Hadoop实战实例 Hadoop实战实例 Hadoop 是Google MapReduce的一个Java实现.MapReduce是一种简化的分布式编程模式,让程序自动分布 ...
- Hadoop数据分析实例:P2P借款人信用风险实时监控模型设计
Hadoop数据分析实例:P2P借款人信用风险实时监控模型设计 一提到hadoop相信熟悉IT领域或者经常关注互联网新闻的朋友都应该很熟悉了,当然,这种熟悉可能也只是听着名字耳熟,但并不知道它具体是什 ...
- hadoop 入门实例【转】
原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 1.数据去重 "数据去重"主要是为了掌握 ...
- hadoop 异常处理实例(一)hadoop内存配置项
Exception in thread "main" java.io.IOException: Job failed! at org.apache.hadoop.mapred.Jo ...
- Hadoop入门实例——WordCount统计单词
首先要说明的是运行Hadoop需要jdk1.6或以上版本,如果你还没有搭建好Hadoop集群,请参考我的另一篇文章: Linux环境搭建Hadoop伪分布模式 马上进入正题. 1.启动Hadoop集群 ...
- WebApi参数传递实例
Get 1.基础数据类型 1.1方法只含有一个形参 (1)Get传值的本质是通过url字符串拼接(2)Get传递参数本质是url字符串拼接,Request-Head头部传递,Request-Body中 ...
- hadoop程序实例
安装了Eclipse及hadoop-eclipse-plugin后学着<hadoop权威指南>中的气温例子写了一个输出气温的程序,数据是我自己简单写的,但是输出却不是我预想的,这中间还有很 ...
- hadoop参数传递
传参关键代码: //从配置文件获取参数,必须在作业创建的前面 conf.addResource("hadoop-bigdata.xml"); keepUrl=conf.get(&q ...
随机推荐
- Remove Extra One 权值线段树
最近疯狂练习线段树... 这道题题意很简单,在1-n中,找寻一个数,使得去掉这个数后,对于每个位置中满足 1<=j<i && a[ j ]<a[ i ] 的位置尽可能 ...
- cesium 基础
scaleByDistance : new Cesium.NearFarScalar(1.5e2, 1.5, 8.0e6, 0.0),--(近值,近端放大率,远值,远端放大率) 给定距离视点的近值和远 ...
- JS遍历数组
for 如果用var会造成变量声明提前等问题for(var i = 1; i <= arr.length; i++){ console.log(arr[i - 1]);} for(let i = ...
- poj 1279 Art Gallery (Half Plane Intersection)
1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...
- Python--day69--单表查询之神奇的双下划线
单表查询之神奇的双下划线: 单表查询之神奇的双下划线 models.Tb1.objects.filter(id__lt=10, id__gt=1) # 获取id大于1 且 小于10的值 models. ...
- git分支合并及冲突解决
小明在愉快的敲代码 vim ok 他在ok里敲了simachanping git commit -am "commit first version by xiaoming" 然后这 ...
- H3C Easy IP配置举例
- Laravel基本使用、生成Cookie、返回视图、JSON/JSONP、文件下载及重定向
https://yq.aliyun.com/ziliao/23889 1.Response篇 1.1 基本响应 最基本的HTTP响应只需在路由闭包或控制器动作中返回一个简单字符串即可,但是具体业务逻辑 ...
- Python--day31--TCP的长链接
- linux 读者/写者旗标
旗标为所有调用者进行互斥, 不管每个线程可能想做什么. 然而, 很多任务分为 2 种清 楚的类型: 只需要读取被保护的数据结构的类型, 和必须做改变的类型. 允许多个并发读 者常常是可能的, 只要没有 ...