[英]Beam TextIO write NullPointerException because destination is null
我正在使用Apache Beam DirectRunner,並且定義了以下管道:
val p = Pipeline.create(options)
p.apply(Create.of("/tmp/dc/foo.txt"))
.apply(FileLoader())
.apply(SaveLineToRedis())
.apply(AddToRedisIndex())
.apply(MatchTransform())
.apply(GroupByKey.create())
.apply(TextIO.writeCustomType<KV<String, Iterable<SimpleMatcherResult>>>().to("/tmp/bar"))
寫入失敗並顯示以下內容:
13:45:47.247 [direct-runner-worker] INFO org.apache.beam.sdk.io.WriteFiles - Opening writer 83c36e3f-7e1f-406c-a9c6-f3ab4bac1cb7 for window org.apache.beam.sdk.transforms.windowing.GlobalWindow@29caf222 pane PaneInfo{isFirst=true, isLast=true, timing=ON_TIME, index=0, onTimeIndex=0} destination null
Exception in thread "main" org.apache.beam.sdk.Pipeline$PipelineExecutionException: java.lang.NullPointerException
at org.apache.beam.runners.direct.DirectRunner$DirectPipelineResult.waitUntilFinish(DirectRunner.java:342)
at org.apache.beam.runners.direct.DirectRunner$DirectPipelineResult.waitUntilFinish(DirectRunner.java:312)
at org.apache.beam.runners.direct.DirectRunner.run(DirectRunner.java:206)
at org.apache.beam.runners.direct.DirectRunner.run(DirectRunner.java:62)
at org.apache.beam.sdk.Pipeline.run(Pipeline.java:311)
at de.techmatrix.dc.matcher.MainKt.main(Main.kt:12)
Caused by: java.lang.NullPointerException
at org.apache.beam.sdk.io.DynamicFileDestinations$ConstantFilenamePolicy.formatRecord(DynamicFileDestinations.java:49)
at org.apache.beam.sdk.io.WriteFiles$WriteShardsIntoTempFilesFn.processElement(WriteFiles.java:718)
DynamicFileDestinations
返回的目標為null
:
public Void getDestination(UserT element) {
return (Void) null;
}
更新:這適用於FileIO
:
.apply(FileIO.writeDynamic<String, KV<String, Iterable<SimpleMatcherResult>>>()
.by { it.value.first().matchedKey }.withDestinationCoder(StringUtf8Coder.of())
.via(Contextful.fn({ mapper.writeValueAsString(it) }), TextIO.sink())
.to("/tmp/bar")
.withNaming{ _ -> defaultNaming("matches", "txt")})
有人可以解釋為什么嗎?
使用TextIO.writeCustomType()
隱式使用DynamicDestinations
。 調用TextIO.writeCustomType().to("/tmp/bar")
只是設置要寫入文件的文件名前綴。 請參見TextIO.TypedWrite.to()定義。 您實際上是否需要寫一個動態目標?
您只需使用TextIO.Write
轉換即可寫入文本文件。 有關參考和示例,請參見TextIO定義。 您將需要一個額外的轉換步驟,以將KV<String, Iterable<SimpleMatcherResult>>
轉換為String
類型。
在第一個代碼段中,您未指定withFormatFunction()
(Beam無法對此進行驗證並給出更好的錯誤消息)。 NPE 從這一行開始調用(缺少)格式功能。
在第二個代碼段中,它指定為Contextful.fn({ mapper.writeValueAsString(it) })
-因此可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.