输入数据文件:的Hadoop MapReduce的实践
名,月,类别,支出
hitesh,1,A1,10020
hitesh,2,A2,10300
hitesh,3,A3,10400
hitesh,4,A4,11000
hitesh,5,A1,21000
hitesh,6,A2,5000
hitesh,7,A3,9000
hitesh,8,A4,1000
hitesh,9,A1,111000
hitesh,10,A2,12000
hitesh,11,A3,71000
hitesh,12,A4,177000
kuwar,1,A1,10700
kuwar,2,A2,17000
kuwar,3,A3,10070
kuwar,4,A4,10007
人明智的总支出和计数独特的类别花费。 (输出需要的样子:姓名,总支出,独特的类别总数)
我曾尝试.....我的代码
人 - 聪明的总支出
public class Emp
{
public static class MyMap extends Mapper<LongWritable,Text,Text,IntWritable>
{
public void map(LongWritable k,Text v, Context con)
throws IOException, InterruptedException
{
String line = v.toString();
String[] w=line.split(",");
String person=w[0];
int exp=Integer.parseInt(w[3]);
con.write(new Text(person), new IntWritable(exp));
}
}
public static class MyRed extends Reducer<Text,IntWritable,Text,IntWritable>
{
public void reduce(Text k, Iterable<IntWritable> vlist, Context con)
throws IOException , InterruptedException
{
int tot =0;
for(IntWrit
able v:vlist)
tot+=v.get();
con.write(k,new IntWritable(tot));
}
}
public static void main(String[] args) throws Exception
{
Configuration c = new Configuration();
Job j= new Job(c,"person-wise");
j.setJarByClass(Emp.class);
j.setMapperClass(MyMap.class);
j.setReducerClass(MyRed.class);
j.setOutputKeyClass(Text.class);
j.setOutputValueClass(IntWritable.class);
Path p1 = new Path(args[0]);
Path p2 = new Path(args[1]);
FileInputFormat.addInputPath(j,p1);
FileOutputFormat.setOutputPath(j,p2);
System.exit(j.waitForCompletion(true) ? 0:1);
}
}
如何以获得该计划中独特类别的总数,以及如何使输出看起来像名称,总支出,独特类别的总数。
感谢
@aashish_soni完成编辑帖子 –
@aashish_soni这有助于解决您的问题吗? –