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/06/16 20:15:37 UTC

[GitHub] [beam] piotr-szuberski opened a new pull request #12022: [BEAM-10135] Add Jdbc Write external transform

piotr-szuberski opened a new pull request #12022:
URL: https://github.com/apache/beam/pull/12022


   Add External transform for Jdbc write.
   
   ------------------------
   
   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`).
    - [ ] Format the pull request title like `[BEAM-XXX] Fixes bug in ApproximateQuantiles`, where you replace `BEAM-XXX` with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
    - [ ] 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/#make-reviewers-job-easier).
   
   Post-Commit Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   
   Lang | SDK | Apex | Dataflow | Flink | Samza | Spark
   --- | --- | --- | --- | --- | --- | ---
   Go | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/) | --- | --- | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/) | --- | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/)
   Java | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Apex/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Apex/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/)
   Python | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Python2/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Python2/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Python35/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Python35/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/) | --- | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PreCommit_Python2_PVR_Flink_Cron/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PreCommit_Python2_PVR_Flink_Cron/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PostCommit_Python35_VR_Flink/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Python35_VR_Flink/lastCompletedBuild/) | --- | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/)
   XLang | --- | --- | --- | [![Build Status](https://builds.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/) | --- | --- | [![Build Status](https://builds.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/)
   
   Pre-Commit Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   
   --- |Java | Python | Go | Website
   --- | --- | --- | --- | ---
   Non-portable | [![Build Status](https://builds.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/)<br>[![Build Status](https://builds.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/) 
   Portable | --- | [![Build Status](https://builds.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/) | --- | ---
   
   See [.test-infra/jenkins/README](https://github.com/apache/beam/blob/master/.test-infra/jenkins/README.md) for trigger phrase, status and link of all Jenkins jobs.
   


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



[GitHub] [beam] piotr-szuberski edited a comment on pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski edited a comment on pull request #12022:
URL: https://github.com/apache/beam/pull/12022#issuecomment-648314418


   @TheNeuralBit If you have some suggestions, I ping you :)


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



[GitHub] [beam] piotr-szuberski commented on pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #12022:
URL: https://github.com/apache/beam/pull/12022#issuecomment-648314418


   @TheNeuralBit I've made the requested changes. If you have further suggestions, go ahead.


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



[GitHub] [beam] piotr-szuberski commented on a change in pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #12022:
URL: https://github.com/apache/beam/pull/12022#discussion_r448116884



##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/JdbcExternalWrite.java
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.jdbc.external;
+
+import com.google.auto.service.AutoService;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.expansion.ExternalTransformRegistrar;
+import org.apache.beam.sdk.io.jdbc.JdbcIO;
+import org.apache.beam.sdk.io.jdbc.JdbcIO.DataSourceConfiguration;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.ExternalTransformBuilder;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PDone;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+/** Exposes {@link JdbcIO.Write} as an external transform for cross-language usage. */
+@Experimental(Kind.PORTABILITY)
+@AutoService(ExternalTransformRegistrar.class)
+public class JdbcExternalWrite implements ExternalTransformRegistrar {
+
+  public static final String URN = "beam:external:java:jdbc:write:v1";
+
+  @Override
+  public Map<String, Class<? extends ExternalTransformBuilder>> knownBuilders() {
+    return ImmutableMap.of(URN, JdbcExternalWrite.Builder.class);
+  }
+
+  /** Parameters class to expose the Write transform to an external SDK. */
+  public static class Configuration {
+    private String statement;
+    private String driverClassName;
+    private String jdbcUrl;
+    private String username;
+    private String password;
+    private String connectionProperties;
+    private Iterable<String> connectionInitSqls;
+
+    public void setStatement(String statement) {
+      this.statement = statement;
+    }
+
+    public void setDriverClassName(String driverClassName) {
+      this.driverClassName = driverClassName;
+    }
+
+    public void setJdbcUrl(String jdbcUrl) {
+      this.jdbcUrl = jdbcUrl;
+    }
+
+    public void setUsername(String username) {
+      this.username = username;
+    }
+
+    public void setPassword(String password) {
+      this.password = password;
+    }
+
+    public void setConnectionProperties(String connectionProperties) {
+      this.connectionProperties = connectionProperties;
+    }
+
+    public void setConnectionInitSqls(Iterable<String> connectionInitSqls) {
+      this.connectionInitSqls = connectionInitSqls;
+    }
+  }
+
+  public static class Builder
+      implements ExternalTransformBuilder<
+          JdbcExternalWrite.Configuration, PCollection<Row>, PDone> {
+    @Override
+    public PTransform<PCollection<Row>, PDone> buildExternal(
+        JdbcExternalWrite.Configuration configuration) {
+      DataSourceConfiguration dataSourceConfiguration =
+          DataSourceConfiguration.create(configuration.driverClassName, configuration.jdbcUrl)
+              .withUsername(configuration.username)
+              .withPassword(configuration.password)
+              .withConnectionProperties(configuration.connectionProperties);
+
+      if (configuration.connectionInitSqls != null) {
+        List<String> connectionInitSqls =
+            StreamSupport.stream(configuration.connectionInitSqls.spliterator(), false)
+                .collect(Collectors.toList());
+        dataSourceConfiguration =
+            dataSourceConfiguration.withConnectionInitSqls(connectionInitSqls);
+      }
+
+      JdbcIO.PreparedStatementSetter<Row> preparedStatementSetter = new PrepareStatementXlangRow();
+
+      return JdbcIO.<Row>write()
+          .withDataSourceConfiguration(dataSourceConfiguration)
+          .withStatement(configuration.statement)
+          .withPreparedStatementSetter(preparedStatementSetter);
+    }
+
+    static class PrepareStatementXlangRow implements JdbcIO.PreparedStatementSetter<Row> {
+      @Override
+      public void setParameters(Row row, PreparedStatement statement) throws SQLException {
+        List<Schema.Field> fieldTypes = row.getSchema().getFields();
+        for (int i = 0; i < fieldTypes.size(); ++i) {
+          Schema.FieldType type = fieldTypes.get(i).getType();
+          if (Schema.FieldType.BYTE.equals(type)) {
+            statement.setByte(i + 1, row.getByte(i));
+          } else if (Schema.FieldType.INT16.equals(type)) {
+            statement.setShort(i + 1, row.getInt16(i));
+          } else if (Schema.FieldType.INT32.equals(type)) {
+            statement.setInt(i + 1, row.getInt32(i));
+          } else if (Schema.FieldType.INT64.equals(type)) {
+            statement.setLong(i + 1, row.getInt64(i));
+          } else if (Schema.FieldType.FLOAT.equals(type)) {
+            statement.setFloat(i + 1, row.getFloat(i));
+          } else if (Schema.FieldType.DOUBLE.equals(type)) {
+            statement.setDouble(i + 1, row.getDouble(i));
+          } else if (Schema.FieldType.DECIMAL.equals(type)) {
+            statement.setBigDecimal(i + 1, row.getDecimal(i));
+          } else if (Schema.FieldType.BOOLEAN.equals(type)) {
+            statement.setBoolean(i + 1, row.getBoolean(i));
+          } else if (Schema.FieldType.STRING.equals(type)) {
+            statement.setString(i + 1, row.getString(i));
+          } else if (Schema.FieldType.BYTES.equals(type)) {
+            statement.setBytes(i + 1, row.getBytes(i));
+          } else if (Schema.FieldType.DATETIME.equals(type)) {
+            statement.setDate(i + 1, new Date(row.getDateTime(i).toDateTime().getMillis()));
+          } else {
+            throw new IllegalArgumentException(String.format("Unknown FieldType: %s", type));
+          }

Review comment:
       I did not come out with an idea that there can be some enum under the hood. I've parsed atomic types only and left TODO to use writeRows with appropriate Jira issue https://issues.apache.org/jira/browse/BEAM-10396




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



[GitHub] [beam] piotr-szuberski commented on a change in pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #12022:
URL: https://github.com/apache/beam/pull/12022#discussion_r447616009



##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/JdbcExternalWrite.java
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.jdbc.external;
+
+import com.google.auto.service.AutoService;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.expansion.ExternalTransformRegistrar;
+import org.apache.beam.sdk.io.jdbc.JdbcIO;
+import org.apache.beam.sdk.io.jdbc.JdbcIO.DataSourceConfiguration;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.ExternalTransformBuilder;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PDone;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+/** Exposes {@link JdbcIO.Write} as an external transform for cross-language usage. */
+@Experimental(Kind.PORTABILITY)
+@AutoService(ExternalTransformRegistrar.class)
+public class JdbcExternalWrite implements ExternalTransformRegistrar {
+
+  public static final String URN = "beam:external:java:jdbc:write:v1";
+
+  @Override
+  public Map<String, Class<? extends ExternalTransformBuilder>> knownBuilders() {
+    return ImmutableMap.of(URN, JdbcExternalWrite.Builder.class);
+  }
+
+  /** Parameters class to expose the Write transform to an external SDK. */
+  public static class Configuration {
+    private String statement;
+    private String driverClassName;
+    private String jdbcUrl;
+    private String username;
+    private String password;
+    private String connectionProperties;
+    private Iterable<String> connectionInitSqls;
+
+    public void setStatement(String statement) {
+      this.statement = statement;
+    }
+
+    public void setDriverClassName(String driverClassName) {
+      this.driverClassName = driverClassName;
+    }
+
+    public void setJdbcUrl(String jdbcUrl) {
+      this.jdbcUrl = jdbcUrl;
+    }
+
+    public void setUsername(String username) {
+      this.username = username;
+    }
+
+    public void setPassword(String password) {
+      this.password = password;
+    }
+
+    public void setConnectionProperties(String connectionProperties) {
+      this.connectionProperties = connectionProperties;
+    }
+
+    public void setConnectionInitSqls(Iterable<String> connectionInitSqls) {
+      this.connectionInitSqls = connectionInitSqls;
+    }
+  }
+
+  public static class Builder
+      implements ExternalTransformBuilder<
+          JdbcExternalWrite.Configuration, PCollection<Row>, PDone> {
+    @Override
+    public PTransform<PCollection<Row>, PDone> buildExternal(
+        JdbcExternalWrite.Configuration configuration) {
+      DataSourceConfiguration dataSourceConfiguration =
+          DataSourceConfiguration.create(configuration.driverClassName, configuration.jdbcUrl)
+              .withUsername(configuration.username)
+              .withPassword(configuration.password)
+              .withConnectionProperties(configuration.connectionProperties);
+
+      if (configuration.connectionInitSqls != null) {
+        List<String> connectionInitSqls =
+            StreamSupport.stream(configuration.connectionInitSqls.spliterator(), false)
+                .collect(Collectors.toList());
+        dataSourceConfiguration =
+            dataSourceConfiguration.withConnectionInitSqls(connectionInitSqls);
+      }
+
+      JdbcIO.PreparedStatementSetter<Row> preparedStatementSetter = new PrepareStatementXlangRow();
+
+      return JdbcIO.<Row>write()
+          .withDataSourceConfiguration(dataSourceConfiguration)
+          .withStatement(configuration.statement)
+          .withPreparedStatementSetter(preparedStatementSetter);
+    }
+
+    static class PrepareStatementXlangRow implements JdbcIO.PreparedStatementSetter<Row> {
+      @Override
+      public void setParameters(Row row, PreparedStatement statement) throws SQLException {
+        List<Schema.Field> fieldTypes = row.getSchema().getFields();
+        for (int i = 0; i < fieldTypes.size(); ++i) {
+          Schema.FieldType type = fieldTypes.get(i).getType();
+          if (Schema.FieldType.BYTE.equals(type)) {
+            statement.setByte(i + 1, row.getByte(i));
+          } else if (Schema.FieldType.INT16.equals(type)) {
+            statement.setShort(i + 1, row.getInt16(i));
+          } else if (Schema.FieldType.INT32.equals(type)) {
+            statement.setInt(i + 1, row.getInt32(i));
+          } else if (Schema.FieldType.INT64.equals(type)) {
+            statement.setLong(i + 1, row.getInt64(i));
+          } else if (Schema.FieldType.FLOAT.equals(type)) {
+            statement.setFloat(i + 1, row.getFloat(i));
+          } else if (Schema.FieldType.DOUBLE.equals(type)) {
+            statement.setDouble(i + 1, row.getDouble(i));
+          } else if (Schema.FieldType.DECIMAL.equals(type)) {
+            statement.setBigDecimal(i + 1, row.getDecimal(i));
+          } else if (Schema.FieldType.BOOLEAN.equals(type)) {
+            statement.setBoolean(i + 1, row.getBoolean(i));
+          } else if (Schema.FieldType.STRING.equals(type)) {
+            statement.setString(i + 1, row.getString(i));
+          } else if (Schema.FieldType.BYTES.equals(type)) {
+            statement.setBytes(i + 1, row.getBytes(i));
+          } else if (Schema.FieldType.DATETIME.equals(type)) {
+            statement.setDate(i + 1, new Date(row.getDateTime(i).toDateTime().getMillis()));
+          } else {
+            throw new IllegalArgumentException(String.format("Unknown FieldType: %s", type));
+          }

Review comment:
       I tried it, but type isn't an enum or int - it's an instance of FieldType class




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



[GitHub] [beam] piotr-szuberski commented on a change in pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #12022:
URL: https://github.com/apache/beam/pull/12022#discussion_r447648913



##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/package-info.java
##########
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+/** External transforms for reading and writing from JDBC. */
+@Experimental(Kind.PORTABILITY)
+package org.apache.beam.sdk.io.jdbc.external;

Review comment:
       Done




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



[GitHub] [beam] piotr-szuberski commented on pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #12022:
URL: https://github.com/apache/beam/pull/12022#issuecomment-648780535


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

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



[GitHub] [beam] piotr-szuberski closed pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski closed pull request #12022:
URL: https://github.com/apache/beam/pull/12022


   


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



[GitHub] [beam] TheNeuralBit commented on a change in pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
TheNeuralBit commented on a change in pull request #12022:
URL: https://github.com/apache/beam/pull/12022#discussion_r447186177



##########
File path: sdks/java/container/Dockerfile
##########
@@ -27,6 +27,7 @@ ADD target/beam-sdks-java-harness.jar /opt/apache/beam/jars/
 # TODO May be removed once custom environments are supported
 ADD target/beam-sdks-java-io-kafka.jar /opt/apache/beam/jars/
 ADD target/kafka-clients.jar /opt/apache/beam/jars/
+ADD target/beam-sdks-java-io-jdbc.jar /opt/apache/beam/jars/

Review comment:
       @chamikaramj is this still necessary? I thought the artifact staging service was supposed to remove the need for this

##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/package-info.java
##########
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+/** External transforms for reading and writing from JDBC. */
+@Experimental(Kind.PORTABILITY)
+package org.apache.beam.sdk.io.jdbc.external;

Review comment:
       Please add `@DefaultAnnotation(NonNull.class)` like this:
   https://github.com/apache/beam/blob/51bd3a4588984c9680f61a3268513fe5bde3ba96/sdks/java/core/src/main/java/org/apache/beam/sdk/package-info.java#L29

##########
File path: runners/flink/job-server/flink_job_server.gradle
##########
@@ -88,6 +88,7 @@ dependencies {
 //  TODO: Enable AWS and HDFS file system.
   // For resolving external transform requests
   runtime project(":sdks:java:io:kafka")
+  runtime project(":sdks:java:io:jdbc")

Review comment:
       I'm not sure there's actually any logic in the Python side that will ever expand jdbc from the expansion server built into Flink. I'd suggest removing this (unless I'm mistaken and there is some path on the Python side that will use it).

##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/JdbcExternalWrite.java
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.jdbc.external;
+
+import com.google.auto.service.AutoService;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.expansion.ExternalTransformRegistrar;
+import org.apache.beam.sdk.io.jdbc.JdbcIO;
+import org.apache.beam.sdk.io.jdbc.JdbcIO.DataSourceConfiguration;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.ExternalTransformBuilder;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PDone;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+/** Exposes {@link JdbcIO.Write} as an external transform for cross-language usage. */
+@Experimental(Kind.PORTABILITY)
+@AutoService(ExternalTransformRegistrar.class)
+public class JdbcExternalWrite implements ExternalTransformRegistrar {
+
+  public static final String URN = "beam:external:java:jdbc:write:v1";
+
+  @Override
+  public Map<String, Class<? extends ExternalTransformBuilder>> knownBuilders() {
+    return ImmutableMap.of(URN, JdbcExternalWrite.Builder.class);
+  }
+
+  /** Parameters class to expose the Write transform to an external SDK. */
+  public static class Configuration {
+    private String statement;
+    private String driverClassName;
+    private String jdbcUrl;
+    private String username;
+    private String password;
+    private String connectionProperties;
+    private Iterable<String> connectionInitSqls;
+
+    public void setStatement(String statement) {
+      this.statement = statement;
+    }
+
+    public void setDriverClassName(String driverClassName) {
+      this.driverClassName = driverClassName;
+    }
+
+    public void setJdbcUrl(String jdbcUrl) {
+      this.jdbcUrl = jdbcUrl;
+    }
+
+    public void setUsername(String username) {
+      this.username = username;
+    }
+
+    public void setPassword(String password) {
+      this.password = password;
+    }
+
+    public void setConnectionProperties(String connectionProperties) {
+      this.connectionProperties = connectionProperties;
+    }
+
+    public void setConnectionInitSqls(Iterable<String> connectionInitSqls) {
+      this.connectionInitSqls = connectionInitSqls;
+    }
+  }
+
+  public static class Builder
+      implements ExternalTransformBuilder<
+          JdbcExternalWrite.Configuration, PCollection<Row>, PDone> {
+    @Override
+    public PTransform<PCollection<Row>, PDone> buildExternal(
+        JdbcExternalWrite.Configuration configuration) {
+      DataSourceConfiguration dataSourceConfiguration =
+          DataSourceConfiguration.create(configuration.driverClassName, configuration.jdbcUrl)
+              .withUsername(configuration.username)
+              .withPassword(configuration.password)
+              .withConnectionProperties(configuration.connectionProperties);
+
+      if (configuration.connectionInitSqls != null) {
+        List<String> connectionInitSqls =
+            StreamSupport.stream(configuration.connectionInitSqls.spliterator(), false)
+                .collect(Collectors.toList());
+        dataSourceConfiguration =
+            dataSourceConfiguration.withConnectionInitSqls(connectionInitSqls);
+      }
+
+      JdbcIO.PreparedStatementSetter<Row> preparedStatementSetter = new PrepareStatementXlangRow();
+
+      return JdbcIO.<Row>write()
+          .withDataSourceConfiguration(dataSourceConfiguration)
+          .withStatement(configuration.statement)
+          .withPreparedStatementSetter(preparedStatementSetter);

Review comment:
       It would be nice at some point to make this into a `JdbcIO.WriteRows` companion to `JdbcIO.ReadRows`, but doesn't need to happen as part of this PR.

##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/JdbcExternalWrite.java
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.jdbc.external;
+
+import com.google.auto.service.AutoService;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.expansion.ExternalTransformRegistrar;
+import org.apache.beam.sdk.io.jdbc.JdbcIO;
+import org.apache.beam.sdk.io.jdbc.JdbcIO.DataSourceConfiguration;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.ExternalTransformBuilder;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PDone;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+/** Exposes {@link JdbcIO.Write} as an external transform for cross-language usage. */
+@Experimental(Kind.PORTABILITY)
+@AutoService(ExternalTransformRegistrar.class)
+public class JdbcExternalWrite implements ExternalTransformRegistrar {
+
+  public static final String URN = "beam:external:java:jdbc:write:v1";
+
+  @Override
+  public Map<String, Class<? extends ExternalTransformBuilder>> knownBuilders() {
+    return ImmutableMap.of(URN, JdbcExternalWrite.Builder.class);
+  }
+
+  /** Parameters class to expose the Write transform to an external SDK. */
+  public static class Configuration {
+    private String statement;
+    private String driverClassName;
+    private String jdbcUrl;
+    private String username;
+    private String password;
+    private String connectionProperties;
+    private Iterable<String> connectionInitSqls;
+
+    public void setStatement(String statement) {
+      this.statement = statement;
+    }
+
+    public void setDriverClassName(String driverClassName) {
+      this.driverClassName = driverClassName;
+    }
+
+    public void setJdbcUrl(String jdbcUrl) {
+      this.jdbcUrl = jdbcUrl;
+    }
+
+    public void setUsername(String username) {
+      this.username = username;
+    }
+
+    public void setPassword(String password) {
+      this.password = password;
+    }
+
+    public void setConnectionProperties(String connectionProperties) {
+      this.connectionProperties = connectionProperties;
+    }
+
+    public void setConnectionInitSqls(Iterable<String> connectionInitSqls) {
+      this.connectionInitSqls = connectionInitSqls;
+    }
+  }
+
+  public static class Builder
+      implements ExternalTransformBuilder<
+          JdbcExternalWrite.Configuration, PCollection<Row>, PDone> {
+    @Override
+    public PTransform<PCollection<Row>, PDone> buildExternal(
+        JdbcExternalWrite.Configuration configuration) {
+      DataSourceConfiguration dataSourceConfiguration =
+          DataSourceConfiguration.create(configuration.driverClassName, configuration.jdbcUrl)
+              .withUsername(configuration.username)
+              .withPassword(configuration.password)
+              .withConnectionProperties(configuration.connectionProperties);
+
+      if (configuration.connectionInitSqls != null) {
+        List<String> connectionInitSqls =
+            StreamSupport.stream(configuration.connectionInitSqls.spliterator(), false)
+                .collect(Collectors.toList());
+        dataSourceConfiguration =
+            dataSourceConfiguration.withConnectionInitSqls(connectionInitSqls);
+      }
+
+      JdbcIO.PreparedStatementSetter<Row> preparedStatementSetter = new PrepareStatementXlangRow();
+
+      return JdbcIO.<Row>write()
+          .withDataSourceConfiguration(dataSourceConfiguration)
+          .withStatement(configuration.statement)
+          .withPreparedStatementSetter(preparedStatementSetter);
+    }
+
+    static class PrepareStatementXlangRow implements JdbcIO.PreparedStatementSetter<Row> {
+      @Override
+      public void setParameters(Row row, PreparedStatement statement) throws SQLException {
+        List<Schema.Field> fieldTypes = row.getSchema().getFields();
+        for (int i = 0; i < fieldTypes.size(); ++i) {
+          Schema.FieldType type = fieldTypes.get(i).getType();
+          if (Schema.FieldType.BYTE.equals(type)) {
+            statement.setByte(i + 1, row.getByte(i));
+          } else if (Schema.FieldType.INT16.equals(type)) {
+            statement.setShort(i + 1, row.getInt16(i));
+          } else if (Schema.FieldType.INT32.equals(type)) {
+            statement.setInt(i + 1, row.getInt32(i));
+          } else if (Schema.FieldType.INT64.equals(type)) {
+            statement.setLong(i + 1, row.getInt64(i));
+          } else if (Schema.FieldType.FLOAT.equals(type)) {
+            statement.setFloat(i + 1, row.getFloat(i));
+          } else if (Schema.FieldType.DOUBLE.equals(type)) {
+            statement.setDouble(i + 1, row.getDouble(i));
+          } else if (Schema.FieldType.DECIMAL.equals(type)) {
+            statement.setBigDecimal(i + 1, row.getDecimal(i));
+          } else if (Schema.FieldType.BOOLEAN.equals(type)) {
+            statement.setBoolean(i + 1, row.getBoolean(i));
+          } else if (Schema.FieldType.STRING.equals(type)) {
+            statement.setString(i + 1, row.getString(i));
+          } else if (Schema.FieldType.BYTES.equals(type)) {
+            statement.setBytes(i + 1, row.getBytes(i));
+          } else if (Schema.FieldType.DATETIME.equals(type)) {
+            statement.setDate(i + 1, new Date(row.getDateTime(i).toDateTime().getMillis()));
+          } else {
+            throw new IllegalArgumentException(String.format("Unknown FieldType: %s", type));
+          }

Review comment:
       nit: this could be a `switch` statement on `type`

##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/JdbcExternalWrite.java
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.jdbc.external;
+
+import com.google.auto.service.AutoService;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.expansion.ExternalTransformRegistrar;
+import org.apache.beam.sdk.io.jdbc.JdbcIO;
+import org.apache.beam.sdk.io.jdbc.JdbcIO.DataSourceConfiguration;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.ExternalTransformBuilder;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PDone;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+/** Exposes {@link JdbcIO.Write} as an external transform for cross-language usage. */
+@Experimental(Kind.PORTABILITY)
+@AutoService(ExternalTransformRegistrar.class)
+public class JdbcExternalWrite implements ExternalTransformRegistrar {
+
+  public static final String URN = "beam:external:java:jdbc:write:v1";
+
+  @Override
+  public Map<String, Class<? extends ExternalTransformBuilder>> knownBuilders() {
+    return ImmutableMap.of(URN, JdbcExternalWrite.Builder.class);
+  }
+
+  /** Parameters class to expose the Write transform to an external SDK. */
+  public static class Configuration {
+    private String statement;
+    private String driverClassName;
+    private String jdbcUrl;
+    private String username;
+    private String password;
+    private String connectionProperties;
+    private Iterable<String> connectionInitSqls;
+
+    public void setStatement(String statement) {
+      this.statement = statement;
+    }
+
+    public void setDriverClassName(String driverClassName) {
+      this.driverClassName = driverClassName;
+    }
+
+    public void setJdbcUrl(String jdbcUrl) {
+      this.jdbcUrl = jdbcUrl;
+    }
+
+    public void setUsername(String username) {
+      this.username = username;
+    }
+
+    public void setPassword(String password) {
+      this.password = password;
+    }
+
+    public void setConnectionProperties(String connectionProperties) {
+      this.connectionProperties = connectionProperties;
+    }
+
+    public void setConnectionInitSqls(Iterable<String> connectionInitSqls) {
+      this.connectionInitSqls = connectionInitSqls;
+    }
+  }
+
+  public static class Builder
+      implements ExternalTransformBuilder<
+          JdbcExternalWrite.Configuration, PCollection<Row>, PDone> {
+    @Override
+    public PTransform<PCollection<Row>, PDone> buildExternal(
+        JdbcExternalWrite.Configuration configuration) {
+      DataSourceConfiguration dataSourceConfiguration =
+          DataSourceConfiguration.create(configuration.driverClassName, configuration.jdbcUrl)
+              .withUsername(configuration.username)
+              .withPassword(configuration.password)
+              .withConnectionProperties(configuration.connectionProperties);
+
+      if (configuration.connectionInitSqls != null) {
+        List<String> connectionInitSqls =
+            StreamSupport.stream(configuration.connectionInitSqls.spliterator(), false)
+                .collect(Collectors.toList());
+        dataSourceConfiguration =
+            dataSourceConfiguration.withConnectionInitSqls(connectionInitSqls);
+      }
+
+      JdbcIO.PreparedStatementSetter<Row> preparedStatementSetter = new PrepareStatementXlangRow();
+
+      return JdbcIO.<Row>write()
+          .withDataSourceConfiguration(dataSourceConfiguration)
+          .withStatement(configuration.statement)
+          .withPreparedStatementSetter(preparedStatementSetter);
+    }
+
+    static class PrepareStatementXlangRow implements JdbcIO.PreparedStatementSetter<Row> {

Review comment:
       I think this can be private




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



[GitHub] [beam] piotr-szuberski commented on a change in pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #12022:
URL: https://github.com/apache/beam/pull/12022#discussion_r447616560



##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/JdbcExternalWrite.java
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.jdbc.external;
+
+import com.google.auto.service.AutoService;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.expansion.ExternalTransformRegistrar;
+import org.apache.beam.sdk.io.jdbc.JdbcIO;
+import org.apache.beam.sdk.io.jdbc.JdbcIO.DataSourceConfiguration;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.ExternalTransformBuilder;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PDone;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+/** Exposes {@link JdbcIO.Write} as an external transform for cross-language usage. */
+@Experimental(Kind.PORTABILITY)
+@AutoService(ExternalTransformRegistrar.class)
+public class JdbcExternalWrite implements ExternalTransformRegistrar {
+
+  public static final String URN = "beam:external:java:jdbc:write:v1";
+
+  @Override
+  public Map<String, Class<? extends ExternalTransformBuilder>> knownBuilders() {
+    return ImmutableMap.of(URN, JdbcExternalWrite.Builder.class);
+  }
+
+  /** Parameters class to expose the Write transform to an external SDK. */
+  public static class Configuration {
+    private String statement;
+    private String driverClassName;
+    private String jdbcUrl;
+    private String username;
+    private String password;
+    private String connectionProperties;
+    private Iterable<String> connectionInitSqls;
+
+    public void setStatement(String statement) {
+      this.statement = statement;
+    }
+
+    public void setDriverClassName(String driverClassName) {
+      this.driverClassName = driverClassName;
+    }
+
+    public void setJdbcUrl(String jdbcUrl) {
+      this.jdbcUrl = jdbcUrl;
+    }
+
+    public void setUsername(String username) {
+      this.username = username;
+    }
+
+    public void setPassword(String password) {
+      this.password = password;
+    }
+
+    public void setConnectionProperties(String connectionProperties) {
+      this.connectionProperties = connectionProperties;
+    }
+
+    public void setConnectionInitSqls(Iterable<String> connectionInitSqls) {
+      this.connectionInitSqls = connectionInitSqls;
+    }
+  }
+
+  public static class Builder
+      implements ExternalTransformBuilder<
+          JdbcExternalWrite.Configuration, PCollection<Row>, PDone> {
+    @Override
+    public PTransform<PCollection<Row>, PDone> buildExternal(
+        JdbcExternalWrite.Configuration configuration) {
+      DataSourceConfiguration dataSourceConfiguration =
+          DataSourceConfiguration.create(configuration.driverClassName, configuration.jdbcUrl)
+              .withUsername(configuration.username)
+              .withPassword(configuration.password)
+              .withConnectionProperties(configuration.connectionProperties);
+
+      if (configuration.connectionInitSqls != null) {
+        List<String> connectionInitSqls =
+            StreamSupport.stream(configuration.connectionInitSqls.spliterator(), false)
+                .collect(Collectors.toList());
+        dataSourceConfiguration =
+            dataSourceConfiguration.withConnectionInitSqls(connectionInitSqls);
+      }
+
+      JdbcIO.PreparedStatementSetter<Row> preparedStatementSetter = new PrepareStatementXlangRow();
+
+      return JdbcIO.<Row>write()
+          .withDataSourceConfiguration(dataSourceConfiguration)
+          .withStatement(configuration.statement)
+          .withPreparedStatementSetter(preparedStatementSetter);

Review comment:
       When I was writing Read xlang transform I noticed there are 2 of them and in read I use ReadRows. But I agree it could be done in another task.




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



[GitHub] [beam] piotr-szuberski commented on a change in pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #12022:
URL: https://github.com/apache/beam/pull/12022#discussion_r447647773



##########
File path: runners/flink/job-server/flink_job_server.gradle
##########
@@ -88,6 +88,7 @@ dependencies {
 //  TODO: Enable AWS and HDFS file system.
   // For resolving external transform requests
   runtime project(":sdks:java:io:kafka")
+  runtime project(":sdks:java:io:jdbc")

Review comment:
       Indeed it wasn't needed here.




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



[GitHub] [beam] piotr-szuberski commented on a change in pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #12022:
URL: https://github.com/apache/beam/pull/12022#discussion_r447648168



##########
File path: sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/external/JdbcExternalWrite.java
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.jdbc.external;
+
+import com.google.auto.service.AutoService;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.expansion.ExternalTransformRegistrar;
+import org.apache.beam.sdk.io.jdbc.JdbcIO;
+import org.apache.beam.sdk.io.jdbc.JdbcIO.DataSourceConfiguration;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.ExternalTransformBuilder;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PDone;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+/** Exposes {@link JdbcIO.Write} as an external transform for cross-language usage. */
+@Experimental(Kind.PORTABILITY)
+@AutoService(ExternalTransformRegistrar.class)
+public class JdbcExternalWrite implements ExternalTransformRegistrar {
+
+  public static final String URN = "beam:external:java:jdbc:write:v1";
+
+  @Override
+  public Map<String, Class<? extends ExternalTransformBuilder>> knownBuilders() {
+    return ImmutableMap.of(URN, JdbcExternalWrite.Builder.class);
+  }
+
+  /** Parameters class to expose the Write transform to an external SDK. */
+  public static class Configuration {
+    private String statement;
+    private String driverClassName;
+    private String jdbcUrl;
+    private String username;
+    private String password;
+    private String connectionProperties;
+    private Iterable<String> connectionInitSqls;
+
+    public void setStatement(String statement) {
+      this.statement = statement;
+    }
+
+    public void setDriverClassName(String driverClassName) {
+      this.driverClassName = driverClassName;
+    }
+
+    public void setJdbcUrl(String jdbcUrl) {
+      this.jdbcUrl = jdbcUrl;
+    }
+
+    public void setUsername(String username) {
+      this.username = username;
+    }
+
+    public void setPassword(String password) {
+      this.password = password;
+    }
+
+    public void setConnectionProperties(String connectionProperties) {
+      this.connectionProperties = connectionProperties;
+    }
+
+    public void setConnectionInitSqls(Iterable<String> connectionInitSqls) {
+      this.connectionInitSqls = connectionInitSqls;
+    }
+  }
+
+  public static class Builder
+      implements ExternalTransformBuilder<
+          JdbcExternalWrite.Configuration, PCollection<Row>, PDone> {
+    @Override
+    public PTransform<PCollection<Row>, PDone> buildExternal(
+        JdbcExternalWrite.Configuration configuration) {
+      DataSourceConfiguration dataSourceConfiguration =
+          DataSourceConfiguration.create(configuration.driverClassName, configuration.jdbcUrl)
+              .withUsername(configuration.username)
+              .withPassword(configuration.password)
+              .withConnectionProperties(configuration.connectionProperties);
+
+      if (configuration.connectionInitSqls != null) {
+        List<String> connectionInitSqls =
+            StreamSupport.stream(configuration.connectionInitSqls.spliterator(), false)
+                .collect(Collectors.toList());
+        dataSourceConfiguration =
+            dataSourceConfiguration.withConnectionInitSqls(connectionInitSqls);
+      }
+
+      JdbcIO.PreparedStatementSetter<Row> preparedStatementSetter = new PrepareStatementXlangRow();
+
+      return JdbcIO.<Row>write()
+          .withDataSourceConfiguration(dataSourceConfiguration)
+          .withStatement(configuration.statement)
+          .withPreparedStatementSetter(preparedStatementSetter);
+    }
+
+    static class PrepareStatementXlangRow implements JdbcIO.PreparedStatementSetter<Row> {

Review comment:
       sure. And I'll change the name to XlangPrepareStatement




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



[GitHub] [beam] piotr-szuberski commented on pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #12022:
URL: https://github.com/apache/beam/pull/12022#issuecomment-651005473


   @TheNeuralBit ping


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



[GitHub] [beam] piotr-szuberski commented on a change in pull request #12022: [BEAM-10135] Add Jdbc Write external transform

Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #12022:
URL: https://github.com/apache/beam/pull/12022#discussion_r447655698



##########
File path: sdks/java/container/Dockerfile
##########
@@ -27,6 +27,7 @@ ADD target/beam-sdks-java-harness.jar /opt/apache/beam/jars/
 # TODO May be removed once custom environments are supported
 ADD target/beam-sdks-java-io-kafka.jar /opt/apache/beam/jars/
 ADD target/kafka-clients.jar /opt/apache/beam/jars/
+ADD target/beam-sdks-java-io-jdbc.jar /opt/apache/beam/jars/

Review comment:
       I removed it on my local environment and it seems to be redundant indeed. But I think that earlier it didn't work without this line.




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