복붙노트

[HADOOP] hadoop 출력 파일에 커스텀 이름을주는 법

HADOOP

hadoop 출력 파일에 커스텀 이름을주는 법

출력 파일을 2012117-part-r-00000 형식으로 원합니다. 기본적으로 출력 파일에 날짜가 추가되도록하여 날짜별로 파일을 정렬 할 수 있도록합니다. OutputFormat과 FileOutputFormat을 보았지만 제 경우에는 도움이되지 않습니다.

해결법

  1. ==============================

    1.MR 작업의 출력 파일 이름에는 유연성이별로 없습니다. MultipleOutputFormat의 서브 클래스를 사용하십시오.

    MR 작업의 출력 파일 이름에는 유연성이별로 없습니다. MultipleOutputFormat의 서브 클래스를 사용하십시오.

    MultipleOutputFormat # generateFileNameForKeyValue 메소드를 구현하고,이 메소드의 입력을 무시하고 date + -part- r- + mapred.task.partition 패턴의 문자열을 리턴해야합니다. mapred.task.partition은 int이므로 미리 0을 적절하게 채워야합니다.

  2. ==============================

    2.방금 새 API를 발견했다. org.apache.hadoop.mapreduce.lib.output.MultipleOutputs와 addNamedOutput () 메소드를 사용할 수있다.

    방금 새 API를 발견했다. org.apache.hadoop.mapreduce.lib.output.MultipleOutputs와 addNamedOutput () 메소드를 사용할 수있다.

  3. ==============================

    3.출력 파일 이름을 변경하는 방법에는 두 가지가 있습니다.

    출력 파일 이름을 변경하는 방법에는 두 가지가 있습니다.

    1. Java 클래스 MultipleOutputFormat을 사용합니다.

    // job.setOutputFormatClass(TextOutputFormat.class);
    LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
    MultipleOutputs.addNamedOutput(job,“20180318”, TextOutputFormat.class, Text.class, IntWritable.class);
    

    2. 사용 방법

    job.getConfiguration().set(“mapreduce.output.basename”, “20180318”);
    

    참조 링크 : http://data-flair.training/forums/topic/in-mapreduce-how-to-change-the-name-of-output-file-from-part-r-00000

  4. ==============================

    4.Mapper / Reducer 클래스에서 Multiple Output Format을 사용하면이 작업을 수행 할 수 있습니다. 예 :

    Mapper / Reducer 클래스에서 Multiple Output Format을 사용하면이 작업을 수행 할 수 있습니다. 예 :

    import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
    

    Mapper / Reducer 클래스에 MultipleOutputs 객체를 만듭니다.

    private MultipleOutputs<Text, NullWritable> _multipleOutputs;
    

    설정에서 다음을 수행 할 수 있습니다.

    _multipleOutputs = new MultipleOutputs<Text, NullWritable>(context);
    

    그런 다음 map / reduce 메소드에서 다음을 수행 할 수 있습니다.

    _multipleOutputs.write(new Text(whatever you want to emit),new Text(whatever you want to emit),"xyz-m");
    

    이것은 xyz-m-00000과 같은 파일 이름을 줄 것입니다.

  5. ==============================

    5.출력 파일에 날짜를 추가하려면 다음을 사용하십시오. 다중 출력 Apache Hadoop

    출력 파일에 날짜를 추가하려면 다음을 사용하십시오. 다중 출력 Apache Hadoop

    다음은 익숙해지기위한 샘플 코드입니다.

    MultipleOutputs<Text, Text> mos;
    
    @Override
    public void setup(Context context) {
        mos = new MultipleOutputs(context);
    }
    
    private IntWritable totalCommits = new IntWritable();
    
    @Override
    public void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {
        int Count = 0;
        for (IntWritable val : values) {
            Count += val.get();
        }
        totalCommits.set(Count);
        String[] keySplit = key.toString().split(",");
    
        mos.write(new Text(keySplit[1]), new Text(totalCommits.toString()),keySplit[0]);
    }
    
    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        mos.close();
    }
    

    write 메소드에는 key, value, filename 순으로 매개 변수가 있어야합니다. 키 및 값 유형은 코드의 첫 번째 행에있는 유형 선언과 동일해야하며 파일 이름은 문자열이어야합니다. filename에 "-", ":"등과 같은 특수 문자를 사용할 수 없습니다.

  6. from https://stackoverflow.com/questions/8901499/how-to-give-a-custom-name-to-hadoop-output-files by cc-by-sa and MIT license