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 2023/01/07 00:04:28 UTC

[GitHub] [beam] ahmedabu98 commented on a diff in pull request #24918: JDBC SchemaTransform implementation. Need to break out into a separat…

ahmedabu98 commented on code in PR #24918:
URL: https://github.com/apache/beam/pull/24918#discussion_r1063854607


##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcReadSchemaTransformProvider.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+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.PTransform;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+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 JdbcReadSchemaTransformProvider
+    extends TypedSchemaTransformProvider<
+        JdbcReadSchemaTransformProvider.JdbcReadSchemaTransformConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<JdbcReadSchemaTransformConfiguration>
+      configurationClass() {
+    return JdbcReadSchemaTransformConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      JdbcReadSchemaTransformConfiguration configuration) {
+    if (configuration.getReadQuery() != null && configuration.getLocation() != null) {

Review Comment:
   ```suggestion
       if (!Strings.isNullOrEmpty(configuration.getReadQuery()) && !Strings.isNullOrEmpty(configuration.getLocation())) {
   ```
   
   Empty strings may be sent from a remote SDK in place of null values. For example, in Python SDK it is currently not supported to send `None` values so no configuration field is actually ever null. For strings `""` is sent instead.
   
   Should apply similar checks to relevant configuration fields in this and JdbcWrite provider



##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcReadSchemaTransformProvider.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+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.PTransform;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+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 JdbcReadSchemaTransformProvider
+    extends TypedSchemaTransformProvider<
+        JdbcReadSchemaTransformProvider.JdbcReadSchemaTransformConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<JdbcReadSchemaTransformConfiguration>
+      configurationClass() {
+    return JdbcReadSchemaTransformConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      JdbcReadSchemaTransformConfiguration configuration) {
+    if (configuration.getReadQuery() != null && configuration.getLocation() != null) {
+      throw new IllegalArgumentException(
+          "ReadQuery and Location are mutually exclusive configurations");
+    }
+    if (configuration.getReadQuery() == null && configuration.getLocation() == null) {
+      throw new IllegalArgumentException("Either ReadQuery or Location must be set.");
+    }
+    return new JdbcReadSchemaTransform(configuration);
+  }
+
+  static class JdbcReadSchemaTransform implements SchemaTransform, Serializable {
+
+    JdbcReadSchemaTransformConfiguration config;
+
+    public JdbcReadSchemaTransform(JdbcReadSchemaTransformConfiguration config) {
+      this.config = config;
+    }
+
+    protected JdbcIO.DataSourceConfiguration dataSourceConfiguration() {
+      JdbcIO.DataSourceConfiguration dsConfig =
+          JdbcIO.DataSourceConfiguration.create(config.getDriverClassName(), config.getJdbcUrl())

Review Comment:
   Maybe `getDriverClassName()` and `getJdbcUrl()` should have some handling too and throw an early error if they're empty/null. 



##########
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcReadSchemaTransformProvider.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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;
+
+import com.google.auto.service.AutoService;
+import com.google.auto.value.AutoValue;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.schemas.AutoValueSchema;
+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.PTransform;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+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 JdbcReadSchemaTransformProvider
+    extends TypedSchemaTransformProvider<
+        JdbcReadSchemaTransformProvider.JdbcReadSchemaTransformConfiguration> {
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized Class<JdbcReadSchemaTransformConfiguration>
+      configurationClass() {
+    return JdbcReadSchemaTransformConfiguration.class;
+  }
+
+  @Override
+  protected @UnknownKeyFor @NonNull @Initialized SchemaTransform from(
+      JdbcReadSchemaTransformConfiguration configuration) {
+    if (configuration.getReadQuery() != null && configuration.getLocation() != null) {
+      throw new IllegalArgumentException(
+          "ReadQuery and Location are mutually exclusive configurations");
+    }
+    if (configuration.getReadQuery() == null && configuration.getLocation() == null) {
+      throw new IllegalArgumentException("Either ReadQuery or Location must be set.");
+    }
+    return new JdbcReadSchemaTransform(configuration);
+  }
+
+  static class JdbcReadSchemaTransform implements SchemaTransform, Serializable {
+
+    JdbcReadSchemaTransformConfiguration config;
+
+    public JdbcReadSchemaTransform(JdbcReadSchemaTransformConfiguration config) {
+      this.config = config;
+    }
+
+    protected JdbcIO.DataSourceConfiguration dataSourceConfiguration() {
+      JdbcIO.DataSourceConfiguration dsConfig =
+          JdbcIO.DataSourceConfiguration.create(config.getDriverClassName(), config.getJdbcUrl())
+              .withUsername(config.getUsername())
+              .withPassword(config.getPassword());
+      String connectionProperties = config.getConnectionProperties();
+      if (connectionProperties != null) {
+        dsConfig = dsConfig.withConnectionProperties(connectionProperties);
+      }
+
+      List<@org.checkerframework.checker.nullness.qual.Nullable String> initialSql =
+          config.getConnectionInitSql();
+      if (initialSql != null && initialSql.size() > 0) {
+        dsConfig = dsConfig.withConnectionInitSqls(initialSql);
+      }
+
+      return dsConfig;
+    }
+
+    @Override
+    public @UnknownKeyFor @NonNull @Initialized PTransform<
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple,
+            @UnknownKeyFor @NonNull @Initialized PCollectionRowTuple>
+        buildTransform() {
+      return new PTransform<PCollectionRowTuple, PCollectionRowTuple>() {
+        @Override
+        public PCollectionRowTuple expand(PCollectionRowTuple input) {
+          String query = config.getReadQuery();
+          if (query == null) {
+            query = String.format("SELECT * FROM %s", config.getLocation());
+          }
+          JdbcIO.ReadRows readRows =
+              JdbcIO.readRows()
+                  .withDataSourceConfiguration(dataSourceConfiguration())
+                  .withQuery(query);
+          Short fetchSize = config.getFetchSize();
+          if (fetchSize != null) {

Review Comment:
   ```suggestion
             if (fetchSize != null && fetchSize > 0) {
   ```
   
   For the same reasons mentioned above, fetchSize could have a default value of 0 in place of null.



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