@Test public void testAutoSchemaProvider() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); assertEquals(EMPTY_SCHEMA, registry.getSchema(TestSchemaClass.class)); }
Coder<T> coder = SchemaCoder.of( schemaRegistry.getSchema(typeDescriptor), schemaRegistry.getToRowFunction(typeDescriptor), schemaRegistry.getFromRowFunction(typeDescriptor)); return coder; } catch (NoSuchSchemaException e) {
@Test public void testRegisterJavaBean() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); registry.registerJavaBean(SimpleBean.class); Schema schema = registry.getSchema(SimpleBean.class); assertTrue(SIMPLE_BEAN_SCHEMA.equivalent(schema)); } }
@Test public void testRegisterPojo() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); registry.registerPOJO(SimplePOJO.class); Schema schema = registry.getSchema(SimplePOJO.class); assertTrue(SIMPLE_POJO_SCHEMA.equivalent(schema)); }
@Test public void testNestedArraysGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SchemaTestUtils.assertSchemaEquivalent( NESTED_ARRAYS_POJO_SCHEMA, registry.getSchema(NestedArraysPOJO.class)); List<List<String>> listOfLists = Lists.newArrayList( Lists.newArrayList("a", "b", "c"), Lists.newArrayList("d", "e", "f"), Lists.newArrayList("g", "h", "i")); NestedArraysPOJO pojo = new NestedArraysPOJO(listOfLists); Row row = registry.getToRowFunction(NestedArraysPOJO.class).apply(pojo); assertEquals(listOfLists, row.getArray("lists")); }
@Test public void testNNestedullValuesSetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row = Row.withSchema(NESTED_NULLABLE_SCHEMA).addValue(null).build(); POJOWithNestedNullable pojo = registry.getFromRowFunction(POJOWithNestedNullable.class).apply(row); assertNull(pojo.nested); } }
@Test public void testRecursiveArrayGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SchemaTestUtils.assertSchemaEquivalent( NESTED_ARRAY_BEAN_SCHEMA, registry.getSchema(NestedArrayBean.class)); SimpleBean simple1 = createSimple("string1"); SimpleBean simple2 = createSimple("string2"); SimpleBean simple3 = createSimple("string3"); NestedArrayBean bean = new NestedArrayBean(simple1, simple2, simple3); Row row = registry.getToRowFunction(NestedArrayBean.class).apply(bean); List<Row> rows = row.getArray("beans"); assertSame(simple1, registry.getFromRowFunction(NestedArrayBean.class).apply(rows.get(0))); assertSame(simple2, registry.getFromRowFunction(NestedArrayBean.class).apply(rows.get(1))); assertSame(simple3, registry.getFromRowFunction(NestedArrayBean.class).apply(rows.get(2))); }
@Test public void testFromRowWithGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SimplePOJO pojo = createSimple("string"); Row row = registry.getToRowFunction(SimplePOJO.class).apply(pojo); // Test that the fromRowFunction simply returns the original object back. SimplePOJO extracted = registry.getFromRowFunction(SimplePOJO.class).apply(row); assertSame(pojo, extracted); }
@Test public void testNestedNullValuesGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row = registry .getToRowFunction(POJOWithNestedNullable.class) .apply(new POJOWithNestedNullable(null)); assertNull(row.getValue("nested")); }
/** * Get a schema for a given {@link Class} type. If no schema exists, throws {@link * NoSuchSchemaException}. */ public <T> Schema getSchema(Class<T> clazz) throws NoSuchSchemaException { return getSchema(TypeDescriptor.of(clazz)); }
@Test public void testRegisterProvider() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); registry.registerSchemaProvider(new Provider()); tryGetters(registry); }
private void tryGetters(SchemaRegistry registry) throws NoSuchSchemaException { assertEquals(STRING_SCHEMA, registry.getSchema(String.class)); assertEquals(STRING_SCHEMA, registry.getSchema(TypeDescriptors.strings())); assertEquals( Row.withSchema(STRING_SCHEMA).addValue("foobar").build(), registry.getToRowFunction(String.class).apply("foobar")); assertEquals( Row.withSchema(STRING_SCHEMA).addValue("foobar").build(), registry.getToRowFunction(TypeDescriptors.strings()).apply("foobar")); assertEquals(INTEGER_SCHEMA, registry.getSchema(Integer.class)); assertEquals(INTEGER_SCHEMA, registry.getSchema(TypeDescriptors.integers())); assertEquals( Row.withSchema(INTEGER_SCHEMA).addValue(42).build(), registry.getToRowFunction(Integer.class).apply(42)); assertEquals( Row.withSchema(INTEGER_SCHEMA).addValue(42).build(), registry.getToRowFunction(TypeDescriptors.integers()).apply(42)); thrown.expect(NoSuchSchemaException.class); registry.getSchema(Double.class); }
@Test public void testRegisterForType() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); registry.registerSchemaForType( TypeDescriptors.strings(), STRING_SCHEMA, s -> Row.withSchema(STRING_SCHEMA).addValue(s).build(), r -> r.getString("string")); registry.registerSchemaForType( TypeDescriptors.integers(), INTEGER_SCHEMA, s -> Row.withSchema(INTEGER_SCHEMA).addValue(s).build(), r -> r.getInt32("integer")); tryGetters(registry); }
/** * Register a POJO type for automatic schema inference. * * <p>Currently schema field names will match field names in the POJO, and all fields must be * mutable (i.e. no final fields). The Java object is expected to have implemented a correct * .equals() and .hashCode methods The equals method must be completely determined by the schema * fields. i.e. if the object has hidden fields that are not reflected in the schema but are * compared in equals, then results will be incorrect. */ public <T> void registerPOJO(TypeDescriptor<T> typeDescriptor) { registerSchemaProvider(typeDescriptor, new JavaFieldSchema()); }
@Test public void testRegisterForClass() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); registry.registerSchemaForClass( String.class, STRING_SCHEMA, s -> Row.withSchema(STRING_SCHEMA).addValue(s).build(), r -> r.getString("string")); registry.registerSchemaForClass( Integer.class, INTEGER_SCHEMA, s -> Row.withSchema(INTEGER_SCHEMA).addValue(s).build(), r -> r.getInt32("integer")); tryGetters(registry); }
public SchemaRegistry getSchemaRegistry() { if (schemaRegistry == null) { schemaRegistry = SchemaRegistry.createDefault(); } return schemaRegistry; }
/** * Register a POJO type for automatic schema inference. * * <p>Currently schema field names will match field names in the POJO, and all fields must be * mutable (i.e. no final fields). */ public <T> void registerPOJO(Class<T> clazz) { registerPOJO(TypeDescriptor.of(clazz)); }
/** * Register a JavaBean type for automatic schema inference. * * <p>Currently schema field names will match getter names in the bean, and all getters must have * matching setters. The Java object is expected to have implemented a correct .equals() and * .hashCode methods The equals method must be completely determined by the schema fields. i.e. if * the object has hidden fields that are not reflected in the schema but are compared in equals, * then results will be incorrect. */ public <T> void registerJavaBean(Class<T> clazz) { registerJavaBean(TypeDescriptor.of(clazz)); }
/** Rerieve the function that converts an object of the specified type to a {@link Row} object. */ public <T> SerializableFunction<T, Row> getToRowFunction(Class<T> clazz) throws NoSuchSchemaException { return getToRowFunction(TypeDescriptor.of(clazz)); }
/** Retrieve the function that converts a {@link Row} object to the specified type. */ public <T> SerializableFunction<Row, T> getFromRowFunction(Class<T> clazz) throws NoSuchSchemaException { return getFromRowFunction(TypeDescriptor.of(clazz)); }