Google cloud dataflow GoogleDataflow:根据条件只将消息输出到PubSub主题之一

Google cloud dataflow GoogleDataflow:根据条件只将消息输出到PubSub主题之一,google-cloud-dataflow,apache-beam,google-cloud-pubsub,Google Cloud Dataflow,Apache Beam,Google Cloud Pubsub,在我的管道中,我希望根据先前转换的结果将消息输出到PubSub主题之一。目前,我正在向同一主题发送输出: SearchItemGeneratorOptions=PipelineOptions工厂.fromArgs(args).withValidation().as(SearchItemGeneratorOptions.class); Pipeline p=Pipeline.create(选项); p、 应用(…) //其他转换 .apply(“ParseFile”,new ParseFile()

在我的管道中,我希望根据先前转换的结果将消息输出到PubSub主题之一。目前,我正在向同一主题发送输出:

SearchItemGeneratorOptions=PipelineOptions工厂.fromArgs(args).withValidation().as(SearchItemGeneratorOptions.class);
Pipeline p=Pipeline.create(选项);
p、 应用(…)
//其他转换
.apply(“ParseFile”,new ParseFile())//输出PCollection,其中每条消息都有一个带有主题名称的MessageType属性。
.apply(“WriteItemsToTopic”,publisubio.writeStrings().to(options.getOutputTopic());
这是我的消息对象:

类消息{
私有消息类型消息类型;
专用字符串有效载荷;
//构造函数,getter
}
我的ParseFile transformer输出PCollection,每个消息对象都有一个属性messageType。基于我想要输出到消息的不同pubsubtopics有效负载属性的messageType属性。我在文章段落中读到“多重转换”处理相同的PCollection,但仍然不知道如何在我的案例中应用它或其他解决方案

更新 感谢@Andrew为您提供的解决方案。 我使用TupleTag解决了我的问题,但方法类似。 我在主管道中创建了两个不同的TupleTag对象:

public static final TupleTag<String> full = new TupleTag<>("full");
public static final TupleTag<String> delta = new TupleTag<>("delta");
并在主管道中从PCollectionTuple中选择每个TupleTag发送到发布/订阅主题

messages.get(full)
            .apply("SendToIndexTopic", PubsubIO.writeStrings().to(options.getOutputIndexTopic()));

messages.get(delta)
            .apply("SendToDeltaTopic", PubsubIO.writeStrings().to(options.getOutputDeltaTopic()));

唯一需要提及的是,我的TupleTag对象是静态对象。

您可以对管道进行分区,将消息发布到多个发布/订阅主题。分区将允许您分离消息,而不是将它们复制到不同的发布/订阅主题。你需要提前知道所有的酒吧/酒吧主题。参考文献:

例如:

//分区管道
PCollectionList msgs=p.apply(Partition.of(2,new PartitionFn()){
public int partitionFor(消息消息消息,int numPartitions){
//TODO:确定如何对消息进行分区
如果(msg.messageType==“x”){
返回0;
}否则{
返回1;
}
}
}));
//访问分区
PCollection partition1=msgs.get(0);
partition1.apply(“WriteItemsToTopic1”,publisubio.writeStrings().to(options.getOutputOpic1());
PCollection partition2=msgs.get(1);
partition2.apply(“WriteItemsToTopic2”,publisubio.writeStrings().to(options.getOutputOpic2());

您可以对管道进行分区,将消息发布到多个发布/订阅主题。分区将允许您分离消息,而不是将它们复制到不同的发布/订阅主题。你需要提前知道所有的酒吧/酒吧主题。参考文献:

例如:

//分区管道
PCollectionList msgs=p.apply(Partition.of(2,new PartitionFn()){
public int partitionFor(消息消息消息,int numPartitions){
//TODO:确定如何对消息进行分区
如果(msg.messageType==“x”){
返回0;
}否则{
返回1;
}
}
}));
//访问分区
PCollection partition1=msgs.get(0);
partition1.apply(“WriteItemsToTopic1”,publisubio.writeStrings().to(options.getOutputOpic1());
PCollection partition2=msgs.get(1);
partition2.apply(“WriteItemsToTopic2”,publisubio.writeStrings().to(options.getOutputOpic2());
messages.get(full)
            .apply("SendToIndexTopic", PubsubIO.writeStrings().to(options.getOutputIndexTopic()));

messages.get(delta)
            .apply("SendToDeltaTopic", PubsubIO.writeStrings().to(options.getOutputDeltaTopic()));