/** 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)); }
@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 testRecursiveArrayGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SchemaTestUtils.assertSchemaEquivalent( NESTED_ARRAY_POJO_SCHEMA, registry.getSchema(NestedArrayPOJO.class)); SimplePOJO simple1 = createSimple("string1"); SimplePOJO simple2 = createSimple("string2"); SimplePOJO simple3 = createSimple("string3"); NestedArrayPOJO pojo = new NestedArrayPOJO(simple1, simple2, simple3); Row row = registry.getToRowFunction(NestedArrayPOJO.class).apply(pojo); List<Row> rows = row.getArray("pojos"); assertSame(simple1, registry.getFromRowFunction(NestedArrayPOJO.class).apply(rows.get(0))); assertSame(simple2, registry.getFromRowFunction(NestedArrayPOJO.class).apply(rows.get(1))); assertSame(simple3, registry.getFromRowFunction(NestedArrayPOJO.class).apply(rows.get(2))); }
@Test public void testMapFieldSetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row1 = createSimpleRow("string1"); Row row2 = createSimpleRow("string2"); Row row3 = createSimpleRow("string3"); Row row = Row.withSchema(NESTED_MAP_POJO_SCHEMA) .addValue( ImmutableMap.of( "simple1", row1, "simple2", row2, "simple3", row3)) .build(); NestedMapPOJO pojo = registry.getFromRowFunction(NestedMapPOJO.class).apply(row); assertEquals(3, pojo.map.size()); assertEquals("string1", pojo.map.get("simple1").str); assertEquals("string2", pojo.map.get("simple2").str); assertEquals("string3", pojo.map.get("simple3").str); }
@Test public void testFromRowWithGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SimpleBean bean = createSimple("string"); Row row = registry.getToRowFunction(SimpleBean.class).apply(bean); // Test that the fromRowFunction simply returns the original object back. SimpleBean extracted = registry.getFromRowFunction(SimpleBean.class).apply(row); assertSame(bean, extracted); }
@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 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 testNullValuesSetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row = Row.withSchema(NULLABLES_SCHEMA).addValues(null, 42).build(); POJOWithNullables pojo = registry.getFromRowFunction(POJOWithNullables.class).apply(row); assertNull(pojo.str); assertEquals(42, pojo.anInt); }
@Test public void testMapFieldSetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row1 = createSimpleRow("string1"); Row row2 = createSimpleRow("string2"); Row row3 = createSimpleRow("string3"); Row row = Row.withSchema(NESTED_MAP_BEAN_SCHEMA) .addValue( ImmutableMap.of( "simple1", row1, "simple2", row2, "simple3", row3)) .build(); NestedMapBean bean = registry.getFromRowFunction(NestedMapBean.class).apply(row); assertEquals(3, bean.getMap().size()); assertEquals("string1", bean.getMap().get("simple1").getStr()); assertEquals("string2", bean.getMap().get("simple2").getStr()); assertEquals("string3", bean.getMap().get("simple3").getStr()); } }
@Test public void testNestedArraysSetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); List<List<String>> listOfLists = Lists.newArrayList( Lists.newArrayList("a", "b", "c"), Lists.newArrayList("d", "e", "f"), Lists.newArrayList("g", "h", "i")); Row row = Row.withSchema(NESTED_ARRAYS_POJO_SCHEMA).addArray(listOfLists).build(); NestedArraysPOJO pojo = registry.getFromRowFunction(NestedArraysPOJO.class).apply(row); assertEquals(listOfLists, pojo.lists); }
@Test public void testPrimitiveArraySetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row = Row.withSchema(PRIMITIVE_ARRAY_POJO_SCHEMA) .addArray("a", "b", "c", "d") .addArray(1, 2, 3, 4) .addArray(42L, 43L, 44L, 45L) .build(); PrimitiveArrayPOJO pojo = registry.getFromRowFunction(PrimitiveArrayPOJO.class).apply(row); assertEquals(row.getArray("strings"), pojo.strings); assertEquals(row.getArray("integers"), Ints.asList(pojo.integers)); assertEquals(row.getArray("longs"), Arrays.asList(pojo.longs)); }
@Test public void testNestedArraysSetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); List<List<String>> listOfLists = Lists.newArrayList( Lists.newArrayList("a", "b", "c"), Lists.newArrayList("d", "e", "f"), Lists.newArrayList("g", "h", "i")); Row row = Row.withSchema(NESTED_ARRAYS_BEAM_SCHEMA).addArray(listOfLists).build(); NestedArraysBean bean = registry.getFromRowFunction(NestedArraysBean.class).apply(row); assertEquals(listOfLists, bean.getLists()); }
@Test public void testPrimitiveArraySetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row = Row.withSchema(PRIMITIVE_ARRAY_BEAN_SCHEMA) .addArray("a", "b", "c", "d") .addArray(1, 2, 3, 4) .addArray(42L, 43L, 44L, 45L) .build(); PrimitiveArrayBean bean = registry.getFromRowFunction(PrimitiveArrayBean.class).apply(row); assertEquals(row.getArray("strings"), bean.getStrings()); assertEquals(row.getArray("integers"), Ints.asList(bean.getIntegers())); assertEquals(row.getArray("longs"), Arrays.asList(bean.getLongs())); }
@Test public void testFromRow() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row = createSimpleRow("string"); SimplePOJO pojo = registry.getFromRowFunction(SimplePOJO.class).apply(row); assertEquals("string", pojo.str); assertEquals((byte) 1, pojo.aByte); assertEquals((short) 2, pojo.aShort); assertEquals((int) 3, pojo.anInt); assertEquals((long) 4, pojo.aLong); assertEquals(true, pojo.aBoolean); assertEquals(DATE, pojo.dateTime); assertEquals(INSTANT, pojo.instant); assertArrayEquals("not equal", BYTE_ARRAY, pojo.bytes); assertEquals(BYTE_BUFFER, pojo.byteBuffer); assertEquals(BigDecimal.ONE, pojo.bigDecimal); assertEquals("stringbuilder", pojo.stringBuilder.toString()); }
@Test public void testRecursiveArraySetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row1 = createSimpleRow("string1"); Row row2 = createSimpleRow("string2"); Row row3 = createSimpleRow("string3"); ; Row row = Row.withSchema(NESTED_ARRAY_POJO_SCHEMA).addArray(row1, row2, row3).build(); NestedArrayPOJO pojo = registry.getFromRowFunction(NestedArrayPOJO.class).apply(row); assertEquals(3, pojo.pojos.length); assertEquals("string1", pojo.pojos[0].str); assertEquals("string2", pojo.pojos[1].str); assertEquals("string3", pojo.pojos[2].str); }
@Test public void testRecursiveSetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row nestedRow = createSimpleRow("string"); Row row = Row.withSchema(NESTED_POJO_SCHEMA).addValue(nestedRow).build(); NestedPOJO pojo = registry.getFromRowFunction(NestedPOJO.class).apply(row); assertEquals("string", pojo.nested.str); assertEquals((byte) 1, pojo.nested.aByte); assertEquals((short) 2, pojo.nested.aShort); assertEquals((int) 3, pojo.nested.anInt); assertEquals((long) 4, pojo.nested.aLong); assertEquals(true, pojo.nested.aBoolean); assertEquals(DATE, pojo.nested.dateTime); assertEquals(INSTANT, pojo.nested.instant); assertArrayEquals("not equal", BYTE_ARRAY, pojo.nested.bytes); assertEquals(BYTE_BUFFER, pojo.nested.byteBuffer); assertEquals(BigDecimal.ONE, pojo.nested.bigDecimal); assertEquals("stringbuilder", pojo.nested.stringBuilder.toString()); }
@Override public PCollection<OutputT> expand(PCollection<? extends InputT> input) { SchemaRegistry schemaRegistry = input.getPipeline().getSchemaRegistry(); CoderRegistry registry = input.getPipeline().getCoderRegistry(); finishSpecifyingStateSpecs(fn, registry, input.getCoder()); TupleTag<OutputT> mainOutput = new TupleTag<>(MAIN_OUTPUT_TAG); PCollection<OutputT> res = input.apply(withOutputTags(mainOutput, TupleTagList.empty())).get(mainOutput); try { res.setSchema( schemaRegistry.getSchema(getFn().getOutputTypeDescriptor()), schemaRegistry.getToRowFunction(getFn().getOutputTypeDescriptor()), schemaRegistry.getFromRowFunction(getFn().getOutputTypeDescriptor())); } catch (NoSuchSchemaException e) { try { res.setCoder( registry.getCoder( getFn().getOutputTypeDescriptor(), getFn().getInputTypeDescriptor(), ((PCollection<InputT>) input).getCoder())); } catch (CannotProvideCoderException e2) { // Ignore and leave coder unset. } } return res; }
@Test public void testFromRow() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row = createSimpleRow("string"); SimpleBean bean = registry.getFromRowFunction(SimpleBean.class).apply(row); assertEquals("string", bean.getStr()); assertEquals((byte) 1, bean.getaByte()); assertEquals((short) 2, bean.getaShort()); assertEquals((int) 3, bean.getAnInt()); assertEquals((long) 4, bean.getaLong()); assertEquals(true, bean.isaBoolean()); assertEquals(DATE, bean.getDateTime()); assertEquals(DATE.toInstant(), bean.getInstant()); assertArrayEquals("not equal", BYTE_ARRAY, bean.getBytes()); assertArrayEquals("not equal", BYTE_ARRAY, bean.getByteBuffer().array()); assertEquals(BigDecimal.ONE, bean.getBigDecimal()); assertEquals("stringbuilder", bean.getStringBuilder().toString()); }
@Test public void testRecursiveArraySetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row1 = createSimpleRow("string1"); Row row2 = createSimpleRow("string2"); Row row3 = createSimpleRow("string3"); ; Row row = Row.withSchema(NESTED_ARRAY_BEAN_SCHEMA).addArray(row1, row2, row3).build(); NestedArrayBean bean = registry.getFromRowFunction(NestedArrayBean.class).apply(row); assertEquals(3, bean.getBeans().length); assertEquals("string1", bean.getBeans()[0].getStr()); assertEquals("string2", bean.getBeans()[1].getStr()); assertEquals("string3", bean.getBeans()[2].getStr()); }
@Test public void testRecursiveSetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row nestedRow = createSimpleRow("string"); Row row = Row.withSchema(NESTED_BEAN_SCHEMA).addValue(nestedRow).build(); NestedBean bean = registry.getFromRowFunction(NestedBean.class).apply(row); assertEquals("string", bean.getNested().getStr()); assertEquals((byte) 1, bean.getNested().getaByte()); assertEquals((short) 2, bean.getNested().getaShort()); assertEquals((int) 3, bean.getNested().getAnInt()); assertEquals((long) 4, bean.getNested().getaLong()); assertEquals(true, bean.getNested().isaBoolean()); assertEquals(DATE, bean.getNested().getDateTime()); assertEquals(DATE.toInstant(), bean.getNested().getInstant()); assertArrayEquals("not equal", BYTE_ARRAY, bean.getNested().getBytes()); assertArrayEquals("not equal", BYTE_ARRAY, bean.getNested().getByteBuffer().array()); assertEquals(BigDecimal.ONE, bean.getNested().getBigDecimal()); assertEquals("stringbuilder", bean.getNested().getStringBuilder().toString()); }