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 2022/11/19 20:50:16 UTC

[GitHub] [beam] pabloem opened a new pull request, #24278: A schema transform implementation for SpannerIO.Write

pabloem opened a new pull request, #24278:
URL: https://github.com/apache/beam/pull/24278

   **Please** add a meaningful description for your change here
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] [**Choose reviewer(s)**](https://beam.apache.org/contribute/#make-your-change) and mention them in a comment (`R: @username`).
    - [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem commented on a diff in pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem commented on code in PR #24278:
URL: https://github.com/apache/beam/pull/24278#discussion_r1029930587


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider
+    extends TypedSchemaTransformProvider<
+        SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<
+          SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration>
+      configurationClass() {
+    return SpannerSchemaTransformWriteConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      SpannerSchemaTransformWriteConfiguration configuration) {
+    return new SpannerSchemaTransformWrite(configuration);
+  }
+
+  static class SpannerSchemaTransformWrite implements SchemaTransform, Serializable {
+    private final SpannerSchemaTransformWriteConfiguration configuration;
+
+    SpannerSchemaTransformWrite(SpannerSchemaTransformWriteConfiguration configuration) {
+      this.configuration = configuration;
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized PTransform<
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple,
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple>
+        buildTransform() {
+      return new PTransform<@NonNull PCollectionRowTuple, @NonNull PCollectionRowTuple>() {
+        @Override
+        public PCollectionRowTuple expand(@NonNull PCollectionRowTuple input) {
+          SpannerWriteResult result =
+              input
+                  .get("input")
+                  .apply(
+                      MapElements.via(
+                          new SimpleFunction<Row, Mutation>(
+                              row ->
+                                  MutationUtils.createMutationFromBeamRows(
+                                      Mutation.newInsertOrUpdateBuilder(configuration.getTableId()),
+                                      Objects.requireNonNull(row))) {}))
+                  .apply(
+                      SpannerIO.write()
+                          .withDatabaseId(configuration.getDatabaseId())
+                          .withInstanceId(configuration.getInstanceId())
+                          .withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES));
+          Schema failureSchema =
+              Schema.builder()
+                  .addStringField("operation")
+                  .addStringField("instanceId")
+                  .addStringField("databaseId")
+                  .addStringField("tableId")
+                  .addStringField("mutationData")
+                  .build();
+          result
+              .getFailedMutations()
+              .apply(
+                  FlatMapElements.into(TypeDescriptors.rows())
+                      .via(
+                          mtg ->
+                              Objects.requireNonNull(mtg).attached().stream()
+                                  .map(
+                                      mutation ->
+                                          Row.withSchema(failureSchema)
+                                              .addValue(mutation.getOperation().toString())
+                                              .addValue(configuration.getInstanceId())
+                                              .addValue(configuration.getDatabaseId())
+                                              .addValue(mutation.getTable())
+                                              // TODO(pabloem): Figure out how to represent
+                                              // mutation
+                                              //  contents in DLQ
+                                              .addValue(
+                                                  Iterators.toString(
+                                                      mutation.getValues().iterator()))
+                                              .build())
+                                  .collect(Collectors.toList())))
+              .setRowSchema(failureSchema);
+          return PCollectionRowTuple.empty(input.getPipeline());

Review Comment:
   also planning to leave this work for later as we productionize the transform. is that ok?



##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider

Review Comment:
   done. thanks!



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] github-actions[bot] commented on pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #24278:
URL: https://github.com/apache/beam/pull/24278#issuecomment-1320979186

   Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
   
   R: @kileys for label java.
   R: @chamikaramj for label io.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
   
   The PR bot will only process comments in the main thread (not review comments).


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem commented on a diff in pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem commented on code in PR #24278:
URL: https://github.com/apache/beam/pull/24278#discussion_r1030867553


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider
+    extends TypedSchemaTransformProvider<
+        SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<
+          SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration>
+      configurationClass() {
+    return SpannerSchemaTransformWriteConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      SpannerSchemaTransformWriteConfiguration configuration) {
+    return new SpannerSchemaTransformWrite(configuration);
+  }
+
+  static class SpannerSchemaTransformWrite implements SchemaTransform, Serializable {
+    private final SpannerSchemaTransformWriteConfiguration configuration;
+
+    SpannerSchemaTransformWrite(SpannerSchemaTransformWriteConfiguration configuration) {
+      this.configuration = configuration;
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized PTransform<
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple,
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple>
+        buildTransform() {
+      return new PTransform<@NonNull PCollectionRowTuple, @NonNull PCollectionRowTuple>() {
+        @Override
+        public PCollectionRowTuple expand(@NonNull PCollectionRowTuple input) {
+          SpannerWriteResult result =
+              input
+                  .get("input")
+                  .apply(
+                      MapElements.via(
+                          new SimpleFunction<Row, Mutation>(
+                              row ->
+                                  MutationUtils.createMutationFromBeamRows(
+                                      Mutation.newInsertOrUpdateBuilder(configuration.getTableId()),
+                                      Objects.requireNonNull(row))) {}))
+                  .apply(
+                      SpannerIO.write()
+                          .withDatabaseId(configuration.getDatabaseId())
+                          .withInstanceId(configuration.getInstanceId())
+                          .withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES));
+          Schema failureSchema =
+              Schema.builder()
+                  .addStringField("operation")
+                  .addStringField("instanceId")
+                  .addStringField("databaseId")
+                  .addStringField("tableId")
+                  .addStringField("mutationData")
+                  .build();
+          result
+              .getFailedMutations()
+              .apply(
+                  FlatMapElements.into(TypeDescriptors.rows())
+                      .via(
+                          mtg ->
+                              Objects.requireNonNull(mtg).attached().stream()
+                                  .map(
+                                      mutation ->
+                                          Row.withSchema(failureSchema)
+                                              .addValue(mutation.getOperation().toString())
+                                              .addValue(configuration.getInstanceId())
+                                              .addValue(configuration.getDatabaseId())
+                                              .addValue(mutation.getTable())
+                                              // TODO(pabloem): Figure out how to represent
+                                              // mutation
+                                              //  contents in DLQ
+                                              .addValue(
+                                                  Iterators.toString(
+                                                      mutation.getValues().iterator()))
+                                              .build())
+                                  .collect(Collectors.toList())))
+              .setRowSchema(failureSchema);
+          return PCollectionRowTuple.empty(input.getPipeline());

Review Comment:
   done and added a rudimentary return value for now



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] ahmedabu98 commented on a diff in pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
ahmedabu98 commented on code in PR #24278:
URL: https://github.com/apache/beam/pull/24278#discussion_r1029585393


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider

Review Comment:
   SpannerWriteSchemaTransformProvider? in reference to https://lists.apache.org/thread/vzt6f074c5oy1pvm2slrc476fmkhoof4



##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider
+    extends TypedSchemaTransformProvider<
+        SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<
+          SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration>
+      configurationClass() {
+    return SpannerSchemaTransformWriteConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      SpannerSchemaTransformWriteConfiguration configuration) {
+    return new SpannerSchemaTransformWrite(configuration);
+  }
+
+  static class SpannerSchemaTransformWrite implements SchemaTransform, Serializable {
+    private final SpannerSchemaTransformWriteConfiguration configuration;
+
+    SpannerSchemaTransformWrite(SpannerSchemaTransformWriteConfiguration configuration) {
+      this.configuration = configuration;
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized PTransform<
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple,
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple>
+        buildTransform() {
+      return new PTransform<@NonNull PCollectionRowTuple, @NonNull PCollectionRowTuple>() {
+        @Override
+        public PCollectionRowTuple expand(@NonNull PCollectionRowTuple input) {
+          SpannerWriteResult result =
+              input
+                  .get("input")
+                  .apply(
+                      MapElements.via(
+                          new SimpleFunction<Row, Mutation>(
+                              row ->
+                                  MutationUtils.createMutationFromBeamRows(
+                                      Mutation.newInsertOrUpdateBuilder(configuration.getTableId()),
+                                      Objects.requireNonNull(row))) {}))
+                  .apply(
+                      SpannerIO.write()
+                          .withDatabaseId(configuration.getDatabaseId())
+                          .withInstanceId(configuration.getInstanceId())
+                          .withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES));
+          Schema failureSchema =
+              Schema.builder()
+                  .addStringField("operation")
+                  .addStringField("instanceId")
+                  .addStringField("databaseId")
+                  .addStringField("tableId")
+                  .addStringField("mutationData")
+                  .build();
+          result
+              .getFailedMutations()
+              .apply(
+                  FlatMapElements.into(TypeDescriptors.rows())
+                      .via(
+                          mtg ->
+                              Objects.requireNonNull(mtg).attached().stream()
+                                  .map(
+                                      mutation ->
+                                          Row.withSchema(failureSchema)
+                                              .addValue(mutation.getOperation().toString())
+                                              .addValue(configuration.getInstanceId())
+                                              .addValue(configuration.getDatabaseId())
+                                              .addValue(mutation.getTable())
+                                              // TODO(pabloem): Figure out how to represent
+                                              // mutation
+                                              //  contents in DLQ
+                                              .addValue(
+                                                  Iterators.toString(
+                                                      mutation.getValues().iterator()))
+                                              .build())
+                                  .collect(Collectors.toList())))
+              .setRowSchema(failureSchema);
+          return PCollectionRowTuple.empty(input.getPipeline());
+        }
+      };
+    }
+  }
+
+  @Override
+  public @UnknownKeyFor @NonNull @Initialized String identifier() {
+    return "beam:schematransform:org.apache.beam:spanner_write:v1";
+  }
+
+  @Override
+  public @UnknownKeyFor @NonNull @Initialized List<@UnknownKeyFor @NonNull @Initialized String>
+      inputCollectionNames() {
+    return Collections.singletonList("input");
+  }
+
+  @Override
+  public @UnknownKeyFor @NonNull @Initialized List<@UnknownKeyFor @NonNull @Initialized String>
+      outputCollectionNames() {
+    return Collections.emptyList();
+  }
+
+  @AutoValue
+  @DefaultSchema(AutoValueSchema.class)
+  public abstract static class SpannerSchemaTransformWriteConfiguration implements Serializable {

Review Comment:
   Any validation checks needed here?



##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider
+    extends TypedSchemaTransformProvider<
+        SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<
+          SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration>
+      configurationClass() {
+    return SpannerSchemaTransformWriteConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      SpannerSchemaTransformWriteConfiguration configuration) {
+    return new SpannerSchemaTransformWrite(configuration);
+  }
+
+  static class SpannerSchemaTransformWrite implements SchemaTransform, Serializable {
+    private final SpannerSchemaTransformWriteConfiguration configuration;
+
+    SpannerSchemaTransformWrite(SpannerSchemaTransformWriteConfiguration configuration) {
+      this.configuration = configuration;
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized PTransform<
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple,
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple>
+        buildTransform() {
+      return new PTransform<@NonNull PCollectionRowTuple, @NonNull PCollectionRowTuple>() {
+        @Override
+        public PCollectionRowTuple expand(@NonNull PCollectionRowTuple input) {
+          SpannerWriteResult result =
+              input
+                  .get("input")
+                  .apply(
+                      MapElements.via(
+                          new SimpleFunction<Row, Mutation>(
+                              row ->
+                                  MutationUtils.createMutationFromBeamRows(
+                                      Mutation.newInsertOrUpdateBuilder(configuration.getTableId()),
+                                      Objects.requireNonNull(row))) {}))
+                  .apply(
+                      SpannerIO.write()
+                          .withDatabaseId(configuration.getDatabaseId())
+                          .withInstanceId(configuration.getInstanceId())
+                          .withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES));
+          Schema failureSchema =
+              Schema.builder()
+                  .addStringField("operation")
+                  .addStringField("instanceId")
+                  .addStringField("databaseId")
+                  .addStringField("tableId")
+                  .addStringField("mutationData")
+                  .build();
+          result
+              .getFailedMutations()
+              .apply(
+                  FlatMapElements.into(TypeDescriptors.rows())
+                      .via(
+                          mtg ->
+                              Objects.requireNonNull(mtg).attached().stream()
+                                  .map(
+                                      mutation ->
+                                          Row.withSchema(failureSchema)
+                                              .addValue(mutation.getOperation().toString())
+                                              .addValue(configuration.getInstanceId())
+                                              .addValue(configuration.getDatabaseId())
+                                              .addValue(mutation.getTable())
+                                              // TODO(pabloem): Figure out how to represent
+                                              // mutation
+                                              //  contents in DLQ
+                                              .addValue(
+                                                  Iterators.toString(
+                                                      mutation.getValues().iterator()))
+                                              .build())
+                                  .collect(Collectors.toList())))
+              .setRowSchema(failureSchema);
+          return PCollectionRowTuple.empty(input.getPipeline());

Review Comment:
   Is there any way to convert the mutations back to rows at this stage and return the failed rows?



##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider

Review Comment:
   ie. for the naming in this class, keep SpannerWrite on the left side and everything else on the right, may help with readability. WDYT?



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem commented on pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem commented on PR #24278:
URL: https://github.com/apache/beam/pull/24278#issuecomment-1322787178

   r: @ahmedabu98 
   
   I would appreciate comments on the API and so on : )


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem commented on pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem commented on PR #24278:
URL: https://github.com/apache/beam/pull/24278#issuecomment-1325725534

   thanks : D addressed comments - anything else? : D @ahmedabu98 


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] github-actions[bot] commented on pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #24278:
URL: https://github.com/apache/beam/pull/24278#issuecomment-1322788022

   Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem commented on pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem commented on PR #24278:
URL: https://github.com/apache/beam/pull/24278#issuecomment-1325432543

   Run Java_GCP_IO_Direct PreCommit


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem merged pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem merged PR #24278:
URL: https://github.com/apache/beam/pull/24278


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] ahmedabu98 commented on a diff in pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
ahmedabu98 commented on code in PR #24278:
URL: https://github.com/apache/beam/pull/24278#discussion_r1030853369


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider
+    extends TypedSchemaTransformProvider<
+        SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<
+          SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration>
+      configurationClass() {
+    return SpannerSchemaTransformWriteConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      SpannerSchemaTransformWriteConfiguration configuration) {
+    return new SpannerSchemaTransformWrite(configuration);
+  }
+
+  static class SpannerSchemaTransformWrite implements SchemaTransform, Serializable {
+    private final SpannerSchemaTransformWriteConfiguration configuration;
+
+    SpannerSchemaTransformWrite(SpannerSchemaTransformWriteConfiguration configuration) {
+      this.configuration = configuration;
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized PTransform<
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple,
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple>
+        buildTransform() {
+      return new PTransform<@NonNull PCollectionRowTuple, @NonNull PCollectionRowTuple>() {
+        @Override
+        public PCollectionRowTuple expand(@NonNull PCollectionRowTuple input) {
+          SpannerWriteResult result =
+              input
+                  .get("input")
+                  .apply(
+                      MapElements.via(
+                          new SimpleFunction<Row, Mutation>(
+                              row ->
+                                  MutationUtils.createMutationFromBeamRows(
+                                      Mutation.newInsertOrUpdateBuilder(configuration.getTableId()),
+                                      Objects.requireNonNull(row))) {}))
+                  .apply(
+                      SpannerIO.write()
+                          .withDatabaseId(configuration.getDatabaseId())
+                          .withInstanceId(configuration.getInstanceId())
+                          .withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES));
+          Schema failureSchema =
+              Schema.builder()
+                  .addStringField("operation")
+                  .addStringField("instanceId")
+                  .addStringField("databaseId")
+                  .addStringField("tableId")
+                  .addStringField("mutationData")
+                  .build();
+          result
+              .getFailedMutations()
+              .apply(
+                  FlatMapElements.into(TypeDescriptors.rows())
+                      .via(
+                          mtg ->
+                              Objects.requireNonNull(mtg).attached().stream()
+                                  .map(
+                                      mutation ->
+                                          Row.withSchema(failureSchema)
+                                              .addValue(mutation.getOperation().toString())
+                                              .addValue(configuration.getInstanceId())
+                                              .addValue(configuration.getDatabaseId())
+                                              .addValue(mutation.getTable())
+                                              // TODO(pabloem): Figure out how to represent
+                                              // mutation
+                                              //  contents in DLQ
+                                              .addValue(
+                                                  Iterators.toString(
+                                                      mutation.getValues().iterator()))
+                                              .build())
+                                  .collect(Collectors.toList())))
+              .setRowSchema(failureSchema);
+          return PCollectionRowTuple.empty(input.getPipeline());

Review Comment:
   Yup! can you leave a TODO for later? maybe someone can pick it up. Same with the validation checks



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem commented on pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem commented on PR #24278:
URL: https://github.com/apache/beam/pull/24278#issuecomment-1325671789

   Run Java_GCP_IO_Direct PreCommit


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem commented on a diff in pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem commented on code in PR #24278:
URL: https://github.com/apache/beam/pull/24278#discussion_r1029930463


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaTransformWriteProvider.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.sdk.io.gcp.spanner;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.spanner.Mutation;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.transforms.FlatMapElements;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterators;
+import org.checkerframework.checker.initialization.qual.Initialized;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
+
+@AutoService(SchemaTransformProvider.class)
+public class SpannerSchemaTransformWriteProvider
+    extends TypedSchemaTransformProvider<
+        SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<
+          SpannerSchemaTransformWriteProvider.SpannerSchemaTransformWriteConfiguration>
+      configurationClass() {
+    return SpannerSchemaTransformWriteConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      SpannerSchemaTransformWriteConfiguration configuration) {
+    return new SpannerSchemaTransformWrite(configuration);
+  }
+
+  static class SpannerSchemaTransformWrite implements SchemaTransform, Serializable {
+    private final SpannerSchemaTransformWriteConfiguration configuration;
+
+    SpannerSchemaTransformWrite(SpannerSchemaTransformWriteConfiguration configuration) {
+      this.configuration = configuration;
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized PTransform<
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple,
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple>
+        buildTransform() {
+      return new PTransform<@NonNull PCollectionRowTuple, @NonNull PCollectionRowTuple>() {
+        @Override
+        public PCollectionRowTuple expand(@NonNull PCollectionRowTuple input) {
+          SpannerWriteResult result =
+              input
+                  .get("input")
+                  .apply(
+                      MapElements.via(
+                          new SimpleFunction<Row, Mutation>(
+                              row ->
+                                  MutationUtils.createMutationFromBeamRows(
+                                      Mutation.newInsertOrUpdateBuilder(configuration.getTableId()),
+                                      Objects.requireNonNull(row))) {}))
+                  .apply(
+                      SpannerIO.write()
+                          .withDatabaseId(configuration.getDatabaseId())
+                          .withInstanceId(configuration.getInstanceId())
+                          .withFailureMode(SpannerIO.FailureMode.REPORT_FAILURES));
+          Schema failureSchema =
+              Schema.builder()
+                  .addStringField("operation")
+                  .addStringField("instanceId")
+                  .addStringField("databaseId")
+                  .addStringField("tableId")
+                  .addStringField("mutationData")
+                  .build();
+          result
+              .getFailedMutations()
+              .apply(
+                  FlatMapElements.into(TypeDescriptors.rows())
+                      .via(
+                          mtg ->
+                              Objects.requireNonNull(mtg).attached().stream()
+                                  .map(
+                                      mutation ->
+                                          Row.withSchema(failureSchema)
+                                              .addValue(mutation.getOperation().toString())
+                                              .addValue(configuration.getInstanceId())
+                                              .addValue(configuration.getDatabaseId())
+                                              .addValue(mutation.getTable())
+                                              // TODO(pabloem): Figure out how to represent
+                                              // mutation
+                                              //  contents in DLQ
+                                              .addValue(
+                                                  Iterators.toString(
+                                                      mutation.getValues().iterator()))
+                                              .build())
+                                  .collect(Collectors.toList())))
+              .setRowSchema(failureSchema);
+          return PCollectionRowTuple.empty(input.getPipeline());
+        }
+      };
+    }
+  }
+
+  @Override
+  public @UnknownKeyFor @NonNull @Initialized String identifier() {
+    return "beam:schematransform:org.apache.beam:spanner_write:v1";
+  }
+
+  @Override
+  public @UnknownKeyFor @NonNull @Initialized List<@UnknownKeyFor @NonNull @Initialized String>
+      inputCollectionNames() {
+    return Collections.singletonList("input");
+  }
+
+  @Override
+  public @UnknownKeyFor @NonNull @Initialized List<@UnknownKeyFor @NonNull @Initialized String>
+      outputCollectionNames() {
+    return Collections.emptyList();
+  }
+
+  @AutoValue
+  @DefaultSchema(AutoValueSchema.class)
+  public abstract static class SpannerSchemaTransformWriteConfiguration implements Serializable {

Review Comment:
   not for now. For now we will allow ourselves to fail at runtime, but we may add validations later.



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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


[GitHub] [beam] pabloem commented on pull request #24278: A schema transform implementation for SpannerIO.Write

Posted by GitBox <gi...@apache.org>.
pabloem commented on PR #24278:
URL: https://github.com/apache/beam/pull/24278#issuecomment-1324496074

   Run Kotlin_Examples PreCommit


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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