use of org.talend.components.jdbc.runtime.JDBCSink in project components by Talend.
the class JDBCRollbackTestIT method testRollback.
@SuppressWarnings("rawtypes")
@Test
public void testRollback() throws IOException, ClassNotFoundException, SQLException {
// connection part
TJDBCConnectionDefinition connectionDefinition = new TJDBCConnectionDefinition();
TJDBCConnectionProperties connectionProperties = DBTestUtils.createCommonJDBCConnectionProperties(allSetting, connectionDefinition);
JDBCSourceOrSink sourceOrSink = new JDBCSourceOrSink();
sourceOrSink.initialize(null, connectionProperties);
ValidationResult result = sourceOrSink.validate(container);
assertTrue(result.getStatus() == ValidationResult.Result.OK);
// output part
TJDBCOutputDefinition outputDefinition = new TJDBCOutputDefinition();
TJDBCOutputProperties outputProperties = (TJDBCOutputProperties) outputDefinition.createRuntimeProperties();
outputProperties.main.schema.setValue(DBTestUtils.createTestSchema(tablename));
outputProperties.updateOutputSchemas();
outputProperties.tableSelection.tablename.setValue(tablename);
outputProperties.dataAction.setValue(DataAction.INSERT);
outputProperties.referencedComponent.componentInstanceId.setValue(refComponentId);
outputProperties.referencedComponent.setReference(connectionProperties);
JDBCSink sink = new JDBCSink();
sink.initialize(container, outputProperties);
WriteOperation writerOperation = sink.createWriteOperation();
writerOperation.initialize(container);
JDBCOutputInsertWriter writer = (JDBCOutputInsertWriter) writerOperation.createWriter(container);
try {
writer.open("wid");
IndexedRecord r1 = new GenericData.Record(outputProperties.main.schema.getValue());
r1.put(0, 4);
r1.put(1, "xiaoming");
writer.write(r1);
DBTestUtils.assertSuccessRecord(writer, r1);
IndexedRecord r2 = new GenericData.Record(outputProperties.main.schema.getValue());
r2.put(0, 5);
r2.put(1, "xiaobai");
writer.write(r2);
DBTestUtils.assertSuccessRecord(writer, r2);
writer.close();
} finally {
writer.close();
}
// rollback part
TJDBCRollbackDefinition rollbackDefinition = new TJDBCRollbackDefinition();
TJDBCRollbackProperties rollbackProperties = (TJDBCRollbackProperties) rollbackDefinition.createRuntimeProperties();
rollbackProperties.referencedComponent.componentInstanceId.setValue(refComponentId);
rollbackProperties.closeConnection.setValue(false);
JDBCRollbackSourceOrSink rollbackSourceOrSink = new JDBCRollbackSourceOrSink();
rollbackSourceOrSink.initialize(container, rollbackProperties);
rollbackSourceOrSink.validate(container);
// create another session and check if the data is inserted
int count = -1;
try (Connection conn = JdbcRuntimeUtils.createConnection(allSetting);
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery("select count(*) from " + tablename)) {
if (resultset.next()) {
count = resultset.getInt(1);
}
}
Assert.assertEquals(3, count);
try (java.sql.Connection refConnection = (java.sql.Connection) container.getComponentData(ComponentConstants.CONNECTION_KEY, refComponentId)) {
assertTrue(refConnection != null);
Assert.assertTrue(!refConnection.isClosed());
}
}
use of org.talend.components.jdbc.runtime.JDBCSink in project components by Talend.
the class JDBCOutputTestIT method testInsertWithDataSupplier.
@Test
public void testInsertWithDataSupplier() throws Exception {
TJDBCOutputDefinition definition = new TJDBCOutputDefinition();
TJDBCOutputProperties properties = DBTestUtils.createCommonJDBCOutputProperties(allSetting, definition);
Schema schema = DBTestUtils.createTestSchema(tablename);
properties.main.schema.setValue(schema);
properties.updateOutputSchemas();
properties.tableSelection.tablename.setValue(tablename);
properties.dataAction.setValue(DataAction.INSERT);
properties.dieOnError.setValue(true);
randomBatchAndCommit(properties);
JDBCSink sink = new JDBCSink();
sink.initialize(null, properties);
WriteOperation writerOperation = sink.createWriteOperation();
Supplier<IndexedRecord> indexRecordSupplier = createDataSupplier(properties.main.schema.getValue());
new WriterDataSupplier<>(writerOperation, indexRecordSupplier, null).writeData();
;
TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, schema, definition1, properties1);
assertThat(records, hasSize(5));
Assert.assertEquals(new Integer(4), records.get(3).get(0));
Assert.assertEquals("xiaoming", records.get(3).get(1));
Assert.assertEquals(new Integer(5), records.get(4).get(0));
Assert.assertEquals("xiaobai", records.get(4).get(1));
}
use of org.talend.components.jdbc.runtime.JDBCSink in project components by Talend.
the class DBTestUtils method createCommonJDBCOutputWriter.
@SuppressWarnings("rawtypes")
public static JDBCOutputWriter createCommonJDBCOutputWriter(TJDBCOutputDefinition definition, TJDBCOutputProperties properties, RuntimeContainer container) {
JDBCSink sink = new JDBCSink();
sink.initialize(container, properties);
WriteOperation writerOperation = sink.createWriteOperation();
writerOperation.initialize(container);
JDBCOutputWriter writer = (JDBCOutputWriter) writerOperation.createWriter(container);
return writer;
}
use of org.talend.components.jdbc.runtime.JDBCSink in project components by Talend.
the class JDBCCommitTestIT method testCommit.
@SuppressWarnings("rawtypes")
@Test
public void testCommit() throws IOException, ClassNotFoundException, SQLException {
// connection part
TJDBCConnectionDefinition connectionDefinition = new TJDBCConnectionDefinition();
TJDBCConnectionProperties connectionProperties = DBTestUtils.createCommonJDBCConnectionProperties(allSetting, connectionDefinition);
JDBCSourceOrSink sourceOrSink = new JDBCSourceOrSink();
sourceOrSink.initialize(null, connectionProperties);
ValidationResult result = sourceOrSink.validate(container);
assertTrue(result.getStatus() == ValidationResult.Result.OK);
// output part
TJDBCOutputDefinition outputDefinition = new TJDBCOutputDefinition();
TJDBCOutputProperties outputProperties = (TJDBCOutputProperties) outputDefinition.createRuntimeProperties();
outputProperties.main.schema.setValue(DBTestUtils.createTestSchema(tablename));
outputProperties.updateOutputSchemas();
outputProperties.tableSelection.tablename.setValue(tablename);
outputProperties.dataAction.setValue(DataAction.INSERT);
outputProperties.referencedComponent.componentInstanceId.setValue(refComponentId);
outputProperties.referencedComponent.setReference(connectionProperties);
JDBCSink sink = new JDBCSink();
sink.initialize(container, outputProperties);
WriteOperation writerOperation = sink.createWriteOperation();
writerOperation.initialize(container);
JDBCOutputInsertWriter writer = (JDBCOutputInsertWriter) writerOperation.createWriter(container);
try {
writer.open("wid");
IndexedRecord r1 = new GenericData.Record(outputProperties.main.schema.getValue());
r1.put(0, 4);
r1.put(1, "xiaoming");
writer.write(r1);
DBTestUtils.assertSuccessRecord(writer, r1);
IndexedRecord r2 = new GenericData.Record(outputProperties.main.schema.getValue());
r2.put(0, 5);
r2.put(1, "xiaobai");
writer.write(r2);
DBTestUtils.assertSuccessRecord(writer, r2);
writer.close();
} finally {
writer.close();
}
// commit part
TJDBCCommitDefinition commitDefinition = new TJDBCCommitDefinition();
TJDBCCommitProperties commitProperties = (TJDBCCommitProperties) commitDefinition.createRuntimeProperties();
commitProperties.referencedComponent.componentInstanceId.setValue(refComponentId);
commitProperties.closeConnection.setValue(false);
JDBCCommitSourceOrSink commitSourceOrSink = new JDBCCommitSourceOrSink();
commitSourceOrSink.initialize(container, commitProperties);
commitSourceOrSink.validate(container);
int count = -1;
// create another session and check if the data is inserted
try (Connection conn = JdbcRuntimeUtils.createConnection(allSetting);
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery("select count(*) from " + tablename)) {
if (resultset.next()) {
count = resultset.getInt(1);
}
}
Assert.assertEquals(5, count);
try (java.sql.Connection refConnection = (java.sql.Connection) container.getComponentData(ComponentConstants.CONNECTION_KEY, refComponentId)) {
assertTrue(refConnection != null);
Assert.assertTrue(!refConnection.isClosed());
}
}
Aggregations