@Test public void testReadUUIDList() throws IOException { Schema uuidListSchema = SchemaBuilder.record(RecordWithUUIDList.class.getName()) .fields() .name("uuids").type().array().items().stringType().noDefault() .endRecord(); uuidListSchema.getField("uuids").schema().addProp( SpecificData.CLASS_PROP, List.class.getName()); LogicalTypes.uuid().addToSchema( uuidListSchema.getField("uuids").schema().getElementType()); UUID u1 = UUID.randomUUID(); UUID u2 = UUID.randomUUID(); GenericRecord r = new GenericData.Record(uuidListSchema); r.put("uuids", Arrays.asList(u1.toString(), u2.toString())); RecordWithUUIDList expected = new RecordWithUUIDList(); expected.uuids = Arrays.asList(u1, u2); File test = write(uuidListSchema, r); Assert.assertEquals("Should convert Strings to UUIDs", expected, read(REFLECT.createDatumReader(uuidListSchema), test).get(0)); }
@Test public void testReadUUIDArray() throws IOException { Schema uuidArraySchema = SchemaBuilder.record(RecordWithUUIDArray.class.getName()) .fields() .name("uuids").type().array().items().stringType().noDefault() .endRecord(); LogicalTypes.uuid().addToSchema( uuidArraySchema.getField("uuids").schema().getElementType()); UUID u1 = UUID.randomUUID(); UUID u2 = UUID.randomUUID(); GenericRecord r = new GenericData.Record(uuidArraySchema); r.put("uuids", Arrays.asList(u1.toString(), u2.toString())); RecordWithUUIDArray expected = new RecordWithUUIDArray(); expected.uuids = new UUID[] {u1, u2}; File test = write(uuidArraySchema, r); Assert.assertEquals("Should convert Strings to UUIDs", expected, read(REFLECT.createDatumReader(uuidArraySchema), test).get(0)); }
@Test public void testWriteUUIDList() throws IOException { Schema uuidListSchema = SchemaBuilder.record(RecordWithUUIDList.class.getName()) .fields() .name("uuids").type().array().items().stringType().noDefault() .endRecord(); uuidListSchema.getField("uuids").schema().addProp( SpecificData.CLASS_PROP, List.class.getName()); LogicalTypes.uuid().addToSchema( uuidListSchema.getField("uuids").schema().getElementType()); Schema stringArraySchema = SchemaBuilder.record("RecordWithUUIDArray") .fields() .name("uuids").type().array().items().stringType().noDefault() .endRecord(); stringArraySchema.getField("uuids").schema() .addProp(SpecificData.CLASS_PROP, List.class.getName()); UUID u1 = UUID.randomUUID(); UUID u2 = UUID.randomUUID(); GenericRecord expected = new GenericData.Record(stringArraySchema); expected.put("uuids", Arrays.asList(u1.toString(), u2.toString())); RecordWithUUIDList r = new RecordWithUUIDList(); r.uuids = Arrays.asList(u1, u2); File test = write(REFLECT, uuidListSchema, r); Assert.assertEquals("Should read UUIDs as Strings", expected, read(REFLECT.createDatumReader(stringArraySchema), test).get(0)); }
@Test public void testListOfStructs() { Type list = Types.ListType.ofRequired(34, Types.StructType.of( required(35, "lat", Types.FloatType.get()), required(36, "long", Types.FloatType.get()) )); Schema schema = addElementId(34, SchemaBuilder.array().items( record("r34", requiredField(35, "lat", Schema.create(Schema.Type.FLOAT)), requiredField(36, "long", Schema.create(Schema.Type.FLOAT))) )); Assert.assertEquals("Avro schema to list", list, AvroSchemaUtil.convert(schema)); Assert.assertEquals("List to Avro schema", schema, AvroSchemaUtil.convert(list)); }
@Test public void testSchemaMergeArrayTypes() throws Exception { String jsonSample = "{\"id\": 1}\n{\"id\": 2}" + "{\"id\": \"socket\", \"anArray\": [33, 34, 35]}" + "{\"id\": 3, \"anArray\": [\"badger\", \"porcupine\"]}"; Schema schema = SchemaBuilder.record("Test").fields() .name("id").type() .unionOf().intType().and().stringType().endUnion().noDefault() .name("anArray").type().optional().array().items() .unionOf().intType().and().stringType().endUnion() .endRecord(); Assert.assertEquals("Should match expected schema", schema, mergeSchemas(jsonSample)); }
@Override public Schema getSchema() { return SchemaBuilder.unionOf().array().prop(SchemaConstants.JAVA_CLASS_FLAG, getDatumClass().getCanonicalName()).items() .fixed("Inet4").size(4).and().fixed("Inet6").size(16).endUnion(); }
@Test public void testList() { Type list = Types.ListType.ofRequired(34, Types.UUIDType.get()); Schema schema = addElementId(34, SchemaBuilder.array().items( LogicalTypes.uuid().addToSchema(Schema.createFixed("uuid_fixed", null, null, 16)))); Assert.assertEquals("Avro schema to list", list, AvroSchemaUtil.convert(schema)); Assert.assertEquals("List to Avro schema", schema, AvroSchemaUtil.convert(list)); }
@Test(expected = IllegalArgumentException.class) public void testRejectsNonRecordSchemas() { final DatasetDescriptor desc = new DatasetDescriptor.Builder() .schema(SchemaBuilder.array().items().stringType()) .build(); new CSVFileReader<GenericData.Record>(localfs, csvFile, desc, DataModelUtil.accessor(GenericData.Record.class, desc.getSchema())); }
.name("mapF").type().map().values().stringType() .mapDefault(mapdef) .name("arrayF").type().array().items().stringType() .arrayDefault(arrdef) .name("recordF").type().record("inner").fields() .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()
|| dataType.equals(DataType.list(firstArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().array() .items(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(firstArg))).endUnion().noDefault(); } else {
switch (dataType) { case INT: fieldAssembler = fieldAssembler.name(fieldSpec.getName()).type().array().items().intType().noDefault(); break; case LONG: fieldAssembler = fieldAssembler.name(fieldSpec.getName()).type().array().items().longType().noDefault(); break; case FLOAT: fieldAssembler = fieldAssembler.name(fieldSpec.getName()).type().array().items().floatType().noDefault(); break; case DOUBLE: fieldAssembler = fieldAssembler.name(fieldSpec.getName()).type().array().items().doubleType().noDefault(); break; case STRING: fieldAssembler = fieldAssembler.name(fieldSpec.getName()).type().array().items().stringType().noDefault(); break; default:
@Test public void testWriteUUIDArray() throws IOException { Schema uuidArraySchema = SchemaBuilder.record(RecordWithUUIDArray.class.getName()) .fields() .name("uuids").type().array().items().stringType().noDefault() .endRecord(); LogicalTypes.uuid().addToSchema( uuidArraySchema.getField("uuids").schema().getElementType()); Schema stringArraySchema = SchemaBuilder.record("RecordWithUUIDArray") .fields() .name("uuids").type().array().items().stringType().noDefault() .endRecord(); stringArraySchema.getField("uuids").schema() .addProp(SpecificData.CLASS_PROP, List.class.getName()); UUID u1 = UUID.randomUUID(); UUID u2 = UUID.randomUUID(); GenericRecord expected = new GenericData.Record(stringArraySchema); List<String> uuids = new ArrayList<>(); uuids.add(u1.toString()); uuids.add(u2.toString()); expected.put("uuids", uuids); RecordWithUUIDArray r = new RecordWithUUIDArray(); r.uuids = new UUID[] {u1, u2}; File test = write(REFLECT, uuidArraySchema, r); Assert.assertEquals("Should read UUIDs as Strings", expected, read(ReflectData.get().createDatumReader(stringArraySchema), test).get(0)); }
.endRecord(); final org.apache.avro.Schema avroSchema = org.apache.avro.SchemaBuilder.array().items(entrySchema); final Map<String, Long> value = ImmutableMap.of("foo", 123L); final List<GenericRecord> avroValue = new LinkedList<>();
@Test public void testUnionFieldBuild() { SchemaBuilder.record("r").fields() .name("allUnion").type().unionOf() .booleanType().and() .intType().and() .longType().and() .floatType().and() .doubleType().and() .stringType().and() .bytesType().and() .nullType().and() .fixed("Fix").size(1).and() .enumeration("Enu").symbols("Q").and() .array().items().intType().and() .map().values().longType().and() .record("Rec").fields() .name("one").type("Fix").noDefault() .endRecord() .endUnion().booleanDefault(false) .endRecord(); }
@Test public void testAllTypes() throws SchemaValidationException { Schema s = SchemaBuilder.record("r").fields() .requiredBoolean("boolF") .requiredInt("intF") .requiredLong("longF") .requiredFloat("floatF") .requiredDouble("doubleF") .requiredString("stringF") .requiredBytes("bytesF") .name("fixedF1").type().fixed("F1").size(1).noDefault() .name("enumF").type().enumeration("E1").symbols("S").noDefault() .name("mapF").type().map().values().stringType().noDefault() .name("arrayF").type().array().items().stringType().noDefault() .name("recordF").type().record("inner").fields() .name("f").type().intType().noDefault() .endRecord().noDefault() .optionalBoolean("boolO") .endRecord(); testValidatorPasses(builder.mutualReadStrategy().validateLatest(), s, s); }
@Test public void testArray() { Schema longSchema = Schema.create(Schema.Type.LONG); Schema expected = Schema.createArray(longSchema); Schema schema1 = SchemaBuilder.array().items().longType(); Assert.assertEquals(expected, schema1); Schema schema2 = SchemaBuilder.array().items(longSchema); Assert.assertEquals(expected, schema2); Schema schema3 = SchemaBuilder.array().prop("p", "v") .items().type("long"); expected.addProp("p", "v"); Assert.assertEquals(expected, schema3); }
@Test public void testReflectedSchema() { Schema expected = SchemaBuilder.record(RecordWithUUIDList.class.getName()) .fields() .name("uuids").type().array().items().stringType().noDefault() .endRecord(); expected.getField("uuids").schema().addProp( SpecificData.CLASS_PROP, List.class.getName()); LogicalTypes.uuid().addToSchema( expected.getField("uuids").schema().getElementType()); Schema actual = REFLECT.getSchema(RecordWithUUIDList.class); Assert.assertEquals("Should use the UUID logical type", expected, actual); }
@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 public void shouldDeserializeArrayToArray() { shouldDeserializeTypeCorrectly( org.apache.avro.SchemaBuilder.array().items().intType(), ImmutableList.of(1, 2, 3, 4, 5, 6), SchemaBuilder.array(Schema.OPTIONAL_INT32_SCHEMA).optional().build() ); }