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();
}
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);
}
}
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);
}
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();
}
Aggregations