要求:

根据输入文件中的信息,计算出某几个字符串出现的个数

输入文件格式: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参数传递实例的更多相关文章

  1. Jsp与servlet之间页面跳转及参数传递实例(转)

    原网址:http://blog.csdn.net/ssy_shandong/article/details/9328985 11. jsp与servlet之间页面跳转及参数传递实例 分类: Java ...

  2. Hadoop实战实例

    Hadoop实战实例        Hadoop实战实例        Hadoop 是Google MapReduce的一个Java实现.MapReduce是一种简化的分布式编程模式,让程序自动分布 ...

  3. Hadoop数据分析实例:P2P借款人信用风险实时监控模型设计

    Hadoop数据分析实例:P2P借款人信用风险实时监控模型设计 一提到hadoop相信熟悉IT领域或者经常关注互联网新闻的朋友都应该很熟悉了,当然,这种熟悉可能也只是听着名字耳熟,但并不知道它具体是什 ...

  4. hadoop 入门实例【转】

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 1.数据去重  "数据去重"主要是为了掌握 ...

  5. hadoop 异常处理实例(一)hadoop内存配置项

    Exception in thread "main" java.io.IOException: Job failed! at org.apache.hadoop.mapred.Jo ...

  6. Hadoop入门实例——WordCount统计单词

    首先要说明的是运行Hadoop需要jdk1.6或以上版本,如果你还没有搭建好Hadoop集群,请参考我的另一篇文章: Linux环境搭建Hadoop伪分布模式 马上进入正题. 1.启动Hadoop集群 ...

  7. WebApi参数传递实例

    Get 1.基础数据类型 1.1方法只含有一个形参 (1)Get传值的本质是通过url字符串拼接(2)Get传递参数本质是url字符串拼接,Request-Head头部传递,Request-Body中 ...

  8. hadoop程序实例

    安装了Eclipse及hadoop-eclipse-plugin后学着<hadoop权威指南>中的气温例子写了一个输出气温的程序,数据是我自己简单写的,但是输出却不是我预想的,这中间还有很 ...

  9. hadoop参数传递

    传参关键代码: //从配置文件获取参数,必须在作业创建的前面 conf.addResource("hadoop-bigdata.xml"); keepUrl=conf.get(&q ...

随机推荐

  1. 性能改善后复杂SQL

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...

  2. 排他网关(ExclusiveGateWay)

    网关(ExclusiveGateWay) 作者:邓家海2018年3月11日 00:13:25 情景:某一家公司最近在给一个单位做一个财务审批的OA.具体需求是这样的:当部门申请的金额小于一万块的时候, ...

  3. TabHost选项卡的实现(一):使用TabActivity实现

    一. TabHost的基本开发流程 TabHost是一种非常实用的组件,可以很方便的在窗口上防止多个标签页,每个标签页相当于获得了一个外部容器相同大小的组件摆放区域. 我们熟悉的手机电话系统" ...

  4. 从规则引擎到复杂事件处理(CEP)

    Drools Fusion既是规则引擎,又可以作为CEP.除了事件定义和时间推理之外,对于引擎本身也会有一些不同的使用.主要体现在会话时钟.流模式.滑动窗口和对事件的内存管理. 会话时钟 由于事件的时 ...

  5. zoj 3652 Maze

    Maze Time Limit: 2 Seconds      Memory Limit: 65536 KB Celica is a brave person and believer of a Go ...

  6. java抽象类的体现-模板模式

    抽象类是多个具体子类抽象出来的父类,具有高层次的抽象性;以该抽象类作为子类的模板可以避免子类设计的随意性; 抽象类的体现主要就是模板模式设计,抽象类作为多个子类的通用模板,子类在抽象类的基础上进行拓展 ...

  7. H3C 路由计算

  8. P1097 方程的整数解

    题目描述 给定一个整数N,求方程 \(x^3-x^2-x=N\) 的整数解. 保证解的范围在 \([-100,100]\) 范围内. 输入格式 一行一个整数 \(N(-10^6<=N<=1 ...

  9. Java 5,6,7,8,9,10,11新特性吐血总结

    作者:拔剑少年 简书地址:https://www.jianshu.com/u/dad4d9675892 博客地址:https://it18monkey.github.io 转载请注明出处 java5 ...

  10. Array数组的常用方法

    1.concat()链接数组 var a = [1,2,3]; console.log(a.concat(4,5));//[1,2,3,4,5] 2.join()分隔符链接数组变字符串,如果省略该参数 ...