[英]How to use ParseJsons in Apache Beam / Google Dataflow?
java新手在這里。 我正在努力理解如何在我的 Apache Beam 管道中使用ParseJsons將字符串 PCollection 解析為對象 PCollection。
我的理解是需要先定義一個匹配json結構的類,然后使用ParseJsons將json字符串映射到那個類的對象中。
然而,ParseJsons 文檔對我來說看起來很神秘。 我不確定如何使用 Apache Beam 實際執行轉換。 有人能給我一個關於如何解析行分隔的 json 字符串的快速而骯臟的例子嗎?
這是我所做的嘗試之一,但不幸的是語法不正確。
class Product {
private String name = null;
private String url = null;
}
p.apply("ReadLines", TextIO.read().from(options.getInputFile()))
.apply(new ParseJsons.of(Product))
.apply("WriteCounts", TextIO.write().to(options.getOutput()));
我想你想要:
PCollectoion<Product> =
p.apply("ReadLines", TextIO.read().from(options.getInputFile()))
.apply(new ParseJsons.of(Product.class))
.setCoder(SerializableCoder.of(MyPojo.class));
ParseJsons.of
方法是靜態的。 所以你可以在不實例化類的情況下調用它。 但是,您需要將結果轉換回字符串。 例子:
PCollection<MyPojo> =
p.apply("ReadLines", TextIO.read().from(options.getInputFile()))
.apply("Parse JSON", ParseJsons.of(MyPojo.class))
.apply("Convert back to String", ParDo.of(new FormatPojoFn()))
.apply("WriteCounts", TextIO.write().to(options.getOutput()));
您還可以嘗試在TextIO 類上使用writeCustomType
方法:
p.apply(TextIO.<UserEvent>writeCustomType(new FormatEvent()).to(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.