복붙노트

[HADOOP] MapReduce 출력 ArrayWritable

HADOOP

MapReduce 출력 ArrayWritable

간단한 MapReduce- 작업에서 ArrayWritable 출력을 얻으려고합니다. 비슷한 문제가있는 몇 가지 질문을 발견했지만 자체적으로 문제를 해결할 수는 없습니다. 그래서 당신의 도움을 기다리고 있습니다. 감사 :)!

입력 : 문장이있는 텍스트 파일.

출력은 다음과 같아야합니다.

<Word, <length, number of same words in Textfile>>
 Example: Hello  5  2 

내 직업에서받는 결과는 다음과 같습니다.

hello WordLength_V01$IntArrayWritable@221cf05
test WordLength_V01$IntArrayWritable@799e525a

나는 문제가 IntArrayWritable에서 하위 클래스에 있다고 생각하지만,이 문제를 해결하기 위해 올바른 수정을 얻지 못했습니다. 우리는 하둡 2.5를 가지고 있습니다. 이 결과를 얻으려면 다음 코드를 사용합니다.

주요 방법 :

public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word length V1");

    // Set Classes
    job.setJarByClass(WordLength_V01.class);
    job.setMapperClass(MyMapper.class);
    // job.setCombinerClass(MyReducer.class);
    job.setReducerClass(MyReducer.class);

    // Set Output and Input Parameters
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntArrayWritable.class);

    // Number of Reducers
    job.setNumReduceTasks(1);

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

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

매퍼 :

public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {

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

    // Map Method
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

        // Use Tokenizer
        StringTokenizer itr = new StringTokenizer(value.toString());

        // Select each word
        while (itr.hasMoreTokens()) {
            word.set(itr.nextToken());

            // Output Pair
            context.write(word, one);
        }
    }
}

감속기 :

public static class MyReducer extends Reducer<Text, IntWritable, Text, IntArrayWritable> {

    // Initialize Variables
    private IntWritable count = new IntWritable();
    private IntWritable length = new IntWritable();

    // Reduce Method
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        // Count Words
        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }

        count.set(sum);

        // Wordlength
        length.set(key.getLength());

        // Define Output
        IntWritable[] temp = new IntWritable[2];
        IntArrayWritable output = new IntArrayWritable(temp);

        temp[0] = count;
        temp[1] = length;

        // Output
        output.set(temp);
        context.write(key, new IntArrayWritable(output.get()));
    }
}

아강

public static class IntArrayWritable extends ArrayWritable {
    public IntArrayWritable(IntWritable[] intWritables) {
        super(IntWritable.class);
    }

    @Override
    public IntWritable[] get() {
        return (IntWritable[]) super.get();
    }

    @Override
    public void write(DataOutput arg0) throws IOException {
        for(IntWritable data : get()){
            data.write(arg0);
        }
    }
}   

솔루션을 찾으려면 다음 링크를 사용하십시오.

나는 어떤 생각이라도 정말로 고맙다!

-------- 해결책 --------

새 하위 클래스 :

public static class IntArrayWritable extends ArrayWritable {

    public IntArrayWritable(IntWritable[] values) {
        super(IntWritable.class, values);
    }

    @Override
    public IntWritable[] get() {
        return (IntWritable[]) super.get();
    }

    @Override
    public String toString() {
        IntWritable[] values = get();
        return values[0].toString() + ", " + values[1].toString();
    }
}

새로운 방법 줄이기 :

public void reduce(Text key, Iterable<IntWritable> values,
            Context context) throws IOException, InterruptedException {

        // Count Words
        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }

        count.set(sum);

        // Wordlength
        length.set(key.getLength());

        // Define Output
        IntWritable[] temp = new IntWritable[2];
        temp[0] = count;
        temp[1] = length;

        context.write(key, new IntArrayWritable(temp));
}

해결법

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

    1.모든 것이 완벽 해 보입니다. 배열 대신 문자열을 반환하는 하위 클래스에 printStrings () 메서드를 하나 더 작성해야합니다. 내장 된 toString ()은 값 대신 출력에 주소를 제공하는 이유 인 문자열 배열을 반환합니다.

    모든 것이 완벽 해 보입니다. 배열 대신 문자열을 반환하는 하위 클래스에 printStrings () 메서드를 하나 더 작성해야합니다. 내장 된 toString ()은 값 대신 출력에 주소를 제공하는 이유 인 문자열 배열을 반환합니다.

    public String printStrings() {
         String strings = "";
            for (int i = 0; i < values.length; i++) {
             strings = strings + " "+ values[i].toString();
           }
          return strings;
        }
    
  2. from https://stackoverflow.com/questions/28914596/mapreduce-output-arraywritable by cc-by-sa and MIT license