@Test public void testSplitSchemaBuild() { Schema s = SchemaBuilder .record("HandshakeRequest") .namespace("org.apache.avro.ipc").fields() .name("clientProtocol").type().optional().stringType() .name("meta").type().optional().map().values().bytesType() .endRecord(); String schemaString = s.toString(); int mid = schemaString.length() / 2; Schema parsedStringSchema = new org.apache.avro.Schema.Parser().parse(s.toString()); Schema parsedArrayOfStringSchema = new org.apache.avro.Schema.Parser().parse (schemaString.substring(0, mid), schemaString.substring(mid)); assertNotNull(parsedStringSchema); assertNotNull(parsedArrayOfStringSchema); assertEquals(parsedStringSchema.toString(), parsedArrayOfStringSchema.toString()); }
@Test public void testRecursiveRecord() { Schema schema = SchemaBuilder.record("LongList").fields() .name("value").type().longType().noDefault() .name("next").type().optional().type("LongList") .endRecord(); Assert.assertEquals("LongList", schema.getName()); List<Schema.Field> fields = schema.getFields(); Assert.assertEquals(2, fields.size()); Assert.assertEquals( new Schema.Field("value", Schema.create(Schema.Type.LONG), null, null), fields.get(0)); Assert.assertEquals( Schema.Type.UNION, fields.get(1).schema().getType()); Assert.assertEquals( Schema.Type.NULL, fields.get(1).schema().getTypes().get(0).getType()); Schema recordSchema = fields.get(1).schema().getTypes().get(1); Assert.assertEquals(Schema.Type.RECORD, recordSchema.getType()); Assert.assertEquals("LongList", recordSchema.getName()); Assert.assertEquals(NullNode.getInstance(), fields.get(1).defaultValue()); }
public static File generateEnumTypes(String filename, int nrows, String[][] categories) throws IOException { assert categories.length == 2 : "Needs only 2 columns"; File parentDir = Files.createTempDir(); File f = new File(parentDir, filename); DatumWriter<GenericRecord> w = new GenericDatumWriter<GenericRecord>(); DataFileWriter<GenericRecord> dw = new DataFileWriter<GenericRecord>(w); Schema enumSchema1 = SchemaBuilder.enumeration("CEnum1").symbols(categories[0]); Schema enumSchema2 = SchemaBuilder.enumeration("CEnum2").symbols(categories[1]); Schema schema = SchemaBuilder.builder() .record("test_enum_types").fields() .name("CEnum").type(enumSchema1).noDefault() .name("CUEnum").type().optional().type(enumSchema2) .endRecord(); System.out.println(schema); int numOfCategories1 = categories[0].length; int numOfCategories2 = categories[1].length; try { dw.create(schema, f); for (int i = 0; i < nrows; i++) { GenericRecord gr = new GenericData.Record(schema); gr.put("CEnum", new GenericData.EnumSymbol(enumSchema1, categories[0][i % numOfCategories1])); gr.put("CUEnum", i % (numOfCategories2+1) == 0 ? null : new GenericData.EnumSymbol(enumSchema2, categories[1][i % numOfCategories2])); dw.append(gr); } return f; } finally { dw.close();; } }
Schema full = SchemaBuilder.record("Blah").fields() .name("rbool").type().booleanType().noDefault() .name("obool").type().optional().booleanType() .name("nbool").type().nullable().booleanType().booleanDefault(true) .name("rint").type().intType().noDefault() .name("oint").type().optional().intType() .name("nint").type().nullable().intType().intDefault(1) .name("rlong").type().longType().noDefault() .name("olong").type().optional().longType() .name("nlong").type().nullable().longType().longDefault(2L) .name("rfloat").type().floatType().noDefault() .name("ofloat").type().optional().floatType() .name("nfloat").type().nullable().floatType().floatDefault(-1.1f) .name("rdouble").type().doubleType().noDefault() .name("odouble").type().optional().doubleType() .name("ndouble").type().nullable().doubleType().doubleDefault(99.9d) .name("rstring").type().stringType().noDefault() .name("ostring").type().optional().stringType() .name("nstring").type().nullable().stringType().stringDefault("def") .name("rbytes").type().bytesType().noDefault() .name("obytes").type().optional().bytesType() .name("nbytes").type().nullable().bytesType().bytesDefault(new byte[] {1,2,3}) .endRecord();
Schema writeSchema = SchemaBuilder.record("r").fields() .name("requiredInt").type().intType().noDefault() .name("optionalInt").type().optional().intType() .name("nullableIntWithDefault").type().nullable().intType().intDefault(3) .endRecord(); .name("optionalInt").type().optional().intType() .name("nullableIntWithDefault").type().nullable().intType().intDefault(3) .name("newOptionalInt").type().optional().intType() .name("newNullableIntWithDefault").type().nullable().intType().intDefault(5) .endRecord();
schema = SchemaBuilder.builder() .record("test_union_types").fields() .name("CUString").type().optional().stringType() .name("CUBytes").type().optional().bytesType() .name("CUInt").type().optional().intType() .name("CULong").type().optional().longType() .name("CUFloat").type().optional().floatType() .name("CUDouble").type().optional().doubleType() .name("CUBoolean").type().optional().booleanType() .endRecord(); try {
/** * Shortcut for creating an optional double field: a union of null and double * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().doubleType() * </pre> */ public FieldAssembler<R> optionalDouble(String fieldName) { return name(fieldName).type().optional().doubleType(); }
/** * Shortcut for creating an optional long field: a union of null and long * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().longType() * </pre> */ public FieldAssembler<R> optionalLong(String fieldName) { return name(fieldName).type().optional().longType(); }
/** * Shortcut for creating an optional boolean field: a union of null and * boolean with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().booleanType() * </pre> */ public FieldAssembler<R> optionalBoolean(String fieldName) { return name(fieldName).type().optional().booleanType(); }
/** * Shortcut for creating an optional string field: a union of null and string * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().stringType() * </pre> */ public FieldAssembler<R> optionalString(String fieldName) { return name(fieldName).type().optional().stringType(); }
/** * Shortcut for creating an optional float field: a union of null and float * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().floatType() * </pre> */ public FieldAssembler<R> optionalFloat(String fieldName) { return name(fieldName).type().optional().floatType(); }
/** * Shortcut for creating an optional int field: a union of null and int * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().intType() * </pre> */ public FieldAssembler<R> optionalInt(String fieldName) { return name(fieldName).type().optional().intType(); }
/** * Shortcut for creating an optional boolean field: a union of null and * boolean with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().booleanType() * </pre> */ public FieldAssembler<R> optionalBoolean(String fieldName) { return name(fieldName).type().optional().booleanType(); }
/** * Shortcut for creating an optional bytes field: a union of null and bytes * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().bytesType() * </pre> */ public FieldAssembler<R> optionalBytes(String fieldName) { return name(fieldName).type().optional().bytesType(); }
/** * Shortcut for creating an optional string field: a union of null and string * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().stringType() * </pre> */ public FieldAssembler<R> optionalString(String fieldName) { return name(fieldName).type().optional().stringType(); }
/** * Shortcut for creating an optional bytes field: a union of null and bytes * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().bytesType() * </pre> */ public FieldAssembler<R> optionalBytes(String fieldName) { return name(fieldName).type().optional().bytesType(); }
/** * Shortcut for creating an optional double field: a union of null and double * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().doubleType() * </pre> */ public FieldAssembler<R> optionalDouble(String fieldName) { return name(fieldName).type().optional().doubleType(); }
/** * Shortcut for creating an optional long field: a union of null and long * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().longType() * </pre> */ public FieldAssembler<R> optionalLong(String fieldName) { return name(fieldName).type().optional().longType(); }
/** * Shortcut for creating an optional int field: a union of null and int * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().intType() * </pre> */ public FieldAssembler<R> optionalInt(String fieldName) { return name(fieldName).type().optional().intType(); }
/** * Shortcut for creating an optional float field: a union of null and float * with null default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().optional().floatType() * </pre> */ public FieldAssembler<R> optionalFloat(String fieldName) { return name(fieldName).type().optional().floatType(); }