use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.
the class JdbcDynamicTableFactoryTest method testJdbcSinkProperties.
@Test
public void testJdbcSinkProperties() {
Map<String, String> properties = getAllOptions();
properties.put("sink.buffer-flush.max-rows", "1000");
properties.put("sink.buffer-flush.interval", "2min");
properties.put("sink.max-retries", "5");
DynamicTableSink actual = createTableSink(SCHEMA, properties);
JdbcConnectorOptions options = JdbcConnectorOptions.builder().setDBUrl("jdbc:derby:memory:mydb").setTableName("mytable").build();
JdbcExecutionOptions executionOptions = JdbcExecutionOptions.builder().withBatchSize(1000).withBatchIntervalMs(120_000).withMaxRetries(5).build();
JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(options.getTableName()).withDialect(options.getDialect()).withFieldNames(SCHEMA.getColumnNames().toArray(new String[0])).withKeyFields("bbb", "aaa").build();
JdbcDynamicTableSink expected = new JdbcDynamicTableSink(options, executionOptions, dmlOptions, SCHEMA.toPhysicalRowDataType());
assertEquals(expected, actual);
}
use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.
the class JdbcOutputFormatTest method testExceptionOnClose.
@Test
public void testExceptionOnClose() {
String expectedMsg = "Writing records to JDBC failed.";
try {
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);
TestEntry entry = TEST_DATA[0];
RowData row = buildGenericData(entry.id, entry.title, entry.author, entry.price, entry.qty);
outputFormat.writeRecord(row);
outputFormat.writeRecord(// writing the same record twice must yield a unique key violation.
row);
outputFormat.close();
fail("Expected exception is not thrown.");
} catch (Exception e) {
assertTrue(findThrowable(e, RuntimeException.class).isPresent());
assertTrue(findThrowableWithMessage(e, expectedMsg).isPresent());
}
}
use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.
the class JdbcOutputFormatTest method testExceptionOnInvalidType.
@Test
public void testExceptionOnInvalidType() {
try {
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, false);
outputFormat.open(0, 1);
TestEntry entry = TEST_DATA[0];
RowData row = buildGenericData(entry.id, entry.title, entry.author, 0L, entry.qty);
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.JdbcDmlOptions in project flink by apache.
the class JdbcOutputFormatTest method testInvalidURL.
@Test
public void testInvalidURL() {
try {
JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl("jdbc:der:iamanerror:mory:ebookshop").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, IllegalStateException.class).isPresent());
}
}
use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.
the class JdbcOutputFormatTest method testInvalidConnectionInJdbcOutputFormat.
@Test
public void testInvalidConnectionInJdbcOutputFormat() throws IOException, SQLException {
JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(OUTPUT_TABLE_3).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);
// write records
for (int i = 0; i < 3; i++) {
TestEntry entry = TEST_DATA[i];
outputFormat.writeRecord(buildGenericData(entry.id, entry.title, entry.author, entry.price, entry.qty));
}
// close connection
outputFormat.getConnection().close();
// continue to write rest records
for (int i = 3; i < TEST_DATA.length; i++) {
TestEntry entry = TEST_DATA[i];
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_3);
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);
}
}
Aggregations