[英]How to deserialize JSON Array to Apache beam PCollection<javaObject>
I have data like我有这样的数据
[{"ProjectId":1476401625,"ProjectName":"This is project name","ProjectPostcode":4178},{"ProjectId":2343,"ProjectName":"This is project 2 name","ProjectPostcode":5323}]
I need to to deserialize it to Java object and I use this code:我需要将其反序列化为 Java object 并使用以下代码:
PCollection<Project> deserialisedProjectObject = projectFile.apply("Deserialize Projects", ParseJsons.of(Project.class))
.setCoder(SerializableCoder.of(Project.class));
but I always got error但我总是出错
Exception in thread "main" org.apache.beam.sdk.Pipeline$PipelineExecutionException: java.lang.RuntimeException: Failed to parse a com.lendlease.dp.entity.Project from JSON value: [{"ProjectId":1476401625,"ProjectName":"This is project name","ProjectPostcode":4178},{"ProjectId":2343,"ProjectName":"This is project 2 name","ProjectPostcode":5323}]
If I change the code to become:如果我将代码更改为:
PCollection<Project[]> deserialisedProjectObject = projectFile.apply("Deserialize Projects", ParseJsons.of(Project[].class))
.setCoder(SerializableCoder.of(Project[].class));
The runner able to deserialize it but I need this line to return a collection of Project;跑步者能够反序列化它,但我需要这一行来返回项目的集合; not collection of Project array
不是项目数组的集合
You are starting with a Project[] object, so the parse is correct.您从 Project[] object 开始,因此解析是正确的。 To extract the Project objects from that object, just apply a FlatMap transform after the ParseJson, outputting the elements within the Array.
要从 object 中提取项目对象,只需在 ParseJson 之后应用 FlatMap 转换,输出数组中的元素。
As well as ParseJson you may want to look at:除了 ParseJson,您可能还想看看:
The output of this is a Row object which you can use as a schema which provide a lot of nice functionality, see using schemas . output 是行 object,您可以将其用作提供许多不错功能的模式,请参阅使用模式。 If you need a an actual POJO within the pipeline as well as the Row object you can make use of Convert.fromRow to turn it into a Pojo object.
如果您需要管道中的实际 POJO 以及行 object,您可以使用Convert.fromRow将其转换为 Pojo object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.