【问题标题】:Passing Side Input in PCollection Partition在 PCollection 分区中传递边输入
【发布时间】:2017-11-15 17:31:51
【问题描述】:

我想在 PCollection 分区中传递一个 sideInput,在此基础上,我需要划分我的 PCollection 是他们的无论如何......

PCollectionList<TableRow> part = merged.apply(Partition.of(Pcollection Count Function Called, new PartitionFn<TableRow>(){

                                        @Override
                                        public int partitionFor(TableRow arg0, int arg1) {

                                            return 0;
                                        }

                                    }));

我可以通过任何其他方式对我的 PCollection 进行分区

//不使用动态目标分区 BigQuery 表

merge.apply("write into target", BigQueryIO.writeTableRows()
                                         .to(new SerializableFunction<ValueInSingleWindow<TableRow>, TableDestination>() {
                                                @Override
                                                public TableDestination apply(ValueInSingleWindow<TableRow> value) {
                                                       TableRow row = value.getValue();
                                                       TableReference reference = new TableReference();
                                                       reference.setProjectId("XYZ");
                                                       reference.setDatasetId("ABC");
                                                       System.out.println("date of row " + row.get("authorized_transaction_date_yyyymmdd").toString());               
                                                       LOG.info("date of row "+
                                                       row.get("authorized_transaction_date_yyyymmdd").toString());
                                                       String str = row.get("authorized_transaction_date_yyyymmdd").toString();
                                                       str = str.substring(0, str.length() - 2) + "01";
                                                       System.out.println("str value " + str);
                                                       LOG.info("str value " + str);
                                                       reference.setTableId("TargetTable$" + str);
                                                       return new TableDestination(reference, null);
                                                }
                                         }).withFormatFunction(new SerializableFunction<TableRow, TableRow>() {
                                                @Override
                                                public TableRow apply(TableRow input) {
                                                       LOG.info("format function:"+input.toString());

                                                       return input;
                                                }
                                         })

                                         .withSchema(schema1).withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_TRUNCATE)
                                         .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED));

现在我必须使用 Dynamic Destination Any Solution.Insted 这个并且必须做分区。

【问题讨论】:

    标签: google-cloud-platform google-cloud-dataflow apache-beam


    【解决方案1】:

    根据您在代码中看到的TableRow,我怀疑您想将PCollection 写入BigQuery,将不同​​的元素发送到不同的BigQuery 表。 BigQueryIO.write() 已经提供了一种方法来做到这一点,使用 BigQueryIO.write().to(DynamicDestinations)。见Writing different values to different BigQuery tables in Apache Beam

    【讨论】:

    • 谢谢,@jkff 但在我的情况下,我在表中应用分区功能,因为我必须根据我以 View 形式保存的不同日期划分我的 PCollection 然后我有在 DynamicDestination 中传递不同的 PCollection。因为我可以在动态目标中访问 ListView,所以我可以根据该日期对 BigQuery 表进行分区。示例-:reference.setTableId("TargetTable$" + str); return new TableDestination(reference, null);
    • 动态目标中的方法是: TableDestination 我需要在引用中传递该查看日期并将其设置为 return new TableDestination(reference, null);但为此,我需要知道我的 PCollection 仅基于该日期进行分区,这就是为什么我不能使用 sideOutputs 来划分我的 PCollection 也是因为我们的划分计数也是动态的。使用 For 循环,我将从 DisctDatesView 中一一提取所有 Pcollection 和不同日期并设置在表中
    • BigQueryIO.write().to(DynamicDestinations) 也可用于编写分区表(带有“$”的表),我认为这是您想要的。您既不需要侧面输入,也不需要侧面输出,也不需要 Partition 转换来完成此操作。 write().to(DynamicDestinations) 的单个应用程序将完成所有工作。抱歉,我很难理解你们其他的 cmets。
    • 对不起@Jkff 在我的情况下,BigQuery 提供我们根据日期进行分区,但我必须根据月份对 BigQuery 表进行分区,因为我没有使用 Dynamic目的地代码的某些部分可在-:docs.google.com/document/d/…
    • 我已经更新了问题并添加了代码部分,请查看它,并且场景是这样的,我必须根据月份对 BigQuery 表进行分区,因为在上面的代码中,我已将日期天数替换为 01。如果所有日期在数据分区中都是通用的,则分区以月的形式发生。
    猜你喜欢
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多