[ 
https://issues.apache.org/jira/browse/BEAM-6675?focusedWorklogId=274214&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-274214
 ]

ASF GitHub Bot logged work on BEAM-6675:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 09/Jul/19 17:02
            Start Date: 09/Jul/19 17:02
    Worklog Time Spent: 10m 
      Work Description: amaliujia commented on pull request #8962: [BEAM-6675] 
Generate JDBC statement and preparedStatementSetter automatically when schema 
is available
URL: https://github.com/apache/beam/pull/8962#discussion_r301695275
 
 

 ##########
 File path: 
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/JdbcUtil.java
 ##########
 @@ -0,0 +1,179 @@
+/*
+ * 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 java.sql.Date;
+import java.sql.JDBCType;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.EnumMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.beam.sdk.schemas.Schema;
+import 
org.apache.beam.vendor.guava.v20_0.com.google.common.collect.ImmutableMap;
+
+/** Provides utility functions for working with {@link JdbcIO}. */
+public class JdbcUtil {
+
+  /** Generates an insert statement based on {@Link Schema.Field}. * */
+  public static String generateStatement(String tableName, List<Schema.Field> 
fields) {
+
+    String fieldNames =
+        IntStream.range(0, fields.size())
+            .mapToObj(
+                (index) -> {
+                  return fields.get(index).getName();
+                })
+            .collect(Collectors.joining(", "));
+
+    String valuePlaceholder =
+        IntStream.range(0, fields.size())
+            .mapToObj(
+                (index) -> {
+                  return "?";
+                })
+            .collect(Collectors.joining(", "));
+
+    return String.format("INSERT INTO %s(%s) VALUES(%s)", tableName, 
fieldNames, valuePlaceholder);
+  }
+
+  /** PreparedStatementSetCaller for Schema Field types. * */
+  private static Map<Schema.TypeName, JdbcIO.PreparedStatementSetCaller> 
typeNamePsSetCallerMap =
+      new EnumMap<Schema.TypeName, JdbcIO.PreparedStatementSetCaller>(
+          ImmutableMap.<Schema.TypeName, 
JdbcIO.PreparedStatementSetCaller>builder()
+              .put(
+                  Schema.TypeName.BYTE,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setByte(i + 1, 
element.getByte(fieldWithIndex.getIndex())))
+              .put(
+                  Schema.TypeName.INT16,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setInt(i + 1, 
element.getInt16(fieldWithIndex.getIndex())))
+              .put(
+                  Schema.TypeName.INT64,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setLong(i + 1, 
element.getInt64(fieldWithIndex.getIndex())))
+              .put(
+                  Schema.TypeName.DECIMAL,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setBigDecimal(i + 1, 
element.getDecimal(fieldWithIndex.getIndex())))
+              .put(
+                  Schema.TypeName.FLOAT,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setFloat(i + 1, 
element.getFloat(fieldWithIndex.getIndex())))
+              .put(
+                  Schema.TypeName.DOUBLE,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setDouble(i + 1, 
element.getDouble(fieldWithIndex.getIndex())))
+              .put(
+                  Schema.TypeName.DATETIME,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setTimestamp(
+                          i + 1,
+                          new Timestamp(
+                              
element.getDateTime(fieldWithIndex.getIndex()).getMillis())))
+              .put(
+                  Schema.TypeName.BOOLEAN,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setBoolean(i + 1, 
element.getBoolean(fieldWithIndex.getIndex())))
+              .put(Schema.TypeName.BYTES, createBytesCaller())
+              .put(
+                  Schema.TypeName.INT32,
+                  (element, ps, i, fieldWithIndex) ->
+                      ps.setInt(i + 1, 
element.getInt32(fieldWithIndex.getIndex())))
+              .put(Schema.TypeName.STRING, createStringCaller())
+              .build());
+
+  /** PreparedStatementSetCaller for Schema Field Logical types. * */
+  public static JdbcIO.PreparedStatementSetCaller 
getPreparedStatementSetCaller(
+      Schema.FieldType fieldType) {
+    switch (fieldType.getTypeName()) {
+      case ARRAY:
+        return (element, ps, i, fieldWithIndex) -> {
+          ps.setArray(
+              i + 1,
+              ps.getConnection()
+                  .createArrayOf(
+                      
fieldType.getCollectionElementType().getTypeName().name(),
+                      element.getArray(fieldWithIndex.getIndex()).toArray()));
+        };
+      case LOGICAL_TYPE:
+        {
+          String logicalTypeName = fieldType.getLogicalType().getIdentifier();
+          JDBCType jdbcType = JDBCType.valueOf(logicalTypeName);
+          switch (jdbcType) {
+            case DATE:
+              return (element, ps, i, fieldWithIndex) -> {
+                ps.setDate(
+                    i + 1, new 
Date(element.getDateTime(fieldWithIndex.getIndex()).getMillis()));
 
 Review comment:
   I guess here we should obey `java.sql.Date` and `java.sql.Time`'s specs. 
   
   https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html
   https://docs.oracle.com/javase/8/docs/api/index.html?java/sql/Time.html
 
----------------------------------------------------------------
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:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 274214)
    Time Spent: 3h 40m  (was: 3.5h)

> The JdbcIO sink should accept schemas
> -------------------------------------
>
>                 Key: BEAM-6675
>                 URL: https://issues.apache.org/jira/browse/BEAM-6675
>             Project: Beam
>          Issue Type: Sub-task
>          Components: io-java-jdbc
>            Reporter: Reuven Lax
>            Assignee: Shehzaad Nakhoda
>            Priority: Major
>          Time Spent: 3h 40m
>  Remaining Estimate: 0h
>
> If the input has a schema, there should be a default mapping to a 
> PreparedStatement for writing based on that schema.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to