简体   繁体   English

如何将 JSON Array 反序列化为 Apache beam PCollection<javaobject></javaobject>

[英]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,您可能还想看看:

JsonToRow JsonToRow

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.

相关问题 如何转换 PCollection<tablerow> 到个人收藏<row>在 Apache 梁?</row></tablerow> - How to convert PCollection<TableRow> to PCollection<Row> in Apache Beam? 如何区分两个 PCollection Apache Beam - How to diff two PCollection Apache Beam 如何在 PCollection 中组合数据 - Apache Beam - How to combine Data in PCollection - Apache beam 如何转换 PCollection<row> 在数据流 Apache 中使用 Java 束</row> - How to convert PCollection<Row> to Long in Dataflow Apache beam using Java 如何为 PCollection 设置编码器<List<String> &gt; 在 Apache Beam 中? - How do I set the coder for a PCollection<List<String>> in Apache Beam? 如何使用 Apache Beam 中的流输入 PCollection 请求 Redis 服务器? - How to request Redis server using a streaming input PCollection in Apache Beam? 如何从 PCollection 中提取信息<row>加入 apache 光束后?</row> - How to extract information from PCollection<Row> after a join in apache beam? 如何转换 PCollection<row> 使用 Java 到数据流 Apache 中的 Integer</row> - How to convert PCollection<Row> to Integer in Dataflow Apache beam using Java Apache Beam:扁平化 PCollection <List<Foo> &gt; 到 PCollection<Foo> - Apache Beam: Flattening PCollection<List<Foo>> to PCollection<Foo> Apache Beam - 使用无界PCollection进行集成测试 - Apache Beam - Integration test with unbounded PCollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM