@Override public void setUp() throws Exception { Schema schema = SchemaBuilder.record("UnitTestRecord") .fields() .name("data").type().stringType().noDefault() .name("timestamp").type().nullable().longType().noDefault() .endRecord(); GenericRecordBuilder builder = new GenericRecordBuilder(schema); msg1 = builder.set("data", "foo").set("timestamp", 1467176315L).build(); msg2 = builder.set("data", "bar").set("timestamp", 1467176344L).build(); writer = new SpecificDatumWriter(schema); config = Mockito.mock(SecorConfig.class); when(config.getSchemaRegistryUrl()).thenReturn(""); secorSchemaRegistryClient = Mockito.mock(SecorSchemaRegistryClient.class); when(secorSchemaRegistryClient.getSchema(anyString())).thenReturn(schema); mFactory = new AvroFileReaderWriterFactory(config); when(secorSchemaRegistryClient.decodeMessage("test-avro-topic", AvroFileReaderWriterFactory.serializeAvroRecord(writer, msg1))).thenReturn(msg1); when(secorSchemaRegistryClient.decodeMessage("test-avro-topic", AvroFileReaderWriterFactory.serializeAvroRecord(writer, msg2))).thenReturn(msg2); mFactory.schemaRegistryClient = secorSchemaRegistryClient; }
@Override public void setUp() throws Exception { Schema schema = SchemaBuilder.record("UnitTestRecord") .fields() .name("data").type().stringType().noDefault() .name("timestamp").type().nullable().longType().noDefault() .endRecord(); GenericRecordBuilder builder = new GenericRecordBuilder(schema); msg1 = builder.set("data", "foo").set("timestamp", 1467176315L).build(); msg2 = builder.set("data", "bar").set("timestamp", 1467176344L).build(); writer = new SpecificDatumWriter(schema); config = Mockito.mock(SecorConfig.class); when(config.getSchemaRegistryUrl()).thenReturn(""); secorSchemaRegistryClient = Mockito.mock(SecorSchemaRegistryClient.class); when(secorSchemaRegistryClient.getSchema(anyString())).thenReturn(schema); mFactory = new AvroParquetFileReaderWriterFactory(config); when(secorSchemaRegistryClient.decodeMessage("test-avro-topic", AvroParquetFileReaderWriterFactory.serializeAvroRecord(writer, msg1))).thenReturn(msg1); when(secorSchemaRegistryClient.decodeMessage("test-avro-topic", AvroParquetFileReaderWriterFactory.serializeAvroRecord(writer, msg2))).thenReturn(msg2); mFactory.schemaRegistryClient = secorSchemaRegistryClient; }
@Test public void testRecord() { Schema schema = SchemaBuilder .record("myrecord").namespace("org.example").aliases("oldrecord").fields() .name("f0").aliases("f0alias").type().stringType().noDefault() .name("f1").doc("This is f1").type().longType().noDefault() .name("f2").type().nullable().booleanType().booleanDefault(true) .endRecord(); Assert.assertEquals("myrecord", schema.getName()); Assert.assertEquals("org.example", schema.getNamespace()); Assert.assertEquals("org.example.oldrecord", schema.getAliases().iterator().next()); Assert.assertFalse(schema.isError()); List<Schema.Field> fields = schema.getFields(); Assert.assertEquals(3, fields.size()); Assert.assertEquals( new Schema.Field("f0", Schema.create(Schema.Type.STRING), null, null), fields.get(0)); Assert.assertTrue(fields.get(0).aliases().contains("f0alias")); Assert.assertEquals( new Schema.Field("f1", Schema.create(Schema.Type.LONG), "This is f1", null), fields.get(1)); List<Schema> types = new ArrayList<>(); types.add(Schema.create(Schema.Type.BOOLEAN)); types.add(Schema.create(Schema.Type.NULL)); Schema optional = Schema.createUnion(types); Assert.assertEquals(new Schema.Field("f2", optional, null, true), fields.get(2)); }
/** * Shortcut for creating a nullable long field: a union of long and null * with a long default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().longType().longDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableLong(String fieldName, long defaultVal) { return name(fieldName).type().nullable().longType().longDefault(defaultVal); }
.name("requiredInt").type().intType().noDefault() .name("optionalInt").type().optional().intType() .name("nullableIntWithDefault").type().nullable().intType().intDefault(3) .endRecord(); .name("requiredInt").type().intType().noDefault() .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();
.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();
/** * Shortcut for creating a nullable boolean field: a union of boolean and * null with an boolean default. * <p/> * This is equivalent to: * * <pre> * name(fieldName).type().nullable().booleanType().booleanDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableBoolean(String fieldName, boolean defaultVal) { return name(fieldName) .type().nullable().booleanType().booleanDefault(defaultVal); }
/** * Shortcut for creating a nullable boolean field: a union of boolean and * null with an boolean default. * <p/> * This is equivalent to: * * <pre> * name(fieldName).type().nullable().booleanType().booleanDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableBoolean(String fieldName, boolean defaultVal) { return name(fieldName) .type().nullable().booleanType().booleanDefault(defaultVal); }
/** * Shortcut for creating a nullable long field: a union of long and null * with a long default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().longType().longDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableLong(String fieldName, long defaultVal) { return name(fieldName).type().nullable().longType().longDefault(defaultVal); }
/** * Shortcut for creating a nullable bytes field: a union of bytes and null * with a bytes default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().bytesType().bytesDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableBytes(String fieldName, byte[] defaultVal) { return name(fieldName).type().nullable().bytesType().bytesDefault(defaultVal); }
/** * Shortcut for creating a nullable int field: a union of int and null * with an int default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().intType().intDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableInt(String fieldName, int defaultVal) { return name(fieldName).type().nullable().intType().intDefault(defaultVal); }
/** * Shortcut for creating a nullable string field: a union of string and null * with a string default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().stringType().stringDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableString(String fieldName, String defaultVal) { return name(fieldName).type().nullable().stringType().stringDefault(defaultVal); }
/** * Shortcut for creating a nullable double field: a union of double and null * with a double default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().doubleType().doubleDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableDouble(String fieldName, double defaultVal) { return name(fieldName).type().nullable().doubleType().doubleDefault(defaultVal); }
/** * Shortcut for creating a nullable float field: a union of float and null * with a float default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().floatType().floatDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableFloat(String fieldName, float defaultVal) { return name(fieldName).type().nullable().floatType().floatDefault(defaultVal); }
/** * Shortcut for creating a nullable string field: a union of string and null * with a string default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().stringType().stringDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableString(String fieldName, String defaultVal) { return name(fieldName).type().nullable().stringType().stringDefault(defaultVal); }
/** * Shortcut for creating a nullable double field: a union of double and null * with a double default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().doubleType().doubleDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableDouble(String fieldName, double defaultVal) { return name(fieldName).type().nullable().doubleType().doubleDefault(defaultVal); }
/** * Shortcut for creating a nullable long field: a union of long and null * with a long default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().longType().longDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableLong(String fieldName, long defaultVal) { return name(fieldName).type().nullable().longType().longDefault(defaultVal); }
/** * Shortcut for creating a nullable bytes field: a union of bytes and null * with a bytes default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().bytesType().bytesDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableBytes(String fieldName, byte[] defaultVal) { return name(fieldName).type().nullable().bytesType().bytesDefault(defaultVal); }
/** * Shortcut for creating a nullable float field: a union of float and null * with a float default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().floatType().floatDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableFloat(String fieldName, float defaultVal) { return name(fieldName).type().nullable().floatType().floatDefault(defaultVal); }
/** * Shortcut for creating a nullable int field: a union of int and null * with an int default.<p/> * This is equivalent to: * <pre> * name(fieldName).type().nullable().intType().intDefault(defaultVal) * </pre> */ public FieldAssembler<R> nullableInt(String fieldName, int defaultVal) { return name(fieldName).type().nullable().intType().intDefault(defaultVal); }