zoukankan      html  css  js  c++  java
  • a+b per line

    a+b per line

    描述

    有时候你会遇到这样的问题:你有一个表格,给出了每个人在十二月,一月和二月的收入。表格如下:
    name  Dec   Jan($)
    CM    200   314
    LY    2000  332
    QQM   6000  333
    ZYM   5000  333
    BP    30    12 

    你需要知道每个人这三个月的收入总和,那么你就需要将表格中一行代表收入的数字相加.下面请编写程序解决这个问题。

    输入

    输入只包含一个文件,文件中有一个表格,它的结构如下:
    1 200   314
    2 2000  332
    3 6000  333
    4 5000  333
    5 30    12   
    其中每行最前面的数字是行标

    输出

    输出是一个文本文件,每一行第一个数字式行标,第二个数字是输入文件中每一行除行标外数字的和。如下:
    1 514
    2 2332
    3 6333
    4 5333
    5 42

    输入样例

    input:
    1 200   314
    2 2000  332
    3 6000  333
    4 6000  333
    5 5000  333
    6 30    12 

    输出样例:

    1 514
    2 2332
    3 6333
    4 6333
    5 5333
    6 42

    注意:
    1 输入文件和输出文件都只有一个;
    2 输入和输出文件每行的第一个数字都是行标;
    3 每个数据都是正整数或者零.。

    提示:
    在这个问题中,你不需要对数字排序。hadoop会做这些工作。

    代码如下:

    import java.io.IOException;
    import java.util.StringTokenizer;

    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;


    public class SumMapper extends Mapper<LongWritable, Text, LongWritable, IntWritable> {

    //@SuppressWarnings("unchecked")
    public static int linenum=0;
    @Override

    protected void map(LongWritable key, Text value,
    org.apache.hadoop.mapreduce.Mapper.Context context)
    throws IOException, InterruptedException {
    // TODO Auto-generated method stub
    String line=value.toString();
    System.out.println(line);
    StringTokenizer token=new StringTokenizer(line);
    String []str=new String[3];
    int i=0;
    while(token.hasMoreTokens()){
    str[i]=token.nextToken();
    i++;

    }
    System.out.println(str[0]);
    System.out.println(str[1]);
    System.out.println(str[2]);
    linenum++;
    //value.set(Integer.parseInt(str[1]+Integer.parseInt(str[2])));
    context.write(new LongWritable(linenum), new IntWritable(Integer.parseInt(str[1])+Integer.parseInt(str[2])));
    }


    }
    import java.io.IOException;

    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


    public class Sum {

    /**
    *
    @param args
    *
    @throws IOException
    *
    @throws ClassNotFoundException
    *
    @throws InterruptedException
    */
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
    // TODO Auto-generated method stub
    Job job=new Job();
    job.setJobName("qiuhe");
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    job.setMapOutputKeyClass(LongWritable.class);
    job.setMapOutputValueClass(IntWritable.class);
    job.setMapperClass(SumMapper.class);
    job.setOutputKeyClass(LongWritable.class);
    job.setOutputValueClass(IntWritable.class);
    job.waitForCompletion(false);


    }

    }




  • 相关阅读:
    父子进程 signal 出现 Interrupted system call 问题
    一个测试文章
    《淘宝客户端 for Android》项目实战 html webkit android css3
    Django 中的 ForeignKey ContentType GenericForeignKey 对应的数据库结构
    coreseek 出现段错误和Unigram dictionary load Error 新情况(Gentoo)
    一个 PAM dbus 例子
    漫画统计学 T分数
    解决 paramiko 安装问题 Unable to find vcvarsall.bat
    20141202
    js
  • 原文地址:https://www.cnblogs.com/dlutxm/p/2190771.html
Copyright © 2011-2022 走看看