use of org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions in project flink-mirror by flink-ci.
the class JdbcOutputFormatTest method testJdbcOutputFormat.
@Test
public void testJdbcOutputFormat() throws IOException, SQLException {
JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(OUTPUT_TABLE).build();
JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(jdbcOptions.getTableName()).withDialect(jdbcOptions.getDialect()).withFieldNames(fieldNames).build();
outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(JdbcExecutionOptions.builder().build()).setRowDataTypeInfo(rowDataTypeInfo).build();
setRuntimeContext(outputFormat, true);
outputFormat.open(0, 1);
setRuntimeContext(outputFormat, true);
outputFormat.open(0, 1);
for (TestEntry entry : TEST_DATA) {
outputFormat.writeRecord(buildGenericData(entry.id, entry.title, entry.author, entry.price, entry.qty));
}
outputFormat.close();
try (Connection dbConn = DriverManager.getConnection(DERBY_EBOOKSHOP_DB.getUrl());
PreparedStatement statement = dbConn.prepareStatement(SELECT_ALL_NEWBOOKS);
ResultSet resultSet = statement.executeQuery()) {
int recordCount = 0;
while (resultSet.next()) {
assertEquals(TEST_DATA[recordCount].id, resultSet.getObject("id"));
assertEquals(TEST_DATA[recordCount].title, resultSet.getObject("title"));
assertEquals(TEST_DATA[recordCount].author, resultSet.getObject("author"));
assertEquals(TEST_DATA[recordCount].price, resultSet.getObject("price"));
assertEquals(TEST_DATA[recordCount].qty, resultSet.getObject("qty"));
recordCount++;
}
assertEquals(TEST_DATA.length, recordCount);
}
}
use of org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions in project flink-mirror by flink-ci.
the class JdbcOutputFormatTest method testInvalidDriver.
@Test
public void testInvalidDriver() {
String expectedMsg = "unable to open JDBC writer";
try {
JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName("org.apache.derby.jdbc.idontexist").setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(INPUT_TABLE).build();
JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(jdbcOptions.getTableName()).withDialect(jdbcOptions.getDialect()).withFieldNames(fieldNames).build();
outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(JdbcExecutionOptions.builder().build()).build();
outputFormat.open(0, 1);
fail("Expected exception is not thrown.");
} catch (Exception e) {
assertTrue(findThrowable(e, IOException.class).isPresent());
assertTrue(findThrowableWithMessage(e, expectedMsg).isPresent());
}
}
use of org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions in project flink-mirror by flink-ci.
the class JdbcOutputFormatTest method testIncompatibleTypes.
@Test
public void testIncompatibleTypes() {
try {
JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(INPUT_TABLE).build();
JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(jdbcOptions.getTableName()).withDialect(jdbcOptions.getDialect()).withFieldNames(fieldNames).build();
outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(JdbcExecutionOptions.builder().build()).setRowDataTypeInfo(rowDataTypeInfo).build();
setRuntimeContext(outputFormat, false);
outputFormat.open(0, 1);
RowData row = buildGenericData(4, "hello", "world", 0.99, "imthewrongtype");
outputFormat.writeRecord(row);
outputFormat.close();
fail("Expected exception is not thrown.");
} catch (Exception e) {
assertTrue(findThrowable(e, ClassCastException.class).isPresent());
}
}
use of org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions in project flink-mirror by flink-ci.
the class JdbcOutputFormatTest method testFlush.
@Test
public void testFlush() throws SQLException, IOException {
JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(OUTPUT_TABLE_2).build();
JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(jdbcOptions.getTableName()).withDialect(jdbcOptions.getDialect()).withFieldNames(fieldNames).build();
JdbcExecutionOptions executionOptions = JdbcExecutionOptions.builder().withBatchSize(3).build();
outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(executionOptions).setRowDataTypeInfo(rowDataTypeInfo).build();
setRuntimeContext(outputFormat, true);
outputFormat.open(0, 1);
try (Connection dbConn = DriverManager.getConnection(DERBY_EBOOKSHOP_DB.getUrl());
PreparedStatement statement = dbConn.prepareStatement(SELECT_ALL_NEWBOOKS_2)) {
outputFormat.open(0, 1);
for (int i = 0; i < 2; ++i) {
outputFormat.writeRecord(buildGenericData(TEST_DATA[i].id, TEST_DATA[i].title, TEST_DATA[i].author, TEST_DATA[i].price, TEST_DATA[i].qty));
}
try (ResultSet resultSet = statement.executeQuery()) {
assertFalse(resultSet.next());
}
outputFormat.writeRecord(buildGenericData(TEST_DATA[2].id, TEST_DATA[2].title, TEST_DATA[2].author, TEST_DATA[2].price, TEST_DATA[2].qty));
try (ResultSet resultSet = statement.executeQuery()) {
int recordCount = 0;
while (resultSet.next()) {
assertEquals(TEST_DATA[recordCount].id, resultSet.getObject("id"));
assertEquals(TEST_DATA[recordCount].title, resultSet.getObject("title"));
assertEquals(TEST_DATA[recordCount].author, resultSet.getObject("author"));
assertEquals(TEST_DATA[recordCount].price, resultSet.getObject("price"));
assertEquals(TEST_DATA[recordCount].qty, resultSet.getObject("qty"));
recordCount++;
}
assertEquals(3, recordCount);
}
} finally {
outputFormat.close();
}
}
use of org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions in project flink-mirror by flink-ci.
the class JdbcRowDataLookupFunctionTest method buildRowDataLookupFunction.
private JdbcRowDataLookupFunction buildRowDataLookupFunction(JdbcLookupOptions lookupOptions) {
JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DB_URL).setTableName(LOOKUP_TABLE).build();
RowType rowType = RowType.of(Arrays.stream(fieldDataTypes).map(DataType::getLogicalType).toArray(LogicalType[]::new), fieldNames);
JdbcRowDataLookupFunction lookupFunction = new JdbcRowDataLookupFunction(jdbcOptions, lookupOptions, fieldNames, fieldDataTypes, lookupKeys, rowType);
return lookupFunction;
}
Aggregations