代码之家  ›  专栏  ›  技术社区  ›  HendPro12

apachebeam/googledataflow PubSub到BigQuery管道:处理插入错误和意外的重试行为

  •  3
  • HendPro12  · 技术社区  · 7 年前

    我已经取下一份 Pub/Sub to BigQuery Dataflow template Google's github repository . 我正在本地机器上用 direct-runner

    在测试中,我确认,如果在UDF处理或从JSON到TableRow的转换过程中发生错误,模板只会将失败写入“deadletter”表。

    我还希望通过将它们发送到一个单独的TupleTag中来更优雅地处理在插入BigQuery时发生的失败,这样它们也可以被发送到deadletter表或另一个输出中进行检查和处理。当前,使用 dataflow-runner 这些错误只会写入Stackdriver日志,并且会继续无限期地重试,直到问题得到解决。

    问题一 :在本地测试并发布格式与目标表架构不匹配的消息时,将重试插入5次,然后管道崩溃,并出现RuntimeException以及从HTTP响应返回到Google API的错误。我相信这种行为是在 BigQueryServices.Impl 在这里:

    private static final FluentBackoff INSERT_BACKOFF_FACTORY =
            FluentBackoff.DEFAULT.withInitialBackoff(Duration.millis(200)).withMaxRetries(5);
    

    Google's documentation ,

    在流模式下运行时,包含失败项的包 永久失速。”

    作为梁的 Pub/Sub.IO

    创建和使用无界PCollections

    在我的印象中,当从Pub/Sub读取时,流模式应该在默认情况下启用。我甚至在调用writeTableRows()时添加了streaming\u Inserts方法,但它并没有影响这种行为。

    .apply(
                "WriteSuccessfulRecords",      
                BigQueryIO.writeTableRows()
                    .withMethod(Method.STREAMING_INSERTS)
    
    1. 使用?如果没有,我理解上的缺陷在哪里?

    问题二 :

    1. BigQueryIO.write BigQueryIO.writeTableRows ?

    JavascriptTextTransformer 用于FailsafeJavascriptUdf。

    :

    public static PipelineResult run(DirectOptions options) {
    
    options.setRunner(DirectRunner.class);
    
        Pipeline pipeline = Pipeline.create(options);
    
        // Register the coder for pipeline
        FailsafeElementCoder<PubsubMessage, String> coder =
            FailsafeElementCoder.of(PubsubMessageWithAttributesCoder.of(), StringUtf8Coder.of());
    
        CoderRegistry coderRegistry = pipeline.getCoderRegistry();
        coderRegistry.registerCoderForType(coder.getEncodedTypeDescriptor(), coder);
    
         PCollectionTuple transformOut =
            pipeline
                 //Step #1: Read messages in from Pub/Sub
                .apply(
                    "ReadPubsubMessages",
      PubsubIO.readMessagesWithAttributes().fromTopic(options.getInputTopic()))
    
                 //Step #2: Transform the PubsubMessages into TableRows
                .apply("ConvertMessageToTableRow", new PubsubMessageToTableRow(options));
    
        WriteResult writeResult = null;
    
        try {
          writeResult = 
                transformOut
            .get(TRANSFORM_OUT)
            .apply(
                "WriteSuccessfulRecords",      
                BigQueryIO.writeTableRows()
                    .withMethod(Method.STREAMING_INSERTS)
                    .withoutValidation()
                    .withCreateDisposition(CreateDisposition.CREATE_NEVER)
                    .withWriteDisposition(WriteDisposition.WRITE_APPEND)
                    .withFailedInsertRetryPolicy(InsertRetryPolicy.retryTransientErrors())
                    .to("myproject:MyDataSet.MyTable"));
        } catch (Exception e) {
            System.out.print("Cause of the Standard Insert Failure is: ");
            System.out.print(e.getCause());
        }
    
        try {
            writeResult
                .getFailedInserts()
                .apply(
                        "WriteFailedInsertsToDeadLetter",
                        BigQueryIO.writeTableRows()
                            .to(options.getOutputDeadletterTable())
                            .withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED)
                            .withWriteDisposition(WriteDisposition.WRITE_APPEND));
        } catch (Exception e) {
            System.out.print("Cause of the Error Insert Failure is: ");
            System.out.print(e.getCause());
        }
    
         PCollectionList.of(transformOut.get(UDF_DEADLETTER_OUT))
            .and(transformOut.get(TRANSFORM_DEADLETTER_OUT))
            .apply("Flatten", Flatten.pCollections())
            .apply(
                "WriteFailedRecords",
                WritePubsubMessageErrors.newBuilder()
                    .setErrorRecordsTable(
                        maybeUseDefaultDeadletterTable(
                            options.getOutputDeadletterTable(),
                            options.getOutputTableSpec(),
                            DEFAULT_DEADLETTER_TABLE_SUFFIX))
                    .setErrorRecordsTableSchema(getDeadletterTableSchemaJson())
                    .build());
    
        return pipeline.run();
      }
    

    错误:

    Cause of the Error Insert Failure is: null[WARNING] 
    java.lang.NullPointerException: Outputs for non-root node WriteFailedInsertsToDeadLetter are null
        at org.apache.beam.repackaged.beam_sdks_java_core.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:864)
        at org.apache.beam.sdk.runners.TransformHierarchy$Node.visit(TransformHierarchy.java:672)
        at org.apache.beam.sdk.runners.TransformHierarchy$Node.visit(TransformHierarchy.java:660)
        at org.apache.beam.sdk.runners.TransformHierarchy$Node.access$600(TransformHierarchy.java:311)
        at org.apache.beam.sdk.runners.TransformHierarchy.visit(TransformHierarchy.java:245)
        at org.apache.beam.sdk.Pipeline.traverseTopologically(Pipeline.java:458)
        at org.apache.beam.sdk.Pipeline.validate(Pipeline.java:575)
        at org.apache.beam.sdk.Pipeline.run(Pipeline.java:310)
        at org.apache.beam.sdk.Pipeline.run(Pipeline.java:297)
        at com.google.cloud.teleport.templates.PubSubToBigQuery.run(PubSubToBigQuery.java:312)
        at com.google.cloud.teleport.templates.PubSubToBigQuery.main(PubSubToBigQuery.java:186)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
        at java.lang.Thread.run(Thread.java:748)
    
    1 回复  |  直到 7 年前
        1
  •  6
  •   Ryan McDowell    7 年前

    BigQueryIO.Write WriteResult 对象,使您能够检索未能输出到BigQuery的TableRows的PCollection。使用它,您可以轻松地检索失败,将它们格式化为死信输出的结构,并将记录重新提交给BigQuery。这样就不需要单独的类来管理成功和失败的记录。

    下面是一个关于你的管道的例子。

    // Attempt to write the table rows to the output table.
    WriteResult writeResult =
        pipeline.apply(
            "WriteRecordsToBigQuery",
            BigQueryIO.writeTableRows()
                .to(options.getOutputTable())
                .withCreateDisposition(CreateDisposition.CREATE_NEVER)
                .withWriteDisposition(WriteDisposition.WRITE_APPEND)
                .withFailedInsertRetryPolicy(InsertRetryPolicy.retryTransientErrors()));
    
    /*
     * 1) Get the failed inserts
     * 2) Transform to the deadletter table format.
     * 3) Output to the deadletter table.
    */
    writeResult
      .getFailedInserts()
        .apply("FormatFailedInserts", ParDo.of(new FailedInsertFormatter()))
        .apply(
            "WriteFailedInsertsToDeadletter",
            BigQueryIO.writeTableRows()
                .to(options.getDeadletterTable())
                .withCreateDisposition(CreateDisposition.CREATE_NEVER)
                .withWriteDisposition(WriteDisposition.WRITE_APPEND));
    

    此外,要回答您的问题:

    1. 根据光束 docs ,必须设置 streaming true
    2. 应该没有 性能差异。无论哪种情况,都需要转换 将记录输入到 TableRow 物体。应该没什么区别 如果你事先在ParDo或者在一个可序列化的 函数使用 BigQueryIO.Write.withFormatFunction