如何编写MR单元测试来读取基于avro的模式记录并发出文本基本键值

时间:2014-06-20 09:27:48

标签: avro mrunit

即使我有类似的要求。我想阅读具有一些模式和发出文本数据类型键值的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的记录,并发出具有text数据类型的键值对。 以下是我的mapper类:

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

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

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

0 个答案:

没有答案