Mapreduce-java:数组列表

时间:2016-04-10 19:18:40

标签: java eclipse hadoop arraylist mapreduce

我有mapreduce的任务,我是mapreduce编程的新手。 我想计算每年和特定城市的平均值,最小值和最大值。 所以这是我的样本输入

  

卡尔加里,2009-01-07,604680,12694,2.5207754,0.065721168,0.025668362,0.972051954,0.037000279,0.022319018 ,,, 0.003641149 ,,, 0.002936745 ,,, 0.016723641   卡尔加里,2009-12-30,604620,12694,2.051769654,0.060114973,0.034026918,1.503277516,0.054219005,0.023258217 ,,, 0.00354166 ,,, 0.003361414 ,,, 0.122375131   卡尔加里,2010-01-06,604680,12266,4.015745522,0.097792741,0.032738892,0.368454554,0.019228992,0.032882053 ,,, 0.004778065 ,,, 0.003190444 ,,, 0.064203865   卡尔加里,2010-01-13,604680,12551,3.006492921,0.09051656,0.041508534,0.215395047,0.012081755,0.023706119 ,,, 0.004231772 ,,, 0.003083003 ,,, 0.155212503

我知道如何找到城市和年份 我正在使用此代码:

String line = value.toString();
    String[] tokens = line.split(",");
    String[] date = tokens[2].split("-");
    String year = date[0];
    String location = tokens[0];

现在我想在每一行中找到这两个数字(例如,2.5207754,0.065721168,不完全相同,但是在第三个和第四个逗号之后的所有数字)并找到一个平均值,一个最小值和最大值。

并在输出中应如下所示:

  

2009年卡尔加里平均水平:“”,分钟; “”,max:“”   2010年卡尔加里平均水平:“”,分钟; “”,max:“”

我试图使用这段代码来查找每一行中的值,但由于数据集在每行中都不一样,我得到错误(在该部分中没有数据或更大的长度)

float number = 0;
    float number2 = 0 ;
    char a;
    char c;
    a = line.charAt(34);
    c = line.charAt(44);
    if (a == ',') 
    { 
        number = Float.parseFloat(line.substring(35, 44));
    }
    else 
    {
        number = Float.parseFloat(line.substring(35, 46));
    }

    if (c == ',')
    {
        number2 = Float.parseFloat(line.substring(45, 56));

    } else 
    {
        number = Float.parseFloat(line.substring(47, 58));
    }

    Text numbers = new Text(number + " " + number2 + " ");

然后我尝试使用此代码,并且与上面相同它不起作用:

String number = tokens[4];
String number2 = tokens[5];

你能帮我做这个项目吗?

1 个答案:

答案 0 :(得分:0)

查看您的输入,您的记录似乎被空格分隔。您可以先使用" "然后获取各个值并将其用于计算

        String[] arr = line.split(" ");
        for(String val : arr){
            String[] dataArr = val.split(",");
            String city  = dataArr[0];
            String date = dataArr[2];
            String v1 = dataArr[5];
            String v2 = dataArr[6];
            System.out.println("city: "+city +" date: "+ date +" v1: "+ v1+"v2: "+ v2);
        }

城市:卡尔加里日期:2009-01-07 v1:2.5207754v2:0.065721168 城市:卡尔加里日期:2009-12-30 v1:2.051769654v2:0.060114973 城市:卡尔加里日期:2010-01-06 v1:4.015745522v2:0.097792741 城市:卡尔加里日期:2010-01-13 v1:3.006492921v2:0.09051656 城市:卡尔加里日期:2009-01-07 v1:2.5207754v2:0.065721168