![](/img/trans.png)
[英]Apache Beam Session Windowing and joining across PCollections
[英]Trouble emitting elements to multiple output PCollections in Apache Beam
我在 Apache Beam 中使用 MultiOutputReciver 到帶有 TupleTag 的 PCollectionTuple 時遇到了麻煩。
logSchema 是我的 AvroGenerated class 用於處理傳入日志。 日期、類型、消息等。我想要做的是將不同類型的日志(錯誤、警告、通知)存儲在不同的 PCollections 中。
我收到此錯誤java: incompatible types: logSchema cannot be converted to capture#1 of?
對於每個out.get(tags.get(0)).output(log);
內部processElement
內部branching extends DoFn<logSchema, logSchema>
基本Required type: capture of? Provided: logSchema
Required type: capture of? Provided: logSchema
我主要遵循Beam 編程指南,涵蓋額外的輸出以及我可以在此處找到的其他示例。
有人願意解釋我做錯了什么嗎? 我感到迷失,但也很接近。
編輯:好像我忘記了分支 ParDo 上的 .withOutputTags() 。 將此添加到下面的代碼中,仍然得到相同的不兼容類型錯誤。 IDEA 紅線它(之前也做過)並想要轉換為(PCollectionTuple),為什么這是必要的?
這是我的代碼
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.io.TextIO;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionTuple;
import org.apache.beam.sdk.values.TupleTag;
import org.apache.beam.sdk.values.TupleTagList;
import java.text.ParseException;
public class Pipe extends Thread {
public Pipe() {
}
static class conformToSchema extends DoFn<String, logSchema> {
@ProcessElement
public void processElement(@Element String element, OutputReceiver<logSchema> receiver ) throws ParseException {
logSchema log = new logSchema(element);
receiver.output(log);
}
}
static class branching extends DoFn<logSchema, logSchema> {
private TupleTagList tags;
public branching(TupleTagList tags) {
this.tags = tags;
}
@ProcessElement
public void processElement(@Element logSchema log, MultiOutputReceiver out ) {
if (log.getType().equals("[notice]")) out.get(tags.get(0)).output(log);
else if (log.getType().equals("[error]")) out.get(tags.get(1)).output(log);
else if (log.getType().equals("[warn]")) out.get(tags.get(2)).output(log);
else if (log.getType().equals("[sout]") ) out.get(tags.get(3)).output(log);
}
}
public void run(){
TupleTag<logSchema> all = new TupleTag<>();
TupleTag<logSchema> noticesTag = new TupleTag<>();
TupleTag<logSchema> errorsTag = new TupleTag<>();
TupleTag<logSchema> warningsTag = new TupleTag<>();
TupleTag<logSchema> soutTag = new TupleTag<>();
TupleTagList tags = TupleTagList.of(noticesTag).and(errorsTag).and(warningsTag).and(soutTag);
PipelineOptions options = PipelineOptionsFactory.create();
Pipeline p = Pipeline.create();
PCollection<String> input = p.apply(TextIO.read().from("C:\...."));
PCollection logObjects = input
.apply("Conform", ParDo.of(
new conformToSchema()));
PCollectionTuple multipleOutputs = (PCollectionTuple) logObjects.apply("Branch", ParDo.of(new branching(tags)).withOutputTags(all, tags));
PCollection<logSchema> notices = multipleOutputs.get(noticesTag);
PCollection<logSchema> errors = multipleOutputs.get(errorsTag);
PCollection<logSchema> warning = multipleOutputs.get(warningsTag);
PCollection<logSchema> sout = multipleOutputs.get(soutTag);
try {
p.run().waitUntilFinish();
} catch (Exception e) {
e.printStackTrace();
}
}
}
順便說一句,這是第一次在這里發帖,是的。
您的 TupleTags 沒有類型參數是否有原因?
TupleTag<logSchema> all = new TupleTag<>();
該示例顯示了一個類型參數和空白實現。
TupleTag<logSchema> all = new TupleTag<logSchema>(){};
無關樣式 nit: Class 名稱應在 Java 中大寫,使您的代碼更具可讀性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.