You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/11/05 02:42:40 UTC

[GitHub] [flink] libenchao commented on a change in pull request #10060: [FLINK-14546] [flink-json] Support map type in flink-json

libenchao commented on a change in pull request #10060: [FLINK-14546] [flink-json] Support map type in flink-json
URL: https://github.com/apache/flink/pull/10060#discussion_r342361796
 
 

 ##########
 File path: flink-formats/flink-json/src/main/java/org/apache/flink/formats/json/JsonRowDeserializationSchema.java
 ##########
 @@ -242,11 +247,33 @@ private DeserializationRuntimeConverter wrapIntoNullableConverter(Deserializatio
 			return Optional.of(createObjectArrayConverter(((BasicArrayTypeInfo) typeInfo).getComponentInfo()));
 		} else if (isPrimitiveByteArray(typeInfo)) {
 			return Optional.of(createByteArrayConverter());
+		} else if (typeInfo instanceof MapTypeInfo) {
+			MapTypeInfo<?, ?> mapTypeInfo = (MapTypeInfo<?, ?>) typeInfo;
+			return Optional.of(createMapConverter(mapTypeInfo.getKeyTypeInfo(), mapTypeInfo.getValueTypeInfo()));
 		} else {
 			return Optional.empty();
 		}
 	}
 
+	private DeserializationRuntimeConverter createMapConverter(TypeInformation keyType, TypeInformation valueType) {
+		DeserializationRuntimeConverter valueConverter = createConverter(valueType);
+		DeserializationRuntimeConverter keyConverter = createConverter(keyType);
+
+		return (mapper, jsonNode) -> {
+			// ObjectNode stores Map<String, JsonNode> internally, so we can only get String keys.
+			Iterator<String> fieldNames = jsonNode.fieldNames();
+			Map<Object, Object> result = new HashMap<>();
+			while (fieldNames.hasNext()) {
+				String stringKey = fieldNames.next();
+				JsonNode keyNode = TextNode.valueOf(stringKey);
 
 Review comment:
   done.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services