/** * Create a builder for an Avro fixed type with the specified name and size. * This is equivalent to: * <pre> * builder().fixed(name); * </pre> * @param name the fixed name */ public static FixedBuilder<Schema> fixed(String name) { return builder().fixed(name); }
/** * Create a builder for an Avro map * This is equivalent to: * <pre> * builder().map(); * </pre> */ public static MapBuilder<Schema> map() { return builder().map(); }
/** * Create a builder for an Avro record with the specified name. * This is equivalent to: * <pre> * builder().record(name); * </pre> * @param name the record name */ public static RecordBuilder<Schema> record(String name) { return builder().record(name); }
@Test public void shouldSerializeBigint() { shouldSerializeTypeCorrectly( Schema.OPTIONAL_INT64_SCHEMA, 123L, org.apache.avro.SchemaBuilder.builder().longType()); }
@Test public void shouldSerializeDouble() { shouldSerializeTypeCorrectly( Schema.OPTIONAL_FLOAT64_SCHEMA, 1.23456789012345, org.apache.avro.SchemaBuilder.builder().doubleType()); }
@Test public void shouldDeserializeTimeMillisToBigint() { shouldDeserializeTypeCorrectly( LogicalTypes.timeMillis().addToSchema( org.apache.avro.SchemaBuilder.builder().intType()), ChronoUnit.MILLIS.between( LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT), LocalDateTime.now()), Schema.OPTIONAL_INT64_SCHEMA ); }
@Test public void shouldDeserializeTimeMicrosToBigint() { shouldDeserializeTypeCorrectly( LogicalTypes.timeMicros().addToSchema( org.apache.avro.SchemaBuilder.builder().longType()), ChronoUnit.MICROS.between( LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT), LocalDateTime.now()), Schema.OPTIONAL_INT64_SCHEMA ); }
@Test public void shouldSerializeInteger() { shouldSerializeTypeCorrectly( Schema.OPTIONAL_INT32_SCHEMA, 123, org.apache.avro.SchemaBuilder.builder().intType()); }
/** * This method will create a schema a union field consisting of null and the specified type. * * @param dataType The data type of the field */ protected static Schema getUnionFieldType(String dataType) { return SchemaBuilder.builder().unionOf().nullBuilder().endNull().and().type(getSchemaForType(dataType)).endUnion(); }
@Test public void shouldDeserializeTimestampToInteger() { shouldDeserializeTypeCorrectly( LogicalTypes.timestampMicros().addToSchema( org.apache.avro.SchemaBuilder.builder().longType()), ChronoUnit.MICROS.between( LocalDateTime.of(LocalDate.ofEpochDay(0), LocalTime.MIDNIGHT), LocalDateTime.now()), Schema.OPTIONAL_INT64_SCHEMA ); }
@Test public void shouldDeserializeTimestampToBigint() { shouldDeserializeTypeCorrectly( LogicalTypes.timestampMillis().addToSchema( org.apache.avro.SchemaBuilder.builder().longType()), ChronoUnit.MILLIS.between( LocalDateTime.of(LocalDate.ofEpochDay(0), LocalTime.MIDNIGHT), LocalDateTime.now()), Schema.OPTIONAL_INT64_SCHEMA ); }
@Test public void shouldSerializeString() { shouldSerializeTypeCorrectly( Schema.OPTIONAL_STRING_SCHEMA, "foobar", org.apache.avro.SchemaBuilder.builder().stringType(), new Utf8("foobar")); }
@Test public void shouldDeserializeDateToBigint() { shouldDeserializeTypeCorrectly( LogicalTypes.date().addToSchema( org.apache.avro.SchemaBuilder.builder().intType()), ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now()), Schema.OPTIONAL_INT64_SCHEMA ); }
@Test public void shouldDeserializeDateToInteger() { shouldDeserializeTypeCorrectly( LogicalTypes.date().addToSchema( org.apache.avro.SchemaBuilder.builder().intType()), (int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now()), Schema.OPTIONAL_INT32_SCHEMA ); }
@Test public void testFixedDefaultValueDrop() { Schema md5 = SchemaBuilder.builder().fixed("MD5").size(16); Schema frec = SchemaBuilder.builder().record("test") .fields().name("hash").type(md5).withDefault(new byte[16]).endRecord(); Schema.Field field = frec.getField("hash"); Assert.assertNotNull(field.defaultVal()); Assert.assertArrayEquals(new byte[16], (byte[]) field.defaultVal()); }
@Test public void shouldSerializeArray() { shouldSerializeTypeCorrectly( SchemaBuilder.array(Schema.OPTIONAL_INT32_SCHEMA).optional().build(), ImmutableList.of(1, 2, 3), org.apache.avro.SchemaBuilder.array().items( org.apache.avro.SchemaBuilder.builder() .unionOf().nullType().and().intType().endUnion()) ); }
@Test(expected = AvroRuntimeException.class) public void testFieldNullObjectProp() { SchemaBuilder.builder().record("MyRecord") .fields().name("myField") .prop("nullProp", (Object) null) .type().intType() .noDefault() .endRecord(); }