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

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

                Author: ASF GitHub Bot
            Created on: 31/May/19 18:28
            Start Date: 31/May/19 18:28
    Worklog Time Spent: 10m 
      Work Description: reuvenlax commented on pull request #8725: [BEAM-6674] 
Add schema support to JdbcIO read
URL: https://github.com/apache/beam/pull/8725#discussion_r289493336
 
 

 ##########
 File path: 
sdks/java/io/jdbc/src/main/java/org/apache/beam/sdk/io/jdbc/SchemaUtil.java
 ##########
 @@ -0,0 +1,321 @@
+/*
+ * 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.io.Serializable;
+import java.sql.Array;
+import java.sql.Date;
+import java.sql.JDBCType;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalTime;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.util.ArrayList;
+import java.util.EnumMap;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.beam.sdk.schemas.LogicalTypes;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.values.Row;
+import 
org.apache.beam.vendor.guava.v20_0.com.google.common.annotations.VisibleForTesting;
+import 
org.apache.beam.vendor.guava.v20_0.com.google.common.collect.ImmutableMap;
+import org.joda.time.DateTime;
+import org.joda.time.chrono.ISOChronology;
+
+/** Provides utility functions for working with Beam {@link Schema} types. */
+class SchemaUtil {
+  private static final String SQL_DATE = "SqlDateType";
+  private static final String SQL_TIME = "SqlTimeType";
+  private static final String SQL_TIMESTAMP_WITH_LOCAL_TZ = 
"SqlTimestampWithLocalTzType";
+
+  @VisibleForTesting
+  static final Schema.FieldType SQL_DATE_LOGICAL_TYPE =
+      Schema.FieldType.logicalType(
+          new LogicalTypes.PassThroughLogicalType<Instant>(
+              SQL_DATE, "", Schema.FieldType.DATETIME) {});
+
+  @VisibleForTesting
+  static final Schema.FieldType SQL_TIME_LOGICAL_TYPE =
+      Schema.FieldType.logicalType(
+          new LogicalTypes.PassThroughLogicalType<Instant>(
+              SQL_TIME, "", Schema.FieldType.DATETIME) {});
+
+  @VisibleForTesting
+  static final Schema.FieldType SQL_TIMESTAMP_WITH_LOCAL_TZ_LOGICAL_TYPE =
+      Schema.FieldType.logicalType(
+          new LogicalTypes.PassThroughLogicalType<Instant>(
+              SQL_TIMESTAMP_WITH_LOCAL_TZ, "", Schema.FieldType.DATETIME) {});
+
+  /**
+   * Interface implemented by functions that extract values of different types 
from a JDBC
+   * ResultSet.
+   */
+  @FunctionalInterface
+  interface ResultSetFieldExtractor extends Serializable {
+    Object extract(ResultSet rs, Integer index) throws SQLException;
+  }
+
+  // ResultSetExtractors for primitive schema types (excluding arrays, structs 
and logical types).
+  private static final EnumMap<Schema.TypeName, ResultSetFieldExtractor>
+      RESULTSET_FIELD_EXTRACTORS =
+          new EnumMap<>(
+              ImmutableMap.<Schema.TypeName, ResultSetFieldExtractor>builder()
+                  .put(Schema.TypeName.BOOLEAN, ResultSet::getBoolean)
+                  .put(Schema.TypeName.BYTE, ResultSet::getByte)
+                  .put(Schema.TypeName.BYTES, ResultSet::getBytes)
+                  .put(Schema.TypeName.DATETIME, ResultSet::getTimestamp)
+                  .put(Schema.TypeName.DECIMAL, ResultSet::getBigDecimal)
+                  .put(Schema.TypeName.DOUBLE, ResultSet::getDouble)
+                  .put(Schema.TypeName.FLOAT, ResultSet::getFloat)
+                  .put(Schema.TypeName.INT16, ResultSet::getShort)
+                  .put(Schema.TypeName.INT32, ResultSet::getInt)
+                  .put(Schema.TypeName.INT64, ResultSet::getLong)
+                  .put(Schema.TypeName.STRING, ResultSet::getString)
+                  .build());
+
+  private static final ResultSetFieldExtractor DATE_EXTRACTOR = 
createDateExtractor();
+  private static final ResultSetFieldExtractor TIME_EXTRACTOR = 
createTimeExtractor();
+  private static final ResultSetFieldExtractor TIMESTAMP_EXTRACTOR = 
createTimestampExtractor();
+
+  /*
+   * Mapping of primitive JDBC types to Beam schema types.
+   * TODO: Add support for BLOB and CLOB types
 
 Review comment:
   Please file JIRA tickets for for these
 
----------------------------------------------------------------
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:
us...@infra.apache.org


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

    Worklog Id:     (was: 251951)

> The JdbcIO source should produce schemas
> ----------------------------------------
>
>                 Key: BEAM-6674
>                 URL: https://issues.apache.org/jira/browse/BEAM-6674
>             Project: Beam
>          Issue Type: Sub-task
>          Components: io-java-jdbc
>            Reporter: Reuven Lax
>            Assignee: Shehzaad Nakhoda
>            Priority: Major
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>




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

Reply via email to