我有mapreduce的和赋值,我对mapreduce编程很陌生。我想计算每年和特定城市的平均值、最小值和最大值。这是我的样本输入
卡尔加里,AB,2009-01-076046801264,2.5207754,0.065721168,0.025668362,0.972051954,0.037000279,0.022319018,,,0.0036411449,,,0.002936745,,,0.016723641卡尔加里,AB,2009-12-3060462012694,2.0517769654,0.060114973,0.034026918,1.503277516,0.054219005,0.023258217,,,0.00354166,,,,003361414,,,0.122375131卡尔加里,AB,2010-01-0660468012266,4.015745522,0.097792741,0.032738892,0.368454554,0.019228992,0.032882053,,,0.004778065,,0.00319094444,,0.064203865卡尔加里,AB,2010-01-1360468012551,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.52077540.065721168,不完全相同,但第三个和第四个逗号后的所有数字),并找到平均值、最小值和最大值
并且在输出中应该是这样的:
卡尔加里2009年平均值:",分钟;",最大值:"卡尔加里2010年平均值:",分钟;",最大值:"
我试图使用这个代码来查找每一行中的值,但由于每一行的数据集不相同,我出现了错误(在该部分中没有数据或大于此长度)
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];
你能帮我做这个项目吗?
查看您的输入,您的记录似乎被空格隔开。您可以首先使用"进行拆分,然后获得单个值并将其用于计算
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:0065721168城市:卡尔加里日期:2009-12-30版本号:2.051769654版本号:0.060114973城市:卡尔加里日期:2010-01-06 v1:4.015745522v2:0097792741城市:卡尔加里日期:2010年1月13日版本号:3.006492921版本号:0.09051656城市:卡尔加里日期:2009-01-07 v1:2.5207754v2:0065721168