【问题标题】:AVRO Mapreduce - ClassCastException: org.apache.avro.generic.GenericData$Record cannot be cast to AvroAVRO Mapreduce - ClassCastException:org.apache.avro.generic.GenericData$Record 无法转换为 Avro
【发布时间】:2018-01-05 09:06:19
【问题描述】:

首先,我通过 oozie 将 mapreduce 作为 java 操作运行。使用 AvroTools 提取 avro 文件的模式,然后将其编译为 Java 类 (RxAvro.java)。

每当我尝试使用 Avro 对象时,都会收到以下 ClassCastException: java.lang.ClassCastException:org.apache.avro.generic.GenericData$Record 无法转换为 com.xxx.yyy.zz.jobs.actions.test.RxAvro

2018-01-05 07:46:07,038 WARN [main] org.apache.hadoop.mapred.YarnChild:

Exception running child : java.lang.ClassCastException: org.apache.avro.generic.GenericData$Record cannot be cast to com.xxx.yyy.zzz.jobs.actions.test.RxAvro
        at com.xxx.yyy.zzz.jobs.actions.test.AvroParqueMapreduce$Map.map(AvroParqueMapreduce.java:70)
        at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:145)
        at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
        at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:422)
        at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1796)
        at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)

MapReduce 代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
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.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import parquet.avro.AvroParquetInputFormat;

import java.io.IOException;


public class AvroParqueMapreduce extends Configured implements Tool {


    /**
     * Main entry point for the example.
     *
     * @param args arguments
     * @throws Exception when something goes wrong
     */
    public static void main(final String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new AvroParqueMapreduce(), args);
        System.exit(res);
    }

    /**
     * The MapReduce driver - setup and launch the job.
     *
     * @param args the command-line arguments
     * @return the process exit code
     * @throws Exception if something goes wrong
     */
    public int run(final String[] args) throws Exception {


        Path inputPath = new Path(args[0]);
        Path outputPath = new Path(args[1]);

        Configuration conf = super.getConf();

        Job job = new Job(conf);
        job.setJarByClass(AvroParqueMapreduce.class);

        job.setInputFormatClass(AvroParquetInputFormat.class);
        AvroParquetInputFormat.setInputPaths(job, inputPath);

        job.setMapperClass(Map.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputFormatClass(TextOutputFormat.class);
        FileOutputFormat.setOutputPath(job, outputPath);

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

    public static class Map extends Mapper<Void, RxAvro, Text, Text> {

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

            context.write(new Text(value.getObjectId().toString()),
                    new Text(value.getCollectionInterval().toString()));
        }
    }

任何帮助将不胜感激。

【问题讨论】:

    标签: java hadoop mapreduce avro mapper


    【解决方案1】:

    您的代码中有几个问题。您应该如下正确设置输入格式类,AvroPArquetInputFormat 对于 avro 数据不正确。

        job.setInputFormatClass(AvroKeyInputFormat.class);
    

    除此之外,您还需要设置输入架构

        AvroJob.setInputKeySchema(job, RxAvro.getClassSchema());
    

    本教程可以作为起点https://dzone.com/articles/mapreduce-avro-data-files

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-03
      • 2015-09-21
      • 1970-01-01
      • 2020-02-10
      • 2019-07-02
      • 2015-11-18
      • 2013-07-01
      • 1970-01-01
      相关资源
      最近更新 更多