Package org.apache.avro.generic

Examples of org.apache.avro.generic.GenericRecordBuilder


    this.in = new MemoryChannel();
    Configurables.configure(in, config);

    config.put(DatasetSinkConstants.CONFIG_KITE_DATASET_URI, FILE_DATASET_URI);

    GenericRecordBuilder builder = new GenericRecordBuilder(RECORD_SCHEMA);
    expected = Lists.<GenericRecord>newArrayList(
        builder.set("id", "1").set("msg", "msg1").build(),
        builder.set("id", "2").set("msg", "msg2").build(),
        builder.set("id", "3").set("msg", "msg3").build());

    putToChannel(in, Iterables.transform(expected,
        new Function<GenericRecord, Event>() {
          private int i = 0;
View Full Code Here


  }

  @Test
  public void testDatasetUpdate() throws EventDeliveryException {
    // add an updated record that is missing the msg field
    GenericRecordBuilder updatedBuilder = new GenericRecordBuilder(
      UPDATED_SCHEMA);
    GenericData.Record updatedRecord = updatedBuilder
      .set("id", "0")
      .set("priority", 1)
      .set("msg", "Priority 1 message!")
      .build();

    // make a set of the expected records with the new schema
    Set<GenericRecord> expectedAsUpdated = Sets.newHashSet();
    for (GenericRecord record : expected) {
      expectedAsUpdated.add(updatedBuilder
        .clear("priority")
        .set("id", record.get("id"))
        .set("msg", record.get("msg"))
        .build());
    }
View Full Code Here

  @Test
  public void testCompatibleSchemas() throws EventDeliveryException {
    DatasetSink sink = sink(in, config);

    // add a compatible record that is missing the msg field
    GenericRecordBuilder compatBuilder = new GenericRecordBuilder(
        COMPATIBLE_SCHEMA);
    GenericData.Record compatibleRecord = compatBuilder.set("id", "0").build();

    // add the record to the incoming channel
    putToChannel(in, event(compatibleRecord, COMPATIBLE_SCHEMA, null, false));

    // the record will be read using the real schema, so create the expected
    // record using it, but without any data

    GenericRecordBuilder builder = new GenericRecordBuilder(RECORD_SCHEMA);
    GenericData.Record expectedRecord = builder.set("id", "0").build();
    expected.add(expectedRecord);

    // run the sink
    sink.start();
    sink.process();
View Full Code Here

  @Test
  public void testIncompatibleSchemas() throws EventDeliveryException {
    final DatasetSink sink = sink(in, config);

    GenericRecordBuilder builder = new GenericRecordBuilder(
        INCOMPATIBLE_SCHEMA);
    GenericData.Record rec = builder.set("username", "koala").build();
    putToChannel(in, event(rec, INCOMPATIBLE_SCHEMA, null, false));

    // run the sink
    sink.start();
    assertThrows("Should fail", EventDeliveryException.class,
View Full Code Here

                .name("name").type().stringType().noDefault()
                .name("ID").type().intType().noDefault()
                .endRecord();


        user = new GenericRecordBuilder(schema)
                .set("name", "Jeff")
                .set("ID", 1)
                .build();

View Full Code Here

        .record("MyRecord").namespace("ns")
        .fields()
          .name("field1").type().stringType().noDefault()
          .name("field2").type().stringType().noDefault()
        .endRecord();
    GenericData.Record record = new GenericRecordBuilder(schemaWithoutField).set("field1", "someValue").build();
    byte[] data = writeRecord(schemaWithoutField, record);
    try {
      readRecord(schemaWithField, data);
      Assert.fail("Expected exception not thrown");
    } catch (AvroTypeException typeException) {
View Full Code Here

    Schema rec2 = SchemaBuilder.record("inner2").fields()
      .name("f2").type().intType().noDefault()
      .endRecord();
   
    GenericData.Record recdef =
        new GenericRecordBuilder(rec).set("f", 1).build();
       
    GenericData.Record recdef2 =
        new GenericRecordBuilder(rec2).set("f2", 2).build();
   
    Schema r = SchemaBuilder.record("r").fields()
      .name("boolF").type().booleanType().booleanDefault(false)
      .name("intF").type().intType().intDefault(1)
      .name("longF").type().longType().longDefault(2L)
      .name("floatF").type().floatType().floatDefault(3.0f)
      .name("doubleF").type().doubleType().doubleDefault(4.0d)
      .name("stringF").type().stringType().stringDefault("def")
      .name("bytesF1").type().bytesType().bytesDefault(bytedef)
      .name("bytesF2").type().bytesType().bytesDefault(bufdef)
      .name("bytesF3").type().bytesType().bytesDefault(strdef)
      .name("nullF").type().nullType().nullDefault()
      .name("fixedF1").type().fixed("F1").size(1).fixedDefault(bytedef)
      .name("fixedF2").type().fixed("F2").size(1).fixedDefault(bufdef)
      .name("fixedF3").type().fixed("F3").size(1).fixedDefault(strdef)
      .name("enumF").type().enumeration("E1").symbols("S").enumDefault("S")
      .name("mapF").type().map().values().stringType()
        .mapDefault(mapdef)
      .name("arrayF").type().array().items().stringType()
        .arrayDefault(arrdef)
      .name("recordF").type().record("inner").fields()
        .name("f").type().intType().noDefault()
        .endRecord().recordDefault(recdef)
      .name("byName").type("E1").withDefault("S")
      // union builders, one for each 'first type' in a union:
      .name("boolU").type().unionOf().booleanType().and()
        .intType().endUnion().booleanDefault(false)
      .name("intU").type().unionOf().intType().and()
        .longType().endUnion().intDefault(1)
      .name("longU").type().unionOf().longType().and()
        .intType().endUnion().longDefault(2L)
      .name("floatU").type().unionOf().floatType().and()
        .intType().endUnion().floatDefault(3.0f)
      .name("doubleU").type().unionOf().doubleType().and()
        .intType().endUnion().doubleDefault(4.0d)
      .name("stringU").type().unionOf().stringType().and()
        .intType().endUnion().stringDefault("def")
      .name("bytesU").type().unionOf().bytesType().and()
        .intType().endUnion().bytesDefault(bytedef)
      .name("nullU").type().unionOf().nullType().and()
        .intType().endUnion().nullDefault()
      .name("fixedU").type().unionOf().fixed("F4").size(1).and()
        .intType().endUnion().fixedDefault(bytedef)
      .name("enumU").type().unionOf().enumeration("E2").symbols("SS").and()
        .intType().endUnion().enumDefault("SS")
      .name("mapU").type().unionOf().map().values().stringType().and()
        .intType().endUnion().mapDefault(mapdef)
      .name("arrayU").type().unionOf().array().items().stringType().and()
        .intType().endUnion().arrayDefault(arrdef)
      .name("recordU").type().unionOf().record("inner2").fields()
        .name("f2").type().intType().noDefault()
        .endRecord().and().intType().endUnion().recordDefault(recdef2)
      .endRecord();
   
    GenericData.Record newRec =
        new GenericRecordBuilder(r).build();
   
    Assert.assertEquals(false, newRec.get("boolF"));
    Assert.assertEquals(false, newRec.get("boolU"));
    Assert.assertEquals(1, newRec.get("intF"));
    Assert.assertEquals(1, newRec.get("intU"));
View Full Code Here

      .name("requiredInt").type().intType().noDefault()
      .name("optionalInt").type().optional().intType()
      .name("nullableIntWithDefault").type().nullable().intType().intDefault(3)
      .endRecord();

    GenericData.Record rec1 = new GenericRecordBuilder(writeSchema)
        .set("requiredInt", 1)
        .build();

    Assert.assertEquals(1, rec1.get("requiredInt"));
    Assert.assertEquals(null, rec1.get("optionalInt"));
    Assert.assertEquals(3, rec1.get("nullableIntWithDefault"));

    GenericData.Record rec2 = new GenericRecordBuilder(writeSchema)
        .set("requiredInt", 1)
        .set("optionalInt", 2)
        .set("nullableIntWithDefault", 13)
        .build();
View Full Code Here

          throw new StoreException("Invalid format " + datasetDefinition.getFormat() +
              " specified, you must use 'parquet' with " + this.getClass().getSimpleName() + ".");
        }
      }
    }
    GenericRecordBuilder builder = new GenericRecordBuilder(schema);
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(entity);
    for (Schema.Field f : schema.getFields()) {
      if (beanWrapper.isReadableProperty(f.name())) {
        Schema fieldSchema = f.schema();
        if (f.schema().getType().equals(Schema.Type.UNION)) {
          for (Schema s : f.schema().getTypes()) {
            if (!s.getName().equals("null")) {
              fieldSchema = s;
            }
          }
        }
        if (fieldSchema.getType().equals(Schema.Type.RECORD)) {
          throw new StoreException("Nested record currently not supported for field: " + f.name() +
              " of type: " + beanWrapper.getPropertyDescriptor(f.name()).getPropertyType().getName());
        } else {
          if (fieldSchema.getType().equals(Schema.Type.BYTES)) {
            ByteBuffer buffer = null;
            Object value = beanWrapper.getPropertyValue(f.name());
            if (value == null || value instanceof byte[]) {
              if(value != null) {
                byte[] bytes = (byte[]) value;
                buffer = ByteBuffer.wrap(bytes);
              }
              builder.set(f.name(), buffer);
            } else {
              throw new StoreException("Don't know how to handle " + value.getClass() + " for " + fieldSchema);
            }
          } else {
            builder.set(f.name(), beanWrapper.getPropertyValue(f.name()));
            }
        }
      }
    }
    try {
      writer.write(builder.build());
    } catch (ClassCastException cce) {
      throw new StoreException("Failed to write record with schema: " +
          schema, cce);
    }
  }
View Full Code Here

    config.put(DatasetSinkConstants.CONFIG_KITE_DATASET_NAME, DATASET_NAME);

    this.in = new MemoryChannel();
    Configurables.configure(in, config);

    GenericRecordBuilder builder = new GenericRecordBuilder(RECORD_SCHEMA);
    expected = Lists.newArrayList(
        builder.set("id", "1").set("msg", "msg1").build(),
        builder.set("id", "2").set("msg", "msg2").build(),
        builder.set("id", "3").set("msg", "msg3").build());

    putToChannel(in, Iterables.transform(expected,
        new Function<GenericData.Record, Event>() {
          private int i = 0;
View Full Code Here

TOP

Related Classes of org.apache.avro.generic.GenericRecordBuilder

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.