Java Code Examples for org.apache.avro.generic.GenericData#Fixed

The following examples show how to use org.apache.avro.generic.GenericData#Fixed . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamlineEventSerializer.java    From streamline with Apache License 2.0 6 votes vote down vote up
private static Object getAvroValue(Object input, Schema schema) {
    if (input instanceof byte[] && Schema.Type.FIXED.equals(schema.getType())) {
        return new GenericData.Fixed(schema, (byte[]) input);
    } else if (input instanceof Map && !((Map) input).isEmpty()) {
        GenericRecord result;
        result = new GenericData.Record(schema);
        for (Map.Entry<String, Object> entry: ((Map<String, Object>) input).entrySet()) {
            result.put(entry.getKey(), getAvroValue(entry.getValue(), schema.getField(entry.getKey()).schema()));
        }
        return result;
    } else if (input instanceof Collection && !((Collection) input).isEmpty()) {
        // for array even though we(Schema in streamline registry) support different types of elements in an array, avro expects an array
        // schema to have elements of same type. Hence, for now we will restrict array to have elements of same type. Other option is convert
        // a  streamline Schema Array field to Record in avro. However, with that the issue is that avro Field constructor does not allow a
        // null name. We could potentiall hack it by plugging in a dummy name like arrayfield, but seems hacky so not taking that path
        List<Object> values = new ArrayList<>(((Collection) input).size());
        for (Object value: (Collection) input) {
            values.add(getAvroValue(value, schema.getElementType()));
        }
        return new GenericData.Array<Object>(schema, values);
    } else {
        return input;
    }
}
 
Example 2
Source File: FastSerdeBenchmarkSupport.java    From avro-fastserde with Apache License 2.0 5 votes vote down vote up
public static GenericData.Fixed generateRandomFixed(Schema schema) {
    if (!Schema.Type.FIXED.equals(schema.getType())) {
        throw new IllegalArgumentException("input schema must be an fixed schema");
    }

    return new GenericData.Fixed(schema,
            RandomUtils.nextBytes(schema.getFixedSize()));
}
 
Example 3
Source File: FastSpecificDeserializerGeneratorTest.java    From avro-fastserde with Apache License 2.0 5 votes vote down vote up
@Test(expected = FastDeserializerGeneratorException.class)
public void shouldNotReadStrippedEnum() throws IOException {
    // given
    Schema.Parser parser = new Schema.Parser();
    Schema oldRecordSchema = parser.parse(this.getClass().getResourceAsStream(
            "/schema/fastserdetestoldextendedenum.avsc"));
    GenericData.Fixed testFixed = new GenericData.Fixed(oldRecordSchema.getField("testFixed").schema(),
            new byte[] { 0x01 });
    GenericData.Record subRecord = new GenericData.Record(oldRecordSchema.getField("subRecordUnion").schema()
            .getTypes().get(1));
    GenericData.Record oldRecord = new GenericData.Record(oldRecordSchema);
    oldRecord.put("testInt", 1);
    oldRecord.put("testLong", 1l);
    oldRecord.put("testDouble", 1.0);
    oldRecord.put("testFloat", 1.0f);
    oldRecord.put("testBoolean", true);
    oldRecord.put("testBytes", ByteBuffer.wrap(new byte[] { 0x01, 0x02 }));
    oldRecord.put("testString", "aaa");
    oldRecord.put("testFixed", testFixed);
    oldRecord.put("testFixedUnion", testFixed);
    oldRecord.put("testFixedArray", Arrays.asList(testFixed));
    oldRecord.put("testFixedUnionArray", Arrays.asList(testFixed));
    oldRecord.put("testEnum", new GenericData.EnumSymbol(oldRecordSchema.getField("testEnum").schema(), "F"));
    oldRecord.put("testEnumArray",
            Arrays.asList(new GenericData.EnumSymbol(oldRecordSchema.getField("testEnum").schema(), "F")));
    oldRecord.put("testEnumUnionArray", Collections.emptyList());

    oldRecord.put("subRecordUnion", subRecord);
    oldRecord.put("subRecord", subRecord);

    oldRecord.put("recordsArray", Collections.emptyList());
    oldRecord.put("recordsArrayMap", Collections.emptyList());
    oldRecord.put("recordsMap", Collections.emptyMap());
    oldRecord.put("recordsMapArray", Collections.emptyMap());

    // when
    TestRecord record = deserializeSpecificFast(TestRecord.getClassSchema(), oldRecordSchema,
            serializeGeneric(oldRecord));
}
 
Example 4
Source File: GeneratorFunctions.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public GenericFixed apply(GeneratorContext input) {
    if (schema == null) {
        schema = new Schema.Parser().parse(jsonSchema);
    }
    byte[] buffer = new byte[size];
    input.getRandom().nextBytes(buffer);
    return new GenericData.Fixed(schema, buffer);
}
 
Example 5
Source File: RowDataReader.java    From iceberg with Apache License 2.0 5 votes vote down vote up
private static Object convertConstant(Type type, Object value) {
  if (value == null) {
    return null;
  }

  switch (type.typeId()) {
    case DECIMAL:
      return Decimal.apply((BigDecimal) value);
    case STRING:
      if (value instanceof Utf8) {
        Utf8 utf8 = (Utf8) value;
        return UTF8String.fromBytes(utf8.getBytes(), 0, utf8.getByteLength());
      }
      return UTF8String.fromString(value.toString());
    case FIXED:
      if (value instanceof byte[]) {
        return value;
      } else if (value instanceof GenericData.Fixed) {
        return ((GenericData.Fixed) value).bytes();
      }
      return ByteBuffers.toByteArray((ByteBuffer) value);
    case BINARY:
      return ByteBuffers.toByteArray((ByteBuffer) value);
    default:
  }
  return value;
}
 
Example 6
Source File: IdentityPartitionConverters.java    From iceberg with Apache License 2.0 5 votes vote down vote up
/**
 * Conversions from internal representations to Iceberg generic values.
 */
public static Object convertConstant(Type type, Object value) {
  if (value == null) {
    return null;
  }

  switch (type.typeId()) {
    case STRING:
      return value.toString();
    case TIME:
      return DateTimeUtil.timeFromMicros((Long) value);
    case DATE:
      return DateTimeUtil.dateFromDays((Integer) value);
    case TIMESTAMP:
      if (((Types.TimestampType) type).shouldAdjustToUTC()) {
        return DateTimeUtil.timestamptzFromMicros((Long) value);
      } else {
        return DateTimeUtil.timestampFromMicros((Long) value);
      }
    case FIXED:
      if (value instanceof GenericData.Fixed) {
        return ((GenericData.Fixed) value).bytes();
      }
      return value;
    default:
  }
  return value;
}
 
Example 7
Source File: UUIDConversion.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Override
public GenericFixed toFixed(UUID value, Schema schema, LogicalType type) {
  ByteBuffer buffer = ByteBuffer.allocate(16);
  buffer.order(ByteOrder.BIG_ENDIAN);
  buffer.putLong(value.getMostSignificantBits());
  buffer.putLong(value.getLeastSignificantBits());
  return new GenericData.Fixed(schema, buffer.array());
}
 
Example 8
Source File: AvroRelConverter.java    From samza with Apache License 2.0 5 votes vote down vote up
public static Object convertToAvroObject(Object relObj, Schema schema) {
  if (relObj == null) {
    return null;
  }
  switch (schema.getType()) {
    case RECORD:
      return convertToGenericRecord((SamzaSqlRelRecord) relObj, getNonNullUnionSchema(schema));
    case ARRAY:
      List<Object> avroList = ((List<Object>) relObj).stream()
          .map(o -> convertToAvroObject(o, getNonNullUnionSchema(schema).getElementType()))
          .collect(Collectors.toList());
      return avroList;
    case MAP:
      return ((Map<String, ?>) relObj).entrySet()
          .stream()
          .collect(Collectors.toMap(Map.Entry::getKey,
            e -> convertToAvroObject(e.getValue(), getNonNullUnionSchema(schema).getValueType())));
    case UNION:
      for (Schema unionSchema : schema.getTypes()) {
        if (isSchemaCompatibleWithRelObj(relObj, unionSchema)) {
          return convertToAvroObject(relObj, unionSchema);
        }
      }
      return null;
    case ENUM:
      return new GenericData.EnumSymbol(schema, (String) relObj);
    case FIXED:
      return new GenericData.Fixed(schema, ((ByteString) relObj).getBytes());
    case BYTES:
      return ByteBuffer.wrap(((ByteString) relObj).getBytes());
    default:
      return relObj;
  }
}
 
Example 9
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static GenericData.Fixed newFixedField(Schema ofType) {
  return FACTORY.newFixedField(ofType);
}
 
Example 10
Source File: Avro18Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public GenericData.Fixed newFixedField(Schema fixedSchema, byte[] contents) {
  return new GenericData.Fixed(fixedSchema, contents);
}
 
Example 11
Source File: TestReadWrite.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
@Test
public void testAll() throws Exception {
  Schema schema = new Schema.Parser().parse(
      Resources.getResource("all.avsc").openStream());

  Path file = new Path(createTempFile().getPath());
  List<Integer> integerArray = Arrays.asList(1, 2, 3);
  GenericData.Record nestedRecord = new GenericRecordBuilder(
    schema.getField("mynestedrecord").schema())
    .set("mynestedint", 1).build();
  List<Integer> emptyArray = new ArrayList<Integer>();
  Schema arrayOfOptionalIntegers = Schema.createArray(
    optional(Schema.create(Schema.Type.INT)));
  GenericData.Array<Integer> genericIntegerArrayWithNulls =
    new GenericData.Array<Integer>(
      arrayOfOptionalIntegers,
      Arrays.asList(1, null, 2, null, 3));
  GenericFixed genericFixed = new GenericData.Fixed(
    Schema.createFixed("fixed", null, null, 1), new byte[]{(byte) 65});
  ImmutableMap<String, Integer> emptyMap = new ImmutableMap.Builder<String, Integer>().build();

  try(ParquetWriter<GenericRecord> writer = AvroParquetWriter
      .<GenericRecord>builder(file)
      .withSchema(schema)
      .withConf(testConf)
      .build()) {

    GenericData.Array<Integer> genericIntegerArray = new GenericData.Array<Integer>(
      Schema.createArray(Schema.create(Schema.Type.INT)), integerArray);

    GenericData.Record record = new GenericRecordBuilder(schema)
      .set("mynull", null)
      .set("myboolean", true)
      .set("myint", 1)
      .set("mylong", 2L)
      .set("myfloat", 3.1f)
      .set("mydouble", 4.1)
      .set("mybytes", ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)))
      .set("mystring", "hello")
      .set("mynestedrecord", nestedRecord)
      .set("myenum", "a")
      .set("myarray", genericIntegerArray)
      .set("myemptyarray", emptyArray)
      .set("myoptionalarray", genericIntegerArray)
      .set("myarrayofoptional", genericIntegerArrayWithNulls)
      .set("mymap", ImmutableMap.of("a", 1, "b", 2))
      .set("myemptymap", emptyMap)
      .set("myfixed", genericFixed)
      .build();

    writer.write(record);
  }

  final GenericRecord nextRecord;
  try(AvroParquetReader<GenericRecord> reader = new AvroParquetReader<GenericRecord>(testConf, file)) {
    nextRecord = reader.read();
  }

  Object expectedEnumSymbol = compat ? "a" :
      new GenericData.EnumSymbol(schema.getField("myenum").schema(), "a");

  assertNotNull(nextRecord);
  assertEquals(null, nextRecord.get("mynull"));
  assertEquals(true, nextRecord.get("myboolean"));
  assertEquals(1, nextRecord.get("myint"));
  assertEquals(2L, nextRecord.get("mylong"));
  assertEquals(3.1f, nextRecord.get("myfloat"));
  assertEquals(4.1, nextRecord.get("mydouble"));
  assertEquals(ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), nextRecord.get("mybytes"));
  assertEquals(str("hello"), nextRecord.get("mystring"));
  assertEquals(expectedEnumSymbol, nextRecord.get("myenum"));
  assertEquals(nestedRecord, nextRecord.get("mynestedrecord"));
  assertEquals(integerArray, nextRecord.get("myarray"));
  assertEquals(emptyArray, nextRecord.get("myemptyarray"));
  assertEquals(integerArray, nextRecord.get("myoptionalarray"));
  assertEquals(genericIntegerArrayWithNulls, nextRecord.get("myarrayofoptional"));
  assertEquals(ImmutableMap.of(str("a"), 1, str("b"), 2), nextRecord.get("mymap"));
  assertEquals(emptyMap, nextRecord.get("myemptymap"));
  assertEquals(genericFixed, nextRecord.get("myfixed"));
}
 
Example 12
Source File: ValueReaders.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public static ValueReader<GenericData.Fixed> fixed(Schema schema) {
  return new GenericFixedReader(schema);
}
 
Example 13
Source File: FastDeserializerDefaultsTest.java    From avro-fastserde with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldAddFieldsInMiddleOfSchema() throws IOException {
    // given
    Schema.Parser parser = new Schema.Parser();
    Schema oldRecordSchema = TestRecord.getClassSchema();

    GenericData.Record subRecord = new GenericData.Record(oldRecordSchema.getField("subRecordUnion").schema()
            .getTypes().get(1));
    GenericData.EnumSymbol testEnum = new GenericData.EnumSymbol(
            oldRecordSchema.getField("testEnum").schema(), "A");
    GenericData.Fixed testFixed = new GenericData.Fixed(oldRecordSchema.getField("testFixed").schema(),
            new byte[]{0x01});
    GenericData.Record oldRecord = new GenericData.Record(oldRecordSchema);

    oldRecord.put("testInt", 1);
    oldRecord.put("testLong", 1L);
    oldRecord.put("testDouble", 1.0);
    oldRecord.put("testFloat", 1.0f);
    oldRecord.put("testBoolean", true);
    oldRecord.put("testBytes", ByteBuffer.wrap(new byte[]{0x01, 0x02}));
    oldRecord.put("testString", "aaa");
    oldRecord.put("testFixed", testFixed);
    oldRecord.put("testEnum", testEnum);

    subRecord.put("subField", "abc");
    subRecord.put("anotherField", "ghi");

    oldRecord.put("subRecordUnion", subRecord);
    oldRecord.put("subRecord", subRecord);
    oldRecord.put("recordsArray", Collections.singletonList(subRecord));
    Map<Utf8, GenericData.Record> recordsMap = new HashMap<>();
    recordsMap.put(new Utf8("1"), subRecord);
    oldRecord.put("recordsMap", recordsMap);

    oldRecord.put("testFixedArray", Collections.emptyList());
    oldRecord.put("testFixedUnionArray", Collections.emptyList());
    oldRecord.put("testEnumArray", Collections.emptyList());
    oldRecord.put("testEnumUnionArray", Collections.emptyList());
    oldRecord.put("recordsArrayMap", Collections.emptyList());
    oldRecord.put("recordsMapArray", Collections.emptyMap());

    Schema newRecordSchema = parser
            .parse(this.getClass().getResourceAsStream("/schema/defaultsTestSubrecord.avsc"));
    // when
    GenericRecord record = deserializeGenericFast(newRecordSchema, oldRecordSchema, serializeGeneric(oldRecord));

    // then
    GenericData.Record newSubRecord = new GenericData.Record(newRecordSchema.getField("subRecordUnion")
            .schema().getTypes().get(1));
    newSubRecord.put("subField", "abc");
    newSubRecord.put("anotherField", "ghi");
    newSubRecord.put("newSubField", "newSubFieldValue");
    recordsMap.put(new Utf8("1"), newSubRecord);

    Assert.assertEquals("newSubFieldValue",
            ((GenericRecord) record.get("subRecordUnion")).get("newSubField").toString());
    Assert.assertEquals("newFieldValue", record.get("newField").toString());
    Assert.assertEquals(1, record.get("testInt"));
    Assert.assertEquals(1L, record.get("testLong"));
    Assert.assertEquals(1.0, record.get("testDouble"));
    Assert.assertEquals(1.0f, record.get("testFloat"));
    Assert.assertEquals(true, record.get("testBoolean"));
    Assert.assertEquals(ByteBuffer.wrap(new byte[]{0x01, 0x02}), record.get("testBytes"));
    Assert.assertEquals("aaa", record.get("testString").toString());
    Assert.assertEquals(testFixed, record.get("testFixed"));
    Assert.assertEquals(testEnum, record.get("testEnum"));
    Assert.assertEquals(newSubRecord, record.get("subRecordUnion"));

    Assert.assertEquals(Collections.singletonList(newSubRecord), record.get("recordsArray"));
    Assert.assertEquals(recordsMap, record.get("recordsMap"));
    Assert.assertEquals(Collections.emptyList(), record.get("testFixedArray"));
    Assert.assertEquals(Collections.emptyList(), record.get("testFixedUnionArray"));
    Assert.assertEquals(Collections.emptyList(), record.get("testEnumArray"));
    Assert.assertEquals(Collections.emptyList(), record.get("testEnumUnionArray"));
    Assert.assertEquals(Collections.emptyList(), record.get("recordsArrayMap"));
    Assert.assertEquals(Collections.emptyMap(), record.get("recordsMapArray"));

}
 
Example 14
Source File: FastDeserializerDefaultsTest.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test(groups = {"deserializationTest"}, dataProvider = "SlowFastDeserializer")
public void shouldAddFieldsInMiddleOfSchema(Boolean whetherUseFastDeserializer) throws IOException {
  // given
  Schema oldRecordSchema = TestRecord.SCHEMA$;

  GenericData.Record subRecord =
      new GenericData.Record(oldRecordSchema.getField("subRecordUnion").schema().getTypes().get(1));
  Schema enumSchema = Schema.createEnum("TestEnum", "", "", Arrays.asList("A", "B", "C", "D", "E"));

  GenericData.EnumSymbol testEnum = AvroCompatibilityHelper.newEnumSymbol(enumSchema, "A");
  GenericData.Fixed testFixed = new GenericData.Fixed(oldRecordSchema.getField("testFixed").schema());
  testFixed.bytes(new byte[]{0x01});
  GenericData.Record oldRecord = new GenericData.Record(oldRecordSchema);

  oldRecord.put("testInt", 1);
  oldRecord.put("testLong", 1L);
  oldRecord.put("testDouble", 1.0);
  oldRecord.put("testFloat", 1.0f);
  oldRecord.put("testBoolean", true);
  oldRecord.put("testBytes", ByteBuffer.wrap(new byte[]{0x01, 0x02}));
  oldRecord.put("testString", "aaa");
  oldRecord.put("testFixed", testFixed);
  oldRecord.put("testEnum", testEnum);

  subRecord.put("subField", "abc");
  subRecord.put("anotherField", "ghi");

  oldRecord.put("subRecordUnion", subRecord);
  oldRecord.put("subRecord", subRecord);
  oldRecord.put("recordsArray", Collections.singletonList(subRecord));
  Map<String, GenericData.Record> recordsMap = new HashMap<>();
  recordsMap.put("1", subRecord);
  oldRecord.put("recordsMap", recordsMap);

  oldRecord.put("testFixedArray", Collections.emptyList());
  oldRecord.put("testFixedUnionArray", Collections.emptyList());
  oldRecord.put("testEnumArray", Collections.emptyList());
  oldRecord.put("testEnumUnionArray", Collections.emptyList());
  oldRecord.put("recordsArrayMap", Collections.emptyList());
  oldRecord.put("recordsMapArray", Collections.emptyMap());

  Schema newRecordSchema = Schema.parse(this.getClass().getResourceAsStream("/schema/defaultsTestSubrecord.avsc"));
  // when
  GenericRecord record = null;
  if (whetherUseFastDeserializer || Utils.isAvro14()) {
    record = decodeGenericFast(newRecordSchema, oldRecordSchema, genericDataAsDecoder(oldRecord));
  } else {
    // There is a bug in Schema.applyAliases of avro-1.4, and the following invocation will trigger it.
    record = decodeGenericSlow(newRecordSchema, oldRecordSchema, genericDataAsDecoder(oldRecord));
  }

  // then
  GenericData.Record newSubRecord =
      new GenericData.Record(newRecordSchema.getField("subRecordUnion").schema().getTypes().get(1));
  newSubRecord.put("subField", new Utf8("abc"));
  newSubRecord.put("anotherField", new Utf8("ghi"));
  newSubRecord.put("newSubField", new Utf8("newSubFieldValue"));
  Map<Utf8, GenericData.Record> expectedRecordsMap = new HashMap<>();
  expectedRecordsMap.put(new Utf8("1"), newSubRecord);

  Assert.assertEquals("newSubFieldValue",
      ((GenericRecord) record.get("subRecordUnion")).get("newSubField").toString());
  Assert.assertEquals("newFieldValue", record.get("newField").toString());
  Assert.assertEquals(1, record.get("testInt"));
  Assert.assertEquals(1L, record.get("testLong"));
  Assert.assertEquals(1.0, record.get("testDouble"));
  Assert.assertEquals(1.0f, record.get("testFloat"));
  Assert.assertEquals(true, record.get("testBoolean"));
  Assert.assertEquals(ByteBuffer.wrap(new byte[]{0x01, 0x02}), record.get("testBytes"));
  Assert.assertEquals(new Utf8("aaa"), record.get("testString"));
  Assert.assertEquals(testFixed, record.get("testFixed"));
  Assert.assertEquals(testEnum, record.get("testEnum"));
  Assert.assertEquals(newSubRecord, record.get("subRecordUnion"));

  Assert.assertTrue(Arrays.asList(newSubRecord).equals(record.get("recordsArray")));

  Assert.assertEquals(expectedRecordsMap, record.get("recordsMap"));
  Assert.assertTrue(Collections.emptyList().equals(record.get("testFixedArray")));
  Assert.assertTrue(Collections.emptyList().equals(record.get("testFixedUnionArray")));
  Assert.assertTrue(Collections.emptyList().equals(record.get("testEnumArray")));
  Assert.assertTrue(Collections.emptyList().equals(record.get("testEnumUnionArray")));
  Assert.assertTrue(Collections.emptyList().equals(record.get("recordsArrayMap")));
  Assert.assertEquals(Collections.emptyMap(), record.get("recordsMapArray"));
}
 
Example 15
Source File: Avro17Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public GenericData.Fixed newFixedField(Schema fixedSchema) {
  return new GenericData.Fixed(fixedSchema);
}
 
Example 16
Source File: Generator.java    From avro-random-generator with Do What The F*ck You Want To Public License 4 votes vote down vote up
private GenericFixed generateFixed(Schema schema) {
  byte[] bytes = new byte[schema.getFixedSize()];
  random.nextBytes(bytes);
  return new GenericData.Fixed(schema, bytes);
}
 
Example 17
Source File: FastGenericDeserializerGeneratorTest.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public GenericData.Fixed newFixed(Schema fixedSchema, byte[] bytes) {
  GenericData.Fixed fixed = new GenericData.Fixed(fixedSchema);
  fixed.bytes(bytes);
  return fixed;
}
 
Example 18
Source File: FastDeserializerDefaultsTest.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test(groups = {"deserializationTest"}, dataProvider = "SlowFastDeserializer")
@SuppressWarnings("unchecked")
public void shouldReadGenericDefaults(Boolean whetherUseFastDeserializer) throws IOException {
  // given
  Schema oldRecordSchema = Schema.parse(this.getClass().getResourceAsStream("/schema/defaultsTestOld.avsc"));
  GenericData.Record oldRecord = new GenericData.Record(oldRecordSchema);
  GenericData.Record oldSubRecord = new GenericData.Record(oldRecordSchema.getField("oldSubRecord").schema());
  oldSubRecord.put("oldSubField", new Utf8("testValueOfSubField"));
  oldSubRecord.put("fieldToBeRemoved", 33);
  oldRecord.put("oldSubRecord", oldSubRecord);

  // when
  GenericRecord testRecord = null;
  if (whetherUseFastDeserializer) {
    testRecord = decodeGenericFast(DefaultsTestRecord.SCHEMA$, oldRecordSchema, genericDataAsDecoder(oldRecord));
  } else {
    testRecord = decodeGenericSlow(DefaultsTestRecord.SCHEMA$, oldRecordSchema, genericDataAsDecoder(oldRecord));
  }

  // then
  Assert.assertEquals(oldSubRecord.get("oldSubField"),
      ((GenericData.Record) testRecord.get("oldSubRecord")).get("oldSubField"));
  Assert.assertEquals(new Utf8("defaultOldSubField"),
      ((GenericData.Record) testRecord.get("newFieldWithOldSubRecord")).get("oldSubField"));
  Assert.assertEquals(42, (int) testRecord.get("testInt"));
  Assert.assertNull(testRecord.get("testIntUnion"));
  Assert.assertEquals(9223372036854775807L, (long) testRecord.get("testLong"));
  Assert.assertNull(testRecord.get("testLongUnion"));
  Assert.assertEquals(3.14d, (double) testRecord.get("testDouble"), 0);
  Assert.assertNull(testRecord.get("testDoubleUnion"));
  Assert.assertEquals(3.14f, (float) testRecord.get("testFloat"), 0);
  Assert.assertNull(testRecord.get("testFloatUnion"));
  Assert.assertEquals(true, testRecord.get("testBoolean"));
  Assert.assertNull(testRecord.get("testBooleanUnion"));
  Assert.assertEquals(ByteBuffer.wrap("1234".getBytes()), testRecord.get("testBytes"));
  Assert.assertNull(testRecord.get("testBytesUnion"));
  Assert.assertEquals(new Utf8("testStringValue"), testRecord.get("testString"));
  Assert.assertEquals(new Utf8("http://www.example.com"), testRecord.get("testStringable"));

  Assert.assertNull(testRecord.get("testStringUnion"));
  Schema fixedSchema = Schema.createFixed("DefaultsFixed", "", "", 1);
  GenericData.Fixed expectedFixed1 = AvroCompatibilityHelper.newFixedField(fixedSchema, new byte[]{(byte) '5'});
  Assert.assertEquals(expectedFixed1, testRecord.get("testFixed"));
  Assert.assertNull(testRecord.get("testFixedUnion"));
  GenericData.Fixed expectedFixed2 = AvroCompatibilityHelper.newFixedField(fixedSchema, new byte[]{(byte) '6'});
  Assert.assertTrue(Arrays.asList(expectedFixed2).equals(testRecord.get("testFixedArray")));

  List listWithNull = new LinkedList();
  listWithNull.add(null);
  Assert.assertTrue(listWithNull.equals(testRecord.get("testFixedUnionArray")));
  Assert.assertEquals("C", testRecord.get("testEnum").toString());
  Assert.assertNull(testRecord.get("testEnumUnion"));
  Schema enumSchema = Schema.createEnum("DefaultsNewEnum", "", "", Arrays.asList("A", "B"));
  Assert.assertTrue(Arrays.asList(Arrays.asList(AvroCompatibilityHelper.newEnumSymbol(enumSchema, "B")))
      .equals(testRecord.get("testNewEnumIntUnionArray")));

  Assert.assertEquals("E", ((List<GenericData.EnumSymbol>) testRecord.get("testEnumArray")).get(0).toString());
  Assert.assertEquals("B", ((List<GenericData.EnumSymbol>) testRecord.get("testEnumArray")).get(1).toString());
  Assert.assertTrue(listWithNull.equals(testRecord.get("testEnumUnionArray")));
  Assert.assertNull(testRecord.get("subRecordUnion"));
  Assert.assertEquals(newGenericSubRecord("valueOfSubField", null, "A"), testRecord.get("subRecord"));
  Assert.assertTrue(
      Arrays.asList(newGenericSubRecord("recordArrayValue", null, "A")).equals(testRecord.get("recordArray")));
  Assert.assertTrue(listWithNull.equals(testRecord.get("recordUnionArray")));

  Map stringableMap = new HashMap();
  stringableMap.put(new Utf8("http://www.example2.com"), new Utf8("123"));
  Assert.assertEquals(stringableMap, testRecord.get("stringableMap"));

  Map recordMap = new HashMap();
  recordMap.put(new Utf8("test"), newGenericSubRecord("recordMapValue", null, "A"));
  Assert.assertEquals(recordMap, testRecord.get("recordMap"));

  Map recordUnionMap = new HashMap();
  recordUnionMap.put(new Utf8("test"), null);
  Assert.assertEquals(recordUnionMap, testRecord.get("recordUnionMap"));
  Assert.assertTrue(
      new ArrayList(Collections.singletonList(recordUnionMap)).equals(testRecord.get("recordUnionMapArray")));

  Map recordUnionArrayMap = new HashMap();
  recordUnionArrayMap.put(new Utf8("test"), listWithNull);
  Assert.assertTrue(recordUnionArrayMap.equals(testRecord.get("recordUnionArrayMap")));
}
 
Example 19
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * creates a new {@link org.apache.avro.generic.GenericData.Fixed} of the given schema with a value of zeroes
 * @param fixedSchema fixed schema
 * @return a new {@link org.apache.avro.generic.GenericData.Fixed} with a value of all zeroes of the correct size
 */
public static GenericData.Fixed newFixedField(Schema fixedSchema) {
  assertAvroAvailable();
  return ADAPTER.newFixedField(fixedSchema);
}
 
Example 20
Source File: AvroAdapter.java    From avro-util with BSD 2-Clause "Simplified" License votes vote down vote up
GenericData.Fixed newFixedField(Schema ofType, byte[] contents);