.fields().name("myField") .prop("mapProp", values) .type().intType() .noDefault() .endRecord();
.fields().name("myField") .prop("arrayProp", values) .type().intType() .noDefault() .endRecord();
@Test public void testBasicConvert() throws Exception { Schema dataRecordSchema = SchemaBuilder.record("Data") .fields() .name("data").type().bytesType().noDefault() .name("flags").type().intType().noDefault() .endRecord(); Schema schema = SchemaBuilder.record("TestRecord") .fields() .name("key").type().stringType().noDefault() .name("data").type(dataRecordSchema).noDefault() .endRecord(); GenericData.Record testRecord = new GenericData.Record(schema); String testContent = "hello world"; GenericData.Record dataRecord = new GenericData.Record(dataRecordSchema); dataRecord.put("data", ByteBuffer.wrap(testContent.getBytes(Charset.forName("UTF-8")))); dataRecord.put("flags", 0); testRecord.put("key", "hello"); testRecord.put("data", dataRecord); Converter<Schema, String, GenericRecord, TupleDocument> recordConverter = new AvroToCouchbaseTupleConverter(); TupleDocument returnDoc = recordConverter.convertRecord("", testRecord, null).iterator().next(); byte[] returnedBytes = new byte[returnDoc.content().value1().readableBytes()]; returnDoc.content().value1().readBytes(returnedBytes); Assert.assertEquals(returnedBytes, testContent.getBytes(Charset.forName("UTF-8"))); int returnedFlags = returnDoc.content().value2(); Assert.assertEquals(returnedFlags, 0); }
.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)
switch (dataType) { case INT: fieldAssembler = fieldAssembler.name(fieldSpec.getName()).type().intType().noDefault(); break; case LONG:
static GenericRecord getTestAvroRecord(String identifier, PayloadType payloadType) { Schema dataRecordSchema = SchemaBuilder.record("Data").fields().name("data").type().bytesType().noDefault().name("flags").type().intType() .noDefault().endRecord();
SchemaBuilder.record("Data").fields().name("data").type().bytesType().noDefault().name("flags").type().intType() .noDefault().endRecord();
.namespace("org.example") .fields() .name("myint").type().intType().noDefault() .endRecord(); Schema s2 = SchemaBuilder.record("org.example.myrecord") .fields() .name("myint").type().intType().noDefault() .endRecord(); Schema s3 = SchemaBuilder.record("org.example.myrecord") .namespace("org.example2") .fields() .name("myint").type().intType().noDefault() .endRecord(); Schema s4 = SchemaBuilder.builder("org.example").record("myrecord") .fields() .name("myint").type().intType().noDefault() .endRecord();
try { Schema dataRecordSchema = SchemaBuilder.record("Data").fields().name("data").type().bytesType().noDefault().name("flags").type().intType() .noDefault().endRecord();
.prop("byteProp", new byte[] {0x41, 0x42, 0x43} ) .prop("stringProp", "abc" ) .type().intType() .noDefault() .endRecord();
throws IOException, DataConversionException, ExecutionException, InterruptedException { final Schema dataRecordSchema = SchemaBuilder.record("Data").fields().name("data").type().bytesType().noDefault().name("flags").type().intType() .noDefault().endRecord();
Schema schemaV1 = SchemaBuilder.record("Foo") .fields() .name("data_field_1").type().intType().noDefault() .name("timestamp").type().longType().noDefault() .endRecord(); .name("data_field_1").type().intType().noDefault() .name("data_field_2").type().stringType().noDefault() .name("timestamp").type().longType().noDefault()
@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 testDefaultTypes() { Integer intDef = 1; Long longDef = 2L; Float floatDef = 3F; Double doubleDef = 4D; Schema schema = SchemaBuilder.record("r").fields() .name("int").type().intType().intDefault(intDef) .name("long").type().longType().longDefault(longDef) .name("float").type().floatType().floatDefault(floatDef) .name("double").type().doubleType().doubleDefault(doubleDef) .endRecord(); Assert.assertEquals("int field default type or value mismatch", intDef, schema.getField("int").defaultVal()); Assert.assertEquals("long field default type or value mismatch", longDef, schema.getField("long").defaultVal()); Assert.assertEquals("float field default type or value mismatch", floatDef, schema.getField("float").defaultVal()); Assert.assertEquals("double field default type or value mismatch", doubleDef, schema.getField("double").defaultVal()); }
@Test public void recordWrittenWithExtendedSchemaCanBeReadWithOriginalSchemaButLossOfData() throws Exception { Schema writer = SchemaBuilder.record(RECORD_A) // .fields() // .name("newTopField").type().stringType().noDefault() // .name(FIELD_A).type().intType().noDefault() // .endRecord(); Record record = defaultRecordWithSchema(writer, FIELD_A, 42); record.put("newTopField", "not decoded"); byte[] encoded = encodeGenericBlob(record); Record decoded = decodeGenericBlob(INT_RECORD, writer, encoded); assertEquals(42, decoded.get(FIELD_A)); assertNull(decoded.get("newTopField")); }
@Test public void readerWithDefaultValueIsApplied() throws Exception { Schema reader = SchemaBuilder.record(RECORD_A) // .fields() // .name("newFieldWithDefault").type().intType().intDefault(314) // .name(FIELD_A).type().intType().noDefault() // .endRecord(); Record record = defaultRecordWithSchema(INT_RECORD, FIELD_A, 42); byte[] encoded = encodeGenericBlob(record); Record decoded = decodeGenericBlob(reader, INT_RECORD, encoded); assertEquals(42, decoded.get(FIELD_A)); assertEquals(314, decoded.get("newFieldWithDefault")); }
/** * Shortcut for creating an int field with the given name and no default. * <p/>This is equivalent to: * <pre> * name(fieldName).type().intType().noDefault() * </pre> */ public FieldAssembler<R> requiredInt(String fieldName) { return name(fieldName).type().intType().noDefault(); }
@Test(expected = AvroRuntimeException.class) public void testFieldNullObjectProp() { SchemaBuilder.builder().record("MyRecord") .fields().name("myField") .prop("nullProp", (Object) null) .type().intType() .noDefault() .endRecord(); }
@Test public void readerWithoutDefaultValueThrowsException() throws Exception { expectedException.expect(AvroTypeException.class); expectedException.expectMessage("missing required field newField"); Schema reader = SchemaBuilder.record(RECORD_A) // .fields() // .name("newField").type().intType().noDefault() // .name(FIELD_A).type().intType().noDefault() // .endRecord(); Record record = defaultRecordWithSchema(INT_RECORD, FIELD_A, 42); byte[] encoded = encodeGenericBlob(record); decodeGenericBlob(reader, INT_RECORD, encoded); }
/** * Shortcut for creating an int field with the given name and no default. * <p/>This is equivalent to: * <pre> * name(fieldName).type().intType().noDefault() * </pre> */ public FieldAssembler<R> requiredInt(String fieldName) { return name(fieldName).type().intType().noDefault(); }