Package org.apache.avro.Schema

Examples of org.apache.avro.Schema.Parser


  }
 
  @Test
  public void testGenerateAvro3() {
    try {
      Parser parser = new Schema.Parser();
      Schema peopleSchema = parser.parse(new File(getTestResource("people.avsc").toURI()));
      GenericDatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(peopleSchema);
      DataFileWriter<GenericRecord> dfw = new DataFileWriter<GenericRecord>(datumWriter);
      File tempfile = File.createTempFile("karma-people", "avro");
     
      tempfile.deleteOnExit();
View Full Code Here


  public void setConf(Configuration conf) {
    super.setConf(conf);
    if (conf == null) return;
    mDataModel = AvroSerialization.createDataModel(conf);
    String schemaString = conf.get(CONF_GROUPING_SCHEMA);
    if (schemaString != null) mSchema = new Parser().parse(schemaString);
  }
View Full Code Here

import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;

public class GenericMain {
  public static void main(String[] args) throws IOException {
    Schema schema = new Parser().parse(new File("/home/skye/code/cloudera/avro/doc/examples/user.avsc"));
   
    GenericRecord user1 = new GenericData.Record(schema);
    user1.put("name", "Alyssa");
    user1.put("favorite_number", 256);
    // Leave favorite color null
View Full Code Here

     
      // configure projection schema, if any
      String projectionSchemaString = getConfigs().getString(config, "projectionSchemaString", null);
      Schema projectionSchema;
      if (projectionSchemaString != null) {
        projectionSchema = new Parser().parse(projectionSchemaString);
      } else {       
        String projectionSchemaFile = getConfigs().getString(config, "projectionSchemaFile", null);
        if (projectionSchemaFile != null) {
          try {
            projectionSchema = new Parser().parse(new File(projectionSchemaFile));
          } catch (IOException e) {
            throw new MorphlineCompilationException("Cannot parse external Avro projection schema file: " + projectionSchemaFile, config, e);
          }
        } else {
          projectionSchema = null;
        }
      }     
     
      if (projectionSchema != null) {
        AvroReadSupport.setRequestedProjection(conf, projectionSchema);
      }
     
      // configure reader schema, if any
      String readerSchemaString = getConfigs().getString(config, "readerSchemaString", null);
      Schema readerSchema;
      if (readerSchemaString != null) {
        readerSchema = new Parser().parse(readerSchemaString);
      } else {       
        String readerSchemaFile = getConfigs().getString(config, "readerSchemaFile", null);
        if (readerSchemaFile != null) {
          try {
            readerSchema = new Parser().parse(new File(readerSchemaFile));
          } catch (IOException e) {
            throw new MorphlineCompilationException("Cannot parse external Avro reader schema file: " + readerSchemaFile, config, e);
          }
        } else {
          readerSchema = null;
View Full Code Here

    assertEquals(Arrays.asList(10.0, 20.0), map.get("arrayField"));
  }

  @Test
  public void testToAvroBasic() throws Exception {
    Schema schema = new Parser().parse(new File("src/test/resources/test-avro-schemas/interop.avsc"));
    morphline = createMorphline("test-morphlines/toAvroWithSchemaFile");
   
    byte[] bytes = new byte[] {47, 13};
    byte[] fixed = new byte[16];
    Record jdoc1 = new Record();    
View Full Code Here

    }
  }
 
  @Test
  public void testMap() throws Exception {
    Schema schema = new Parser().parse(new File("src/test/resources/test-avro-schemas/intero1.avsc"));
    GenericData.Record document0 = new GenericData.Record(schema);
    Map map = new LinkedHashMap();
    Schema mapRecordSchema = schema.getField("mapField").schema().getValueType();
    GenericData.Record mapRecord = new GenericData.Record(mapRecordSchema);
    mapRecord.put("label", "nadja");
    map.put(utf8("foo"), mapRecord);
    document0.put("mapField", map);

    morphline = createMorphline("test-morphlines/extractAvroPaths");       
    deleteAllDocuments();
    Record record = new Record();
    record.put(Fields.ATTACHMENT_BODY, document0);
    startSession();
//    System.out.println(schema.toString(true));
//    System.out.println(document0.toString());
    assertTrue(morphline.process(record));
    assertEquals(1, collector.getRecords().size());
    assertEquals(Arrays.asList("nadja"), collector.getFirstRecord().get("/mapField/foo/label"));
    assertEquals(Arrays.asList(), collector.getFirstRecord().get("/unknownField"));

    morphline = createMorphline("test-morphlines/extractAvroPathsFlattened");       
    deleteAllDocuments();
    record = new Record();
    record.put(Fields.ATTACHMENT_BODY, document0);
    startSession();
//      System.out.println(documentSchema.toString(true));
//      System.out.println(document0.toString());
    assertTrue(morphline.process(record));
    assertEquals(1, collector.getRecords().size());
    assertEquals(Arrays.asList("nadja"), collector.getFirstRecord().get("/mapField/foo/label"));
    assertEquals(Arrays.asList(), collector.getFirstRecord().get("/unknownField"));
   
    ingestAndVerifyAvro(schema, document0);
   
    Record event = new Record();
    event.getFields().put(Fields.ATTACHMENT_BODY, document0);
    morphline = createMorphline("test-morphlines/extractAvroTree");
    deleteAllDocuments();
    //System.out.println(document0);
    assertTrue(load(event));
    assertEquals(1, queryResultSetSize("*:*"));
    Record first = collector.getFirstRecord();
    assertEquals(Arrays.asList("nadja"), first.get("/mapField/foo/label"));
    AbstractParser.removeAttachments(first);
    assertEquals(1, first.getFields().asMap().size());

    {
      morphline = createMorphline("test-morphlines/toAvro");
      Record jdoc0 = new Record();    
      jdoc0.put("_dataset_descriptor_schema", schema);
      jdoc0.put("mapField", new HashMap(ImmutableMap.of(
          utf8("foo"), ImmutableMap.of("label", "nadja")
          ))
      );
      Record expect0 = jdoc0.copy();
      expect0.put(Fields.ATTACHMENT_BODY, document0);
      processAndVerifySuccess(jdoc0, expect0, false)
     
      // verify that multiple maps can't be converted to a non-array schema
      jdoc0 = new Record();    
      jdoc0.put("_dataset_descriptor_schema", schema);
      jdoc0.put("mapField", new HashMap(ImmutableMap.of(
          utf8("foo"), ImmutableMap.of("label", "nadja")
          ))
      );
      jdoc0.put("mapField", new HashMap(ImmutableMap.of(
          utf8("foo"), ImmutableMap.of("label", "nadja")
          ))
      );
      collector.reset();
      assertFalse(morphline.process(jdoc0));
     
      // verify that an exception is raised if a required field is missing
      jdoc0 = new Record();    
      jdoc0.put("_dataset_descriptor_schema", schema);
      jdoc0.put("mapField", new HashMap(ImmutableMap.of(
          utf8("foo"), ImmutableMap.of()
          ))
      );
      collector.reset();
      assertFalse(morphline.process(jdoc0));
     
      // verify that default field is used if value is missing
      Schema schema2 = new Parser().parse(new File("src/test/resources/test-avro-schemas/intero2.avsc"));
      jdoc0 = new Record();    
      jdoc0.put("_dataset_descriptor_schema", schema2);
      jdoc0.put("mapField", new HashMap(ImmutableMap.of(
          utf8("foo"), ImmutableMap.of()
          ))
View Full Code Here

        throw new MorphlineCompilationException(
          "Must define only one of schemaFile or schemaString or schemaField at the same time", config);
      }

      if (schemaString != null) {
        this.fixedSchema = new Parser().parse(schemaString);
      } else if (schemaFile != null) {
        try {
          this.fixedSchema = new Parser().parse(new File(schemaFile));
        } catch (IOException e) {
          throw new MorphlineCompilationException(
            "Cannot parse external Avro schema file: " + schemaFile, config, e);
        }
      } else {
View Full Code Here

    public ReadAvro(CommandBuilder builder, Config config, Command parent, Command child, MorphlineContext context) {
      super(builder, config, parent, child, context);
     
      String schemaString = getConfigs().getString(config, "writerSchemaString", null);
      if (schemaString != null) {
        this.writerSchema = new Parser().parse(schemaString);
      } else {       
        String schemaFile = getConfigs().getString(config, "writerSchemaFile", null);
        if (schemaFile != null) {
          try {
            this.writerSchema = new Parser().parse(new File(schemaFile));
          } catch (IOException e) {
            throw new MorphlineCompilationException("Cannot parse external Avro writer schema file: " + schemaFile, config, e);
          }
        } else {
          this.writerSchema = null;
View Full Code Here

    public ReadAvroContainer(CommandBuilder builder, Config config, Command parent, Command child, MorphlineContext context) {  
      super(builder, config, parent, child, context);

      String schemaString = getConfigs().getString(config, "readerSchemaString", null);
      if (schemaString != null) {
        this.readerSchema = new Parser().parse(schemaString);
      } else {       
        String schemaFile = getConfigs().getString(config, "readerSchemaFile", null);
        if (schemaFile != null) {
          try {
            this.readerSchema = new Parser().parse(new File(schemaFile));
          } catch (IOException e) {
            throw new MorphlineCompilationException("Cannot parse external Avro reader schema file: " + schemaFile, config, e);
          }
        } else {
          this.readerSchema = null;
View Full Code Here

        throw new MorphlineCompilationException(
          "Must define only one of schemaFile or schemaString or schemaField at the same time", config);
      }

      if (schemaString != null) {
        this.fixedSchema = new Parser().parse(schemaString);
      } else if (schemaFile != null) {
        try {
          this.fixedSchema = new Parser().parse(new File(schemaFile));
        } catch (IOException e) {
          throw new MorphlineCompilationException(
            "Cannot parse external Avro schema file: " + schemaFile, config, e);
        }
      } else {
View Full Code Here

TOP

Related Classes of org.apache.avro.Schema.Parser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.