要求:

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

输入文件格式: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. Python的unittest拓展和HTMLReport SKIP报表扩展

    C:\Python27\Lib中修改unittest内容 unittest 在init中添加Myskip代码: __all__ = ['TestResult', 'TestCase', 'TestSu ...

  2. CSS属性Display(显示)和Visibility(可见性)

    隐藏一个元素可以通过把display属性设置为“none”,或把visibility属性设置为“hidden”.但是请注意,这两种方法会产生不同的效果. Visibility:hidden可以隐藏某个 ...

  3. C# Brush Color String 互相转换

    using System.Windows.Media; //String转换成Color Color color = (Color)ColorConverter.ConvertFromString(s ...

  4. 解析xml的方式

    1.DOM 理论:将标记文档语言一次性加载进内存,在内存中形成DOM树. 优点:操作方便,可以对文档进行CRUD(增删改查)操作,适用于服务端操作 缺点:占内存,不适用与手机,智能家居等内存容量小的设 ...

  5. hdu 1708 Fibonacci String

    Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. 洛谷P3377 【模板】左偏树(可并堆) 题解

    作者:zifeiy 标签:左偏树 这篇随笔需要你在之前掌握 堆 和 二叉树 的相关知识点. 堆支持在 \(O(\log n)\) 的时间内进行插入元素.查询最值和删除最值的操作.在这里,如果最值是最小 ...

  7. DataTable添加单个或多个字段组成的主键,实现查找

    单列主键 DataTable fdt = CmmDb.GetDataTable(orgsql); fdt.PrimaryKey = new DataColumn[] { fdt.Columns[&qu ...

  8. Class对象的isAssignableFrom方法

    isAssignableFrom 在看一个开源代码时,在加载完某个Class对象后,经常会使用 java.lang.Class#isAssignableFrom 来校验下. 之前真没有注意过Class ...

  9. [转]WebApi 后端文件传输至远程服务器

    /* 功能说明:微信退款需要有数字证书,而我们公司是做小程序平台的,会帮商家自动退款,所以会要求商家把微信证书上传至我们服务器,以便 微信退款. 使用HttpPostedFile 接受前端上传的文件, ...

  10. Vue 设置class样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...