Search in sources :

Example 1 with JdbcType

use of org.apache.gobblin.converter.jdbc.JdbcType in project incubator-gobblin by apache.

the class TeradataWriterCommands method retrieveDateColumns.

/**
 * {@inheritDoc}
 * @see org.apache.gobblin.writer.commands.JdbcWriterCommands#retrieveDateColumns(java.sql.Connection, java.lang.String)
 */
@Override
public Map<String, JdbcType> retrieveDateColumns(String database, String table) throws SQLException {
    Map<String, JdbcType> targetDataTypes = ImmutableMap.<String, JdbcType>builder().put("AT", JdbcType.TIME).put("DA", JdbcType.DATE).put("TS", JdbcType.TIMESTAMP).build();
    ImmutableMap.Builder<String, JdbcType> dateColumnsBuilder = ImmutableMap.builder();
    try (PreparedStatement pstmt = this.conn.prepareStatement(DBC_COLUMNS_SELECT_SQL_PSTMT, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
        pstmt.setString(1, database);
        pstmt.setString(2, table);
        LOG.info("Retrieving column type information from SQL: " + pstmt);
        try (ResultSet rs = pstmt.executeQuery()) {
            if (!rs.first()) {
                throw new IllegalArgumentException("No result from information_schema.columns");
            }
            do {
                String type = rs.getString("columnType").toUpperCase();
                JdbcType convertedType = targetDataTypes.get(type);
                if (convertedType != null) {
                    dateColumnsBuilder.put(rs.getString("columnName"), convertedType);
                }
            } while (rs.next());
        }
    }
    return dateColumnsBuilder.build();
}
Also used : ResultSet(java.sql.ResultSet) JdbcType(org.apache.gobblin.converter.jdbc.JdbcType) PreparedStatement(java.sql.PreparedStatement) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with JdbcType

use of org.apache.gobblin.converter.jdbc.JdbcType in project incubator-gobblin by apache.

the class AvroToJdbcEntryConverterInitializer method initialize.

/**
 * AvroToJdbcEntryConverter list of date columns existing in the table. As we don't want each converter
 * making a connection against database to get the same information. Here, ConverterInitializer will
 * retrieve it and store it into WorkUnit so that AvroToJdbcEntryConverter will use it later.
 *
 * {@inheritDoc}
 * @see org.apache.gobblin.initializer.Initializer#initialize()
 */
@Override
public void initialize() {
    String table = Preconditions.checkNotNull(this.state.getProp(ForkOperatorUtils.getPropertyNameForBranch(JdbcPublisher.JDBC_PUBLISHER_FINAL_TABLE_NAME, this.branches, this.branchId)));
    String db = Preconditions.checkNotNull(this.state.getProp(ForkOperatorUtils.getPropertyNameForBranch(JdbcPublisher.JDBC_PUBLISHER_DATABASE_NAME, this.branches, this.branchId)));
    try (Connection conn = createConnection()) {
        JdbcWriterCommands commands = this.jdbcWriterCommandsFactory.newInstance(this.state, conn);
        Map<String, JdbcType> dateColumnMapping = commands.retrieveDateColumns(db, table);
        LOG.info("Date column mapping: " + dateColumnMapping);
        final String dateFieldsKey = ForkOperatorUtils.getPropertyNameForBranch(AvroToJdbcEntryConverter.CONVERTER_AVRO_JDBC_DATE_FIELDS, this.branches, this.branchId);
        for (WorkUnit wu : this.workUnits) {
            wu.setProp(dateFieldsKey, new Gson().toJson(dateColumnMapping));
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JdbcWriterCommands(org.apache.gobblin.writer.commands.JdbcWriterCommands) Connection(java.sql.Connection) JdbcType(org.apache.gobblin.converter.jdbc.JdbcType) Gson(com.google.gson.Gson) WorkUnit(org.apache.gobblin.source.workunit.WorkUnit)

Example 3 with JdbcType

use of org.apache.gobblin.converter.jdbc.JdbcType in project incubator-gobblin by apache.

the class JdbcWriterCommandsTest method testMySqlDateTypeRetrieval.

@Test
public void testMySqlDateTypeRetrieval() throws SQLException {
    Connection conn = mock(Connection.class);
    PreparedStatement pstmt = mock(PreparedStatement.class);
    when(conn.prepareStatement(any(String.class))).thenReturn(pstmt);
    ResultSet rs = createMockResultSet();
    when(pstmt.executeQuery()).thenReturn(rs);
    MySqlWriterCommands writerCommands = new MySqlWriterCommands(new State(), conn);
    Map<String, JdbcType> actual = writerCommands.retrieveDateColumns("db", "users");
    ImmutableMap.Builder<String, JdbcType> builder = ImmutableMap.builder();
    builder.put("date_of_birth", JdbcType.DATE);
    builder.put("last_modified", JdbcType.TIME);
    builder.put("created", JdbcType.TIMESTAMP);
    Map<String, JdbcType> expected = builder.build();
    Assert.assertEquals(expected, actual);
}
Also used : State(org.apache.gobblin.configuration.State) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) JdbcType(org.apache.gobblin.converter.jdbc.JdbcType) PreparedStatement(java.sql.PreparedStatement) MySqlWriterCommands(org.apache.gobblin.writer.commands.MySqlWriterCommands) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 4 with JdbcType

use of org.apache.gobblin.converter.jdbc.JdbcType in project incubator-gobblin by apache.

the class MySqlWriterCommands method retrieveDateColumns.

/**
 * https://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html
 * {@inheritDoc}
 * @see org.apache.gobblin.writer.commands.JdbcWriterCommands#retrieveDateColumns(java.sql.Connection, java.lang.String)
 */
@Override
public Map<String, JdbcType> retrieveDateColumns(String database, String table) throws SQLException {
    Map<String, JdbcType> targetDataTypes = ImmutableMap.<String, JdbcType>builder().put("DATE", JdbcType.DATE).put("DATETIME", JdbcType.TIMESTAMP).put("TIME", JdbcType.TIME).put("TIMESTAMP", JdbcType.TIMESTAMP).build();
    ImmutableMap.Builder<String, JdbcType> dateColumnsBuilder = ImmutableMap.builder();
    try (PreparedStatement pstmt = this.conn.prepareStatement(INFORMATION_SCHEMA_SELECT_SQL_PSTMT)) {
        pstmt.setString(1, database);
        pstmt.setString(2, table);
        LOG.info("Retrieving column type information from SQL: " + pstmt);
        try (ResultSet rs = pstmt.executeQuery()) {
            if (!rs.first()) {
                throw new IllegalArgumentException("No result from information_schema.columns");
            }
            do {
                String type = rs.getString("column_type").toUpperCase();
                JdbcType convertedType = targetDataTypes.get(type);
                if (convertedType != null) {
                    dateColumnsBuilder.put(rs.getString("column_name"), convertedType);
                }
            } while (rs.next());
        }
    }
    return dateColumnsBuilder.build();
}
Also used : ResultSet(java.sql.ResultSet) JdbcType(org.apache.gobblin.converter.jdbc.JdbcType) PreparedStatement(java.sql.PreparedStatement) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

JdbcType (org.apache.gobblin.converter.jdbc.JdbcType)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Connection (java.sql.Connection)2 Gson (com.google.gson.Gson)1 SQLException (java.sql.SQLException)1 State (org.apache.gobblin.configuration.State)1 WorkUnit (org.apache.gobblin.source.workunit.WorkUnit)1 JdbcWriterCommands (org.apache.gobblin.writer.commands.JdbcWriterCommands)1 MySqlWriterCommands (org.apache.gobblin.writer.commands.MySqlWriterCommands)1 Test (org.testng.annotations.Test)1