簡體   English   中英

如何向 Google Dataflow PubSubIO Write 添加屬性

[英]How to add attributes to Google Dataflow PubSubIO Write

我有一個從 Pubsub 主題訂閱的場景,讀取有效負載並將其發布回另一個具有基於有效負載的附加屬性的 PubSub 主題。 找不到用於填充自定義屬性的任何示例。 有人可以幫助選擇選項或樣品。

pipeline.apply("讀取 PubSub 事件", PubsubIO.readMessagesWithAttributes().fromSubscription(options.getInputSubscription())).apply("寫入 PubSub 事件", PubsubIO.writeMessages().to(options.getOutputTopic()));

提前致謝

給你!

    final List<String> elements = Arrays.asList(
            //Name, Product,Epoch time millis
            "Robert, TV, 1613141590000",
            "Maria, Phone, 1612718280000",
            "Juan, Laptop, 1611618000000",
            "Rebeca, Videogame, 1610000000000"
    );

    p
            .apply(Create.of(elements))
            .apply("to PubSubMessage", ParDo.of(new DoFn<String, PubsubMessage>() {
                @ProcessElement
                public void processElement(ProcessContext c) {
                    String[] columns = c.element().split(", ");

                    HashMap<String, String> attributes = new HashMap<String, String>();
                    attributes.put("timestamp", columns[2]);
                    attributes.put("buyer", columns[0]);

                    PubsubMessage message = new PubsubMessage(c.element().getBytes(StandardCharsets.UTF_8), attributes);

                    c.output(message);
                }
            }))
            .apply(PubsubIO.writeMessages().to(options.getTopic()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM