You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/04/20 01:06:46 UTC

[GitHub] [beam] rezarokni commented on a change in pull request #11437: [BEAM-9770] Add BigQueryIO deadletter pattern

rezarokni commented on a change in pull request #11437:
URL: https://github.com/apache/beam/pull/11437#discussion_r411034442



##########
File path: examples/java/src/main/java/org/apache/beam/examples/snippets/Snippets.java
##########
@@ -753,35 +750,93 @@ public static void main(String[] args) {
       // [START CustomSessionWindow5]
 
       PCollection<TableRow> p =
-          Pipeline.create()
-              .apply(
-                  "Create data",
-                  Create.timestamped(
-                      TimestampedValue.of(
-                          new TableRow().set("user", "mobile").set("score", 12).set("gap", 5),
-                          new Instant()),
-                      TimestampedValue.of(
-                          new TableRow().set("user", "desktop").set("score", 4), new Instant()),
-                      TimestampedValue.of(
-                          new TableRow().set("user", "mobile").set("score", -3).set("gap", 5),
-                          new Instant().plus(2000)),
-                      TimestampedValue.of(
-                          new TableRow().set("user", "mobile").set("score", 2).set("gap", 5),
-                          new Instant().plus(9000)),
-                      TimestampedValue.of(
-                          new TableRow().set("user", "mobile").set("score", 7).set("gap", 5),
-                          new Instant().plus(12000)),
-                      TimestampedValue.of(
-                          new TableRow().set("user", "desktop").set("score", 10),
-                          new Instant().plus(12000))));
+              Pipeline.create()
+                      .apply(
+                              "Create data",
+                              Create.timestamped(
+                                      TimestampedValue.of(
+                                              new TableRow().set("user", "mobile").set("score", 12).set("gap", 5),
+                                              new Instant()),
+                                      TimestampedValue.of(
+                                              new TableRow().set("user", "desktop").set("score", 4), new Instant()),
+                                      TimestampedValue.of(
+                                              new TableRow().set("user", "mobile").set("score", -3).set("gap", 5),
+                                              new Instant().plus(2000)),
+                                      TimestampedValue.of(
+                                              new TableRow().set("user", "mobile").set("score", 2).set("gap", 5),
+                                              new Instant().plus(9000)),
+                                      TimestampedValue.of(
+                                              new TableRow().set("user", "mobile").set("score", 7).set("gap", 5),
+                                              new Instant().plus(12000)),
+                                      TimestampedValue.of(
+                                              new TableRow().set("user", "desktop").set("score", 10),
+                                              new Instant().plus(12000))));
       // [END CustomSessionWindow5]
 
       // [START CustomSessionWindow6]
       p.apply(
-          "Window into sessions",
-          Window.<TableRow>into(
-              DynamicSessions.withDefaultGapDuration(Duration.standardSeconds(10))));
+              "Window into sessions",
+              Window.<TableRow>into(
+                      DynamicSessions.withDefaultGapDuration(Duration.standardSeconds(10))));
       // [END CustomSessionWindow6]
+    }
+
+    public static class DeadLetterBigQuery {
+      public static void deadletter(String[] args) {
+        // [START BigQueryIODeadLetter]
+        // Create pipeline
+        PipelineOptions options =
+                PipelineOptionsFactory.fromArgs(args).withValidation().as(BigQueryOptions.class);
+
+        Pipeline p = Pipeline.create(options);
+
+        // Create a bug by writing the 2nd value as null. The API will correctly
+        // throw an error when trying to insert a null value into a REQUIRED field.
+        WriteResult result =
+                p.apply(Create.of(1, 2))
+                        .apply(
+                                BigQueryIO.<Integer>write()
+                                        .withSchema(
+                                                new TableSchema()
+                                                        .setFields(
+                                                                com.google.common.collect.ImmutableList.of(
+                                                                        new TableFieldSchema()
+                                                                                .setName("num")
+                                                                                .setType("INTEGER")
+                                                                                .setMode("REQUIRED"))))
+                                        .to("Test.dummyTable")
+                                        .withFormatFunction(x -> new TableRow().set("num", (x == 2) ? null : x))
+                                        .withFailedInsertRetryPolicy(InsertRetryPolicy.neverRetry())

Review comment:
       Fixed.

##########
File path: website/src/documentation/patterns/bigqueryio.md
##########
@@ -0,0 +1,37 @@
+---
+layout: section
+title: "BigQuery patterns"
+section_menu: section-menu/documentation.html
+permalink: /documentation/patterns/bigqueryio/
+---
+<!--
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+# Google BigQuery patterns
+
+The samples on this page show you common patterns for use with BigQueryIO.
+
+## BigQueryIO deadletter pattern
+In production systems, it is useful to implement the deadletter pattern with BigQueryIO outputting any elements which had errors during processing by BigQueryIO into another PCollection for further processing. 
+
+When using `STREAMING_INSERTS`  you can use the `WriteResult` object to access a `PCollection` with the `TableRows` that failed to be inserted into BigQuery. 

Review comment:
       Fixed.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org