2017-11-14 5 views
0

5 개의 열과 많은 행이있는 다음 CSV 파일이 있습니다. 하지만 처음 6 행만 보여주고 있습니다.지도에서 키의 행을 선택적으로 선택하는 방법 reduce

Date,Food,Vitamin,Protein,NumStudents 
01/01/17, Pasta, A, Yes, 560 
01/01/17, Pizza, A, Yes, 730 
01/01/17, Burrito, C, Yes, 240 
02/01/17, Pizza, A, Yes, 340 
02/01/17, Pasta, B, Yes, 450 
02/01/17, Beef, B, Yes, 450 

피자와 파스타 만있는 특정 날짜에 NumStudents의 합계를 찾고 싶습니다.

본질적으로 01/01/17의 경우 피자와 파스타에는 NumStudents를 합산해야하지만 Burrito에는 합산하지 않아도됩니다.

예상 출력

01/01/17 1290 
02/01/17 790 

출력 내가 음식의 3 종류의 NumStudents을 요약 할 수 있어요하지만 선택적으로 제외하는 방법을 모르는 내 코드에서

01/01/17 1530 
02/01/17 1240 

받고 있어요 mapper에있는 복합 키의 일부 음식 어떤 생각을 어떻게해야합니까? 다음은

내 코드

public class GroupMR { 

    public static class GroupMapper extends Mapper<LongWritable, Text, DateYear, IntWritable> { 




     public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 

      String line = value.toString(); 
      String[] keyvalue = line.split(","); 
      monyeartext.set(new Text(keyvalue[0])); 

      //populat.set(Integer.parseInt(keyvalue[5])); 
      termText.set(keyvalue[1]); 
      try { 
       numpass.set(Integer.parseInt(keyvalue[4])); 
      }catch (NumberFormatException e){ 
       System.out.println("not a number"); 
      } 
      DateYear monyear = new DateYear(monyeartext, termText); 
      context.write(monyear, numpass); 

     } 
    } 
입니다

답변

0

문자열 [] 키 값 = line.split ("");

Please add a filter after this line 

if(!(keyvalue[2].equals("Pasta") ||keyvalue[2].equals("Pizza"))){ 
    // If the food item is not pizza or pasta then return 
    return; 
} 
+0

덕분에이 일을 많이! 내 결과가 한 달에 한 번 정렬됩니다. 월 단위로 연도별로 정렬하여 매년 값을 출력하도록하려면 어떻게해야합니까? – Alex

+0

감사의 말을하는 좋은 방법이 있습니다. 그것은 상향 투표입니다 : D. 1. 출력이 ASCII 조합 순서로 정렬됩니다. 그리고 달이 아닙니다 (월이 그 달과 같음) 2. 즉시 결과를 얻으려면 MAPPER에서 날짜 형식을 변경하십시오. YYYY/MM/DD. 지금 DD/MM/YYYY – KrazyGautam

+0

나는 당신이 의미하는 것을 얻지 못했습니다. mapper에서 형식을 어떻게 변경해야하는지 조금 더 설명해 주시겠습니까? – Alex

3

코드를 줄였습니다. 나는 월과 날짜 만 선택했으며, 연도를 기준으로 정렬됩니다.

public class GroupMR { 

public static class GroupMapper extends Mapper<LongWritable, Text, Text, Text> { 

    Text numpass = null; 
    Text monyeartext = null; 

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
     String line = value.toString(); 
     String[] keyvalue = line.split(","); 
     String[] monyeartext1 = keyvalue[0].split("/"); 
     monyeartext = new Text(monyeartext1[2] + "/" +monyeartext1[0]); 
     numpass = new Text(keyvalue[1] + "-" + keyvalue[4]); 
     context.write(monyeartext, numpass); 

    } 
} 

public static class GroupReducer extends Reducer<Text, Text, Text, IntWritable> { 
    public void reduce(Text key, Iterable<Text> values, Context context) 
      throws IOException, InterruptedException { 
     boolean doesExist = false; 
     int sum = 0;   
     for (Text val : values) { 
      String[] val2 = val.toString().split("-"); 
      if (val2[0].equals("Pizza") || val2[0].equals("Pasta")){ 
       sum += Integer.parseInt(val2[1]); 
      } 
     } 
     context.write(key, new IntWritable(sum)); 
    } 
} 

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { 
    Configuration conf = new Configuration(); 
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); 
    if (otherArgs.length < 2) { 
     System.err.println("Usage: wordcount <in> [<in>...] <out>"); 
     System.exit(2); 
    } 
    Job job = Job.getInstance(conf, "GroupMR"); 
    job.setJarByClass(GroupMR.class); 
    job.setMapperClass(GroupMapper.class); 

    job.setReducerClass(GroupReducer.class); 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(Text.class); 

    //for (int i = 0; i < otherArgs.length - 1; ++i) { 
     FileInputFormat.addInputPath(job, new Path(otherArgs[0])); 
    // } 

    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); 
    System.exit(job.waitForCompletion(true) ? 0 : 1); 
} 

}