@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)); }
fieldAssembler = fieldAssembler.name(pom.getPredicate().getId().replaceAll("[^\\w]", "_")).type().unionOf().map().values().unionOf().map().values().stringType().and().stringType().and().nullType().endUnion().and().nullType().endUnion().noDefault(); if(targetSchema == null) fieldAssembler = fieldAssembler.name(predicateShortHand).type().unionOf().array().items().stringType().and().stringType().and().nullType().endUnion().noDefault();
.requiredLong("created_at") .requiredLong("version") .name("properties").type().optional().map().values().stringType() .endRecord();
@Test public void testSchemaInferenceMap() throws Exception { Schema recordSchema = SchemaBuilder.record("Test").fields() .requiredString("aString") .name("aMap").type().map().values().stringType().noDefault() .endRecord(); String jsonSample = "{" + "\"aString\": \"triangle\"," + "\"aMap\": { \"left\": \"timid\", \"right\": \"dictionary\" }" + "}"; JsonNode datum = JsonUtil.parse(jsonSample); Assert.assertEquals("Should produce expected schema", recordSchema, JsonUtil.inferSchemaWithMaps(datum, "Test")); Map<String, Object> aMap = Maps.newLinkedHashMap(); aMap.put("left", "timid"); aMap.put("right", "dictionary"); GenericData.Record expected = new GenericData.Record(recordSchema); expected.put("aString", "triangle"); expected.put("aMap", aMap); Assert.assertEquals("Should convert to record", expected, convertGeneric(datum, recordSchema)); }
@Test public void testAllAvroTypes() { Schema r = 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(); assertTrue(SchemaValidationUtil.canRead(r, r)); } }
private Schema avroType(Type spannerType) { switch (spannerType.getCode()) { case BOOL: return SchemaBuilder.builder().booleanType(); case INT64: return SchemaBuilder.builder().longType(); case FLOAT64: return SchemaBuilder.builder().doubleType(); case STRING: return SchemaBuilder.builder().stringType(); case BYTES: return SchemaBuilder.builder().bytesType(); case TIMESTAMP: return SchemaBuilder.builder().stringType(); case DATE: return SchemaBuilder.builder().stringType(); case ARRAY: Schema avroItemsType = avroType(spannerType.getArrayElementType()); return SchemaBuilder.builder().array().items().type(wrapAsNullable(avroItemsType)); default: throw new IllegalArgumentException("Unknown spanner type " + spannerType); } }
public static Schema getSchema(@NotEmpty final String type) { final SchemaBuilder.TypeBuilder<Schema> schema = SchemaBuilder.builder(); switch (type) { case CassandraSchemaField.STRING_TYPE: return schema.stringType(); case CassandraSchemaField.BOOLEAN_TYPE: return schema.booleanType(); case CassandraSchemaField.DOUBLE_TYPE: return schema.doubleType(); case CassandraSchemaField.FLOAT_TYPE: return schema.floatType(); case CassandraSchemaField.LONG_TYPE: return schema.longType(); case CassandraSchemaField.INT_TYPE: return schema.intType(); default: throw new IllegalArgumentException("Type " + type + " invalid for converting to Cassandra field"); } }
@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())); }
.type().array().items().stringType().noDefault()
.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("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()
break; case STRING: fieldAssembler = fieldAssembler.name(fieldSpec.getName()).type().array().items().stringType().noDefault(); break; default:
schema = SchemaBuilder.record("TestRecord").fields() .name("id").type().stringType().noDefault() .name("key").type().map().values().stringType().noDefault() .name("data").type(dataRecordSchema).noDefault() .endRecord();
switch (dataType) { case "string": returnSchema = typeBuilder.stringType(); break; case "boolean":
@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)); }
@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 testString() { Schema.Type type = Schema.Type.STRING; Schema simple = SchemaBuilder.builder().stringType(); Schema expected = primitive(type, simple); Schema built1 = SchemaBuilder.builder() .stringBuilder().prop("p", "v").endString(); Assert.assertEquals(expected, built1); }
@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 shouldSerializeString() { shouldSerializeTypeCorrectly( Schema.OPTIONAL_STRING_SCHEMA, "foobar", org.apache.avro.SchemaBuilder.builder().stringType(), new Utf8("foobar")); }