2

甚至我也有类似的要求。我想读取具有某些模式的 avro 记录并发出文本数据类型键值,我需要为此编写 MR 单元测试用例。我编写了以下代码,但它给了我以下异常:

   org.apache.avro.AvroTypeException: Found string, expecting xyz.abc.Myschema
   at org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:231)
   at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
   .....
   .....

以下是我在设置功能中的代码库:

MyMapper myMapper = new MyMapper();
    mapDriver = new MapDriver<AvroKey<Myschema>, NullWritable, Text, Text>();
    mapDriver.setMapper(myMapper);
    Configuration configuration = mapDriver.getConfiguration();
    //Copy over the default io.serializations. If you don't do this then you will
    //not be able to deserialize the inputs to the mapper
    String[] strings = mapDriver.getConfiguration().getStrings("io.serializations");
    String[] newStrings = new String[strings.length +1];
    System.arraycopy( strings, 0, newStrings, 0, strings.length );
    newStrings[newStrings.length-1] = AvroSerialization.class.getName();

    //Now you have to configure AvroSerialization by sepecifying the key
    //writer Schema and the value writer schema.
    configuration.setStrings("io.serializations", newStrings);
    Text x = new Text();

    Configuration conf = mapDriver.getConfiguration();
    AvroSerialization.addToConfiguration(conf);
    AvroSerialization.setKeyWriterSchema(conf, Schema.create(Schema.Type.STRING));

    AvroSerialization.setKeyReaderSchema(conf, new Myschema().getSchema());

    Job job = new Job(conf);
    job.setMapperClass(MyMapper.class);
    job.setInputFormatClass(AvroKeyInputFormat.class);
    AvroJob.setInputKeySchema(job, new Myschema().getSchema());
    job.setOutputKeyClass((new Text()).getClass());

我需要读取具有 Myschema 架构的基于 avro 的记录,并发出具有文本数据类型的键值对。以下是我的映射器类:

    public class MyMapper extends Mapper<AvroKey<Myschema>, NullWritable, Text, Text>...

    protected void map(AvroKey<Myschema> key, NullWritable value, Context context)...

有人可以检查我是否缺少任何配置参数并帮助我吗?

4

0 回答 0