Search in sources :

Example 1 with JDBCOutputInsertWriter

use of org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter 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());
    }
}
Also used : Connection(java.sql.Connection) IndexedRecord(org.apache.avro.generic.IndexedRecord) Statement(java.sql.Statement) Connection(java.sql.Connection) TJDBCRollbackDefinition(org.talend.components.jdbc.tjdbcrollback.TJDBCRollbackDefinition) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCConnectionDefinition(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition) JDBCOutputInsertWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter) JDBCSourceOrSink(org.talend.components.jdbc.runtime.JDBCSourceOrSink) TJDBCConnectionProperties(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties) JDBCRollbackSourceOrSink(org.talend.components.jdbc.runtime.JDBCRollbackSourceOrSink) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) TJDBCRollbackProperties(org.talend.components.jdbc.tjdbcrollback.TJDBCRollbackProperties) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) JDBCSink(org.talend.components.jdbc.runtime.JDBCSink) ResultSet(java.sql.ResultSet) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) Test(org.junit.Test)

Example 2 with JDBCOutputInsertWriter

use of org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter in project components by Talend.

the class JDBCOutputWriteOperation method createWriter.

@Override
public Writer<Result> createWriter(RuntimeContainer runtimeContainer) {
    RuntimeSettingProvider properties = ((JDBCSink) this.getSink()).properties;
    DataAction dataAction = properties.getRuntimeSetting().getDataAction();
    switch(dataAction) {
        case INSERT:
            return new JDBCOutputInsertWriter(this, runtimeContainer);
        case UPDATE:
            return new JDBCOutputUpdateWriter(this, runtimeContainer);
        case DELETE:
            return new JDBCOutputDeleteWriter(this, runtimeContainer);
        case INSERT_OR_UPDATE:
            return new JDBCOutputInsertOrUpdateWriter(this, runtimeContainer);
        case UPDATE_OR_INSERT:
            return new JDBCOutputUpdateOrInsertWriter(this, runtimeContainer);
        default:
            return null;
    }
}
Also used : JDBCOutputUpdateWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputUpdateWriter) DataAction(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties.DataAction) JDBCOutputInsertOrUpdateWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputInsertOrUpdateWriter) JDBCOutputInsertWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter) RuntimeSettingProvider(org.talend.components.jdbc.RuntimeSettingProvider) JDBCOutputDeleteWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputDeleteWriter) JDBCOutputUpdateOrInsertWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputUpdateOrInsertWriter)

Example 3 with JDBCOutputInsertWriter

use of org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter 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());
    }
}
Also used : JDBCCommitSourceOrSink(org.talend.components.jdbc.runtime.JDBCCommitSourceOrSink) Connection(java.sql.Connection) IndexedRecord(org.apache.avro.generic.IndexedRecord) Statement(java.sql.Statement) TJDBCCommitProperties(org.talend.components.jdbc.tjdbccommit.TJDBCCommitProperties) Connection(java.sql.Connection) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCConnectionDefinition(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition) JDBCOutputInsertWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter) JDBCSourceOrSink(org.talend.components.jdbc.runtime.JDBCSourceOrSink) TJDBCConnectionProperties(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) TJDBCCommitDefinition(org.talend.components.jdbc.tjdbccommit.TJDBCCommitDefinition) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) JDBCSink(org.talend.components.jdbc.runtime.JDBCSink) ResultSet(java.sql.ResultSet) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) Test(org.junit.Test)

Aggregations

JDBCOutputInsertWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 Test (org.junit.Test)2 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)2 JDBCSink (org.talend.components.jdbc.runtime.JDBCSink)2 JDBCSourceOrSink (org.talend.components.jdbc.runtime.JDBCSourceOrSink)2 TJDBCConnectionDefinition (org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition)2 TJDBCConnectionProperties (org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties)2 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)2 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)2 ValidationResult (org.talend.daikon.properties.ValidationResult)2 RuntimeSettingProvider (org.talend.components.jdbc.RuntimeSettingProvider)1 JDBCCommitSourceOrSink (org.talend.components.jdbc.runtime.JDBCCommitSourceOrSink)1 JDBCRollbackSourceOrSink (org.talend.components.jdbc.runtime.JDBCRollbackSourceOrSink)1 JDBCOutputDeleteWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputDeleteWriter)1 JDBCOutputInsertOrUpdateWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputInsertOrUpdateWriter)1 JDBCOutputUpdateOrInsertWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputUpdateOrInsertWriter)1