복붙노트

[HADOOP] 예상 org.apache.hadoop.io.Text를 해결하는 방법, mapreduce 작업에서 org.apache.hadoop.io.LongWritable을 받았습니다.

HADOOP

예상 org.apache.hadoop.io.Text를 해결하는 방법, mapreduce 작업에서 org.apache.hadoop.io.LongWritable을 받았습니다.

youtube 데이터 세트에서 일부 정보를 분석 할 수있는 작업을 작성하려고합니다. 드라이버 클래스의 맵에서 출력 키를 올바르게 설정했다고 생각하지만 여전히 위의 오류가 발생합니다. 코드와 예외를 게시하고 있습니다. 이리,

매퍼

public class YouTubeDataMapper extends Mapper<LongWritable,Text,Text,IntWritable>{

private static final IntWritable one = new IntWritable(1); 
private Text category = new Text(); 
public void mapper(LongWritable key,Text value,Context context) throws IOException, InterruptedException{
    String str[] = value.toString().split("\t");
    category.set(str[3]);
    context.write(category, one);
}

}

감속기 클래스

public class YouTubeDataReducer extends Reducer<Text,IntWritable,Text,IntWritable>{

public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException{
    int sum=0;
    for(IntWritable count:values){
        sum+=count.get();
    }
    context.write(key, new IntWritable(sum));
}

}

드라이버 클래스

public class YouTubeDataDriver {

public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();

    @SuppressWarnings("deprecation")
    Job job = new Job(conf, "categories");
    job.setJarByClass(YouTubeDataDriver.class);

    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);
    // job.setNumReduceTasks(0);
    job.setOutputKeyClass(Text.class);// Here i have set the output keys
    job.setOutputValueClass(IntWritable.class);

    job.setMapperClass(YouTubeDataMapper.class);
    job.setReducerClass(YouTubeDataReducer.class);

    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    Path out = new Path(args[1]);
    out.getFileSystem(conf).delete(out);
    job.waitForCompletion(true);

}

}

내가 얻은 예외

드라이버 클래스에서 출력 키를 설정했습니다

    job.setOutputKeyClass(Text.class);// Here i have set the output keys
    job.setOutputValueClass(IntWritable.class);

그러나 왜 여전히 오류가 발생합니까? 도와주세요, mapreduce를 처음 사용합니다

해결법

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

    1.mapper () 메서드의 이름을 map ()으로 바꿉니다 (공식 문서 참조).

    mapper () 메서드의 이름을 map ()으로 바꿉니다 (공식 문서 참조).

    일어나고있는 일은 실제로 매퍼가 처리하는 데이터가 없다는 것입니다. mapper () 메소드를 입력하지 않으므로 (map () 메소드를 찾고 있으므로) 맵 단계를 변경하지 않은 채로 두십시오. 이는 맵 출력 키가 여전히 LongWritable임을 의미합니다.

    여담으로,

    String str[] = value.toString().split("\t");
    category.set(str[3]);
    

    매우 위험합니다. 모든 입력 데이터에 3 자 이상이 포함되어 있다고 가정하는 것은 위험합니다. 많은 양의 데이터를 처리 할 때는 거의 항상 예상 한 형식이 아닌 일부 데이터가있을 수 있으며 이로 인해 전체 작업이 중단되는 것을 원하지 않습니다. 다음과 같은 작업을 고려하십시오.

    String valueStr = value.toString();
    if (valueStr != null) {
        String str[] = valueStr.split("\t");
        if (str[] != null && str.size > 3) {
            category.set(str[3]);
            context.write(category, one);
        }
    }
    
  2. ==============================

    2.아래 코드 (Object로 LongWritable 업데이트)가 나를 위해 작동합니다-

    아래 코드 (Object로 LongWritable 업데이트)가 나를 위해 작동합니다-

    import java.io.IOException;
    import java.util.StringTokenizer;
    
    import org.apache.hadoop.conf.Configuration;
    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.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
    
    public class YouTubeDataDriver {
    
        public static class YouTubeDataMapper
                extends Mapper<Object, Text, Text, IntWritable>{
    
            private final static IntWritable one = new IntWritable(1);
            private Text word = new Text();
    
            public void map(Object key, Text value, Context context
            ) throws IOException, InterruptedException {
                StringTokenizer itr = new StringTokenizer(value.toString());
                while (itr.hasMoreTokens()) {
                    word.set(itr.nextToken());
                    context.write(word, one);
                }
            }
        }
    
        public static class YouTubeDataReducer
                extends Reducer<Text,IntWritable,Text,IntWritable> {
            private IntWritable result = new IntWritable();
    
            public void reduce(Text key, Iterable<IntWritable> values,
                               Context context
            ) throws IOException, InterruptedException {
                int sum = 0;
                for (IntWritable val : values) {
                    sum += val.get();
                }
                result.set(sum);
                context.write(key, result);
            }
        }
    
        public static void main(String[] args) throws Exception {
            Configuration conf = new Configuration();
    
            @SuppressWarnings("deprecation")
            Job job = new Job(conf, "categories");
            job.setJarByClass(YouTubeDataDriver.class);
    
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
            // job.setNumReduceTasks(0);
            job.setOutputKeyClass(Text.class);// Here i have set the output keys
            job.setOutputValueClass(IntWritable.class);
    
            job.setMapperClass(YouTubeDataMapper.class);
            job.setReducerClass(YouTubeDataReducer.class);
    
            job.setInputFormatClass(TextInputFormat.class);
            job.setOutputFormatClass(TextOutputFormat.class);
    
            FileInputFormat.addInputPath(job, new Path(args[0]));
            FileOutputFormat.setOutputPath(job, new Path(args[1]));
            Path out = new Path(args[1]);
            out.getFileSystem(conf).delete(out);
            job.waitForCompletion(true);
    
        }
    
    }
    
  3. from https://stackoverflow.com/questions/49912555/how-to-solve-expected-org-apache-hadoop-io-text-received-org-apache-hadoop-io-l by cc-by-sa and MIT license