intellij 아이디어 로 컬 개발 디 버 깅 hadop 방법

6940 단어 Hadoop
전재 출처 를 밝 혀 주 십시오: http://blog.csdn.net/programmer_wei/article/details/45286749
나의 intellij idea 버 전 은 14, hadop 버 전 2.6 이 고 의 날씨 통계 소스 코드 를 예 로 들 었 다.
데이터 세트http://hadoopbook.com/code.html1901 년 과 1902 년 의 데 이 터 를 다운로드 할 수 있 습 니 다.
package com.hadoop.maxtemperature;

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MaxTemperatureMapper
        extends Mapper {  // 1
    private static final int MISSING = 9999;
    @Override
    public void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        String line = value.toString();
        String year = line.substring(15, 19);
        int airTemperature;
        if (line.charAt(87) == '+') { // parseInt doesn't like leading plus signs
            airTemperature = Integer.parseInt(line.substring(88, 92));
        } else {
            airTemperature = Integer.parseInt(line.substring(87, 92));
        }
        String quality = line.substring(92, 93);
        if (airTemperature != MISSING && quality.matches("[01459]")) {
            context.write(new Text(year), new IntWritable(airTemperature));
        }
    }
}
package com.hadoop.maxtemperature;


import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class MaxTemperatureReducer
        extends Reducer {
    @Override
    public void reduce(Text key, Iterable values,
                       Context context)
            throws IOException, InterruptedException {
        int maxValue = Integer.MIN_VALUE;
        for (IntWritable value : values) {
            maxValue = Math.max(maxValue, value.get());
        }
        context.write(key, new IntWritable(maxValue));
    }
}
package com.hadoop.maxtemperature;


import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MaxTemperature {
    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println("Usage: MaxTemperature  ");
            System.exit(-1);
        }
        Job job = new Job();
        job.setJarByClass(MaxTemperature.class);
        job.setJobName("Max temperature");

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setMapperClass(MaxTemperatureMapper.class);
        job.setReducerClass(MaxTemperatureReducer.class);

        job.setOutputKeyClass(Text.class);              // 1
        job.setOutputValueClass(IntWritable.class);

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

1. 우선 hadop 홈 페이지 에서 hadop 을 로 컬 로 다운로드 합 니 다. (환경 변 수 를 설정 할 필요 가 없고 hadop 의 가방 만 다운로드 하면 됩 니 다.)
2. intellij idea 를 열 어 빈 항목 을 만 들 고 소스 코드 를 붙 여 넣 습 니 다. 그림 과 같 습 니 다.

2. 이때 코드 중의 많은 종 류 는 식별 할 수 없 으 니 서 두 르 지 마 세 요.다음은 procject structure 를 열 고 왼쪽 에서 modules 를 찾 습 니 다.

3. 아래 화살 표를 클릭 하여 일 일 휴가 jar 또는 디 렉 터 리

4. 방금 다운로드 한 hadop 디 렉 터 리 의 share 폴 더 에 해당 하 는 디 렉 터 리 를 추가 합 니 다.

5. 왼쪽 아 티 팩 트 를 클릭 하여 빈 jar 추가

6. jar 의 이름 을 입력 하 십시오. 여기 max temperature 를 입력 하 십시오.

7. output layot 아래 의 작은 화살 표를 클릭 하고 module output 를 선택 한 다음 에 우리 의 항목 을 선택 하고 확인 을 클릭 합 니 다.

8. 이때 방금 표 시 된 각종 가방 과 유형의 오류 정 보 는 모두 사 라 졌 다.

9. 다음 오른쪽 상단 에 있 는 edit configurations 를 클릭 하고 현재 application 이 없 으 면 application 을 새로 만 듭 니 다.

10. application 의 이름 은 여기 서 MaxTemperature 를 지 은 다음 에 main class 는 org. apache. hadoop. util. RunJar 를 입력 하고 program arguments 를 클릭 하여 인 자 를 작성 해 야 합 니 다. 다음 과 같 습 니 다.
그 중에서 첫 번 째 매개 변 수 는 procject structure 에 작 성 된 jar 파일 경로 이 고 두 번 째 매개 변 수 는 파일 을 입력 하 는 디 렉 터 리 이 며 두 번 째 매개 변 수 는 출력 파일 의 경로 입 니 다.

11. 그 다음 에 우 리 는 입력 경 로 를 새로 만 들 고 입력 파일 을 넣 어야 합 니 다. (출력 파일 은 만 들 지 마 십시오. 이것 은 시스템 에서 직접 만 듭 니 다)

12. 실행 을 클릭 하면 오류 알림 이 있 습 니 다. 종 류 를 찾 을 수 없 음 을 표시 합 니 다.

13. 자 료 를 조회 한 결과 방금 매개 변 수 를 작성 한 곳 에 매개 변수 가 있 음 을 발 견 했 습 니 다. main 함수 가 있 는 유형의 경 로 를 추가 해 야 합 니 다.

14 、 다시 클릭 하여 실행, 실행 성공 발견

15. 이때 프로젝트 디 렉 터 리 아래 에 output 디 렉 터 리 를 생 성 했 고 그 안에 실행 결 과 를 저장 했다.

전재 출처 를 밝 혀 주 십시오: http://blog.csdn.net/programmer_wei/article/details/45286749

좋은 웹페이지 즐겨찾기