/** * Get a MAP value by field name, {@link IllegalStateException} is thrown if schema doesn't match. */ @Nullable public <T1, T2> Map<T1, T2> getMap(String fieldName) { return getMap(getSchema().indexOf(fieldName)); }
@Test public void testCreateMapWithNullValue() { Map<Integer, String> data = new HashMap(); data.put(1, "value1"); data.put(2, "value2"); data.put(3, null); data.put(4, null); Schema type = Stream.of(Schema.Field.of("map", FieldType.map(FieldType.INT32, FieldType.STRING, true))) .collect(toSchema()); Row row = Row.withSchema(type).addValue(data).build(); assertEquals(data, row.getMap("map")); }
@Test public void testCreateMapWithMapValueWithNull() { Map<Integer, Map<Integer, String>> data = new HashMap(); Map<Integer, String> innerData = new HashMap(); innerData.put(11, null); innerData.put(12, "value3"); data.put(1, ImmutableMap.of(1, "value1")); data.put(2, ImmutableMap.of(2, "value2")); data.put(3, null); data.put(4, innerData); Schema type = Stream.of( Schema.Field.of( "map", FieldType.map( FieldType.INT32, FieldType.map(FieldType.INT32, FieldType.STRING, true), true))) .collect(toSchema()); Row row = Row.withSchema(type).addValue(data).build(); assertEquals(data, row.getMap("map")); }
@Test public void testCreateMapWithArrayValue() { Map<Integer, List<String>> data = ImmutableMap.<Integer, List<String>>builder() .put(1, Arrays.asList("value1")) .put(2, Arrays.asList("value2")) .build(); Schema type = Stream.of( Schema.Field.of( "map", FieldType.map(FieldType.INT32, FieldType.array(FieldType.STRING)))) .collect(toSchema()); Row row = Row.withSchema(type).addValue(data).build(); assertEquals(data, row.getMap("map")); }
@Test public void testCreateMapWithRowValue() { Schema nestedType = Stream.of(Schema.Field.of("f1_str", FieldType.STRING)).collect(toSchema()); Map<Integer, Row> data = ImmutableMap.<Integer, Row>builder() .put(1, Row.withSchema(nestedType).addValues("one").build()) .put(2, Row.withSchema(nestedType).addValues("two").build()) .build(); Schema type = Stream.of(Schema.Field.of("map", FieldType.map(FieldType.INT32, FieldType.row(nestedType)))) .collect(toSchema()); Row row = Row.withSchema(type).addValue(data).build(); assertEquals(data, row.getMap("map")); }
@Test public void testMapFieldGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SchemaTestUtils.assertSchemaEquivalent( NESTED_MAP_BEAN_SCHEMA, registry.getSchema(NestedMapBean.class)); SimpleBean simple1 = createSimple("string1"); SimpleBean simple2 = createSimple("string2"); SimpleBean simple3 = createSimple("string3"); NestedMapBean bean = new NestedMapBean( ImmutableMap.of( "simple1", simple1, "simple2", simple2, "simple3", simple3)); Row row = registry.getToRowFunction(NestedMapBean.class).apply(bean); Map<String, Row> extractedMap = row.getMap("map"); assertEquals(3, extractedMap.size()); assertEquals("string1", extractedMap.get("simple1").getString("str")); assertEquals("string2", extractedMap.get("simple2").getString("str")); assertEquals("string3", extractedMap.get("simple3").getString("str")); }
@Test public void testMapFieldGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SchemaTestUtils.assertSchemaEquivalent( NESTED_MAP_POJO_SCHEMA, registry.getSchema(NestedMapPOJO.class)); SimplePOJO simple1 = createSimple("string1"); SimplePOJO simple2 = createSimple("string2"); SimplePOJO simple3 = createSimple("string3"); NestedMapPOJO pojo = new NestedMapPOJO( ImmutableMap.of( "simple1", simple1, "simple2", simple2, "simple3", simple3)); Row row = registry.getToRowFunction(NestedMapPOJO.class).apply(pojo); Map<String, Row> extractedMap = row.getMap("map"); assertEquals(3, extractedMap.size()); assertEquals("string1", extractedMap.get("simple1").getString("str")); assertEquals("string2", extractedMap.get("simple2").getString("str")); assertEquals("string3", extractedMap.get("simple3").getString("str")); }
@Test public void testCreateMapWithPrimitiveValue() { Map<Integer, String> data = ImmutableMap.<Integer, String>builder() .put(1, "value1") .put(2, "value2") .put(3, "value3") .put(4, "value4") .build(); Schema type = Stream.of(Schema.Field.of("map", FieldType.map(FieldType.INT32, FieldType.STRING))) .collect(toSchema()); Row row = Row.withSchema(type).addValue(data).build(); assertEquals(data, row.getMap("map")); }
@Test public void testCreateMapWithMapValue() { Map<Integer, Map<Integer, String>> data = ImmutableMap.<Integer, Map<Integer, String>>builder() .put(1, ImmutableMap.of(1, "value1")) .put(2, ImmutableMap.of(2, "value2")) .build(); Schema type = Stream.of( Schema.Field.of( "map", FieldType.map( FieldType.INT32, FieldType.map(FieldType.INT32, FieldType.STRING)))) .collect(toSchema()); Row row = Row.withSchema(type).addValue(data).build(); assertEquals(data, row.getMap("map")); }