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);


    }

    }




  • 相关阅读:
    【爬虫】微信读书笔记如何导出到本地?
    工作面试题——值得一看
    算法实验三——图的遍历算法
    数据结构之排序算法
    汇编语言学习总结
    洛谷—— P2658 汽车拉力比赛
    洛谷—— P1419 寻找段落
    CODEVS——T 1700 施工方案第二季
    洛谷—— P3811 【模板】乘法逆元
    JAVA中传递的值还是引用的问题
  • 原文地址:https://www.cnblogs.com/dlutxm/p/2190771.html
Copyright © 2011-2022 走看看