2017-11-28 3 views
1

필자는 쉼표로 구분 된 파일을 여러 열로 가져 와서 회사 이름, 고객 상호 작용 결과 및 발생한 횟수를 가져와야하는 프로젝트가 있습니다.
그럼 좋은 상호 작용에 나쁜 상호 작용의 비율을 계산해야합니다. Hadoop과 Java를 사용하고 있습니다.
나는 작업 맵과 Reduce을 사용하여 회사 이름과 좋고 나쁜 상호 작용의 수를 알려줍니다.하둡 (Hadoop) 백분율 얻기

내 문제는 하둡이 나에게 백분율을 나누어 줄 수있는 방법을 찾을 수 없다는 것입니다.
대부분의 회사에는 나쁜 상호 작용이 없습니다.

여기 나의

public class TermProjectReducer extends Reducer < Text, IntWritable, Text, IntWritable > 
{ 
    private IntWritable result = new IntWritable(); 
     @Override 
     public void reduce(Text key, Iterable <IntWritable> values, Context context) throws IOException, InterruptedException 
     { 
      int sum = 0; 
      for (IntWritable val : values) 
      { 
       sum += val.get(); 
      } 
      if (sum > 0) 
      { 
       result.set(sum); 
       context.write(key, result); 
      } 
     } 
    } 

이 내가 지금 무엇입니까 무엇의 예입니다 감소되어 내 MAPP

다음
public class TermProjectMapper extends Mapper<LongWritable, Text, Text, IntWritable> { 

    private final static IntWritable one = new IntWritable(1); 
    private Text word = new Text(); 

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

      String[] columb = value.toString().split(","); 
      String companyName = columb[5]; 
      String companyResponseToConsumer = columb[12]; 
      String lookfor = "closed without relief"; 

       if (companyResponseToConsumer.toLowerCase().contains(lookfor)) {companyResponseToConsumer="Bad";} 
       else {companyResponseToConsumer="Good";} 
       //System.out.println(companyResponseToConsumer); 
       if (companyName != "" && companyResponseToConsumer != "") 
       { 
        word.set (companyName + " " + companyResponseToConsumer); 
        context.write(word, one); 
       } 
     } 
     } 

입니다.

AMERICAN EAGLE MORTGAGE COMPANY,Good, 4 
AMERICAN EQUITY MORTGAGE,Good, 26 
AMERICAN EXPRESS COMPANY,Bad, 250 
AMERICAN EXPRESS COMPANY,Good, 9094 
AMERICAN FEDERAL MORTGAGE CORPORATION,Bad, 1 
AMERICAN FEDERAL MORTGAGE CORPORATION,Good, 3 
AMERICAN FINANCE HOUSE LARIBA,Good, 3 
AMERICAN FINANCIAL MORTGAGE COMPANY,Good, 3 
+0

Text의 감속기의 값으로 DoubleWritable을 사용하는 것이 좋습니다? –

+0

예, MapReduce 및 Java가 필요합니다. – Keg

+0

좋아, 당신의 감속기는 단어 수를하고 있습니다. 'Good'과'Bad' 만 카운트를 분리하려고 시도한 것은 무엇입니까? –

답변

0

기업을 집계하려면 키를 출력하여 감속기에서 결합해야합니다. 다른 말로하면, 좋은 값과 나쁜 값을 모두 같은 값으로 갖고 싶기 때문입니다.

나는 처음에 당신이 [1, 0] 또는 [0, 1]을 할 수 있다고 생각하지만, 다루기 쉬울 것 단지 1 또는 -1 대신 ("GOOD", 1)("BAD", 1)를 출력한다.

따라서, 예를 들어,

private final static IntWritable ONE = new IntWritable(1); 
private final static IntWritable NEG_ONE = new IntWritable(-1); 

... 

    IntWritable status; 
    if (companyResponseToConsumer.toLowerCase().contains(lookfor)) {status=NEG_ONE;} 
    else {status=ONE;} 

    if (!companyName.isEmpty()) 
    { 
     word.set (companyName); 
     context.write(companyName, status); 
    } 

지금, 감속기에있어서, 카운트 값뿐만 아니라, 비율을 계산 (하둡위한보다 효율적인 데이터 전송).

public class TermProjectReducer extends Reducer < Text, IntWritable, Text, IntWritable > 
{ 
    private IntWritable result = new IntWritable(); 

    @Override 
    public void reduce(Text key, Iterable <IntWritable> values, Context context) throws IOException, InterruptedException 
    { 
     int total = 0; 
     int good_sum = 0; 
     for (IntWritable val : values) 
     { 
      good_sum += (val.get() == 1 ? 1 : 0); 
      total += 1 
     } 
     if (total > 0) // Prevent division by zero 
     { 
      double percent = 1.0*good_sum/total; 
      // Round it to how every many decimal places, if you want 
      result.set(String.valueOf(percent)); // convert the floating number to a string 
     } else { 
      result.set("0.00"); 
     } 
     context.write(key, result); 
    } 
} 

그리고 난 단지 당신이 (1 - good) = bad 스스로 할 수있는 처리로 인해 다운 스트림에서 좋은 값을 계산 하였다.

또한, 나는 그것이 맵리 듀스 대신 하이브/스파크/돼지해야합니까 대신

+0

감사합니다. cricket_007 그게 전부입니다. 모든 것이 함께 작동하게하려면 약간의 시간이 걸렸지 만, 당신은 올바른 길로 나를 잡았습니다! 고맙습니다! – Keg