use of org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry in project flink by apache.
the class JdbcAppendOnlyWriterTest method testMaxRetry.
@Test(expected = IOException.class)
public void testMaxRetry() throws Exception {
format = JdbcOutputFormat.builder().setOptions(JdbcConnectorOptions.builder().setDBUrl(getDbMetadata().getUrl()).setTableName(OUTPUT_TABLE).build()).setFieldNames(fieldNames).setKeyFields(null).build();
RuntimeContext context = Mockito.mock(RuntimeContext.class);
ExecutionConfig config = Mockito.mock(ExecutionConfig.class);
doReturn(config).when(context).getExecutionConfig();
doReturn(true).when(config).isObjectReuseEnabled();
format.setRuntimeContext(context);
format.open(0, 1);
// alter table schema to trigger retry logic after failure.
alterTable();
for (TestEntry entry : TEST_DATA) {
format.writeRecord(Tuple2.of(true, toRow(entry)));
}
// after retry default times, throws a BatchUpdateException.
format.flush();
}
use of org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry 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.JdbcTestFixture.TestEntry 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.JdbcTestFixture.TestEntry 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);
}
}
use of org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry in project flink by apache.
the class JdbcRowOutputFormatTest method testExceptionOnInvalidType.
@Test
public void testExceptionOnInvalidType() {
String expectedMsg = "field index: 3, field value: 0.";
try {
jdbcOutputFormat = JdbcRowOutputFormat.buildJdbcOutputFormat().setDrivername(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setQuery(String.format(INSERT_TEMPLATE, OUTPUT_TABLE)).setSqlTypes(new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.DOUBLE, Types.INTEGER }).finish();
setRuntimeContext(jdbcOutputFormat, true);
jdbcOutputFormat.open(0, 1);
TestEntry entry = TEST_DATA[0];
Row row = new Row(5);
row.setField(0, entry.id);
row.setField(1, entry.title);
row.setField(2, entry.author);
// use incompatible type (Long instead of Double)
row.setField(3, 0L);
row.setField(4, entry.qty);
jdbcOutputFormat.writeRecord(row);
jdbcOutputFormat.close();
} catch (Exception e) {
assertTrue(findThrowable(e, ClassCastException.class).isPresent());
assertTrue(findThrowableWithMessage(e, expectedMsg).isPresent());
}
}
Aggregations