首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVRO ClassCastException: org.apache.avro.generic.GenericData$Record不能转换为Avro

AVRO ClassCastException: org.apache.avro.generic.GenericData$Record不能转换为Avro
EN

Stack Overflow用户
提问于 2018-01-05 09:06:19
回答 1查看 2K关注 0票数 0

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

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

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
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()));
        }
    }

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2018-01-10 03:24:24

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

代码语言:javascript
复制
    job.setInputFormatClass(AvroKeyInputFormat.class);

除此之外,您还需要设置输入模式。

代码语言:javascript
复制
    AvroJob.setInputKeySchema(job, RxAvro.getClassSchema());

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48110304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档