Search in sources :

Example 6 with TJDBCOutputDefinition

use of org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition in project components by Talend.

the class JDBCOutputTestIT method testClearDataInTable.

@Test
public void testClearDataInTable() throws Exception {
    TJDBCOutputDefinition definition = new TJDBCOutputDefinition();
    TJDBCOutputProperties properties = DBTestUtils.createCommonJDBCOutputProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema2(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    DataAction action = DBTestUtils.randomDataAction();
    properties.dataAction.setValue(action);
    properties.dieOnError.setValue(DBTestUtils.randomBoolean());
    randomBatchAndCommit(properties);
    properties.clearDataInTable.setValue(true);
    JDBCOutputWriter writer = DBTestUtils.createCommonJDBCOutputWriter(definition, properties);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 4);
        r1.put(1, "xiaoming");
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 5);
        r2.put(1, "xiaobai");
        writer.write(r2);
        DBTestUtils.assertSuccessRecord(writer, r2);
        writer.close();
    } finally {
        writer.close();
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, schema, definition1, properties1);
    if (action == DataAction.INSERT || action == DataAction.INSERT_OR_UPDATE || action == DataAction.UPDATE_OR_INSERT) {
        assertThat(records, hasSize(2));
        Assert.assertEquals(new Integer(4), records.get(0).get(0));
        Assert.assertEquals("xiaoming", records.get(0).get(1));
        Assert.assertEquals(new Integer(5), records.get(1).get(0));
        Assert.assertEquals("xiaobai", records.get(1).get(1));
    } else {
        assertThat(records, hasSize(0));
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) Schema(org.apache.avro.Schema) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) DataAction(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties.DataAction) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCOutputWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputWriter) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 7 with TJDBCOutputDefinition

use of org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition in project components by Talend.

the class JDBCOutputTestIT method testBatch.

@Test
public void testBatch() 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);
    properties.useBatch.setValue(true);
    properties.batchSize.setValue(2);
    properties.commitEvery.setValue(3);
    JDBCOutputWriter writer = DBTestUtils.createCommonJDBCOutputWriter(definition, properties);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 4);
        r1.put(1, "xiaoming");
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 5);
        r2.put(1, "xiaobai");
        writer.write(r2);
        DBTestUtils.assertSuccessRecord(writer, r2);
        IndexedRecord r3 = new GenericData.Record(properties.main.schema.getValue());
        r3.put(0, 6);
        r3.put(1, "xiaohong");
        writer.write(r3);
        DBTestUtils.assertSuccessRecord(writer, r3);
        IndexedRecord r4 = new GenericData.Record(properties.main.schema.getValue());
        r4.put(0, 7);
        r4.put(1, "xiaored");
        writer.write(r4);
        DBTestUtils.assertSuccessRecord(writer, r4);
        IndexedRecord r5 = new GenericData.Record(properties.main.schema.getValue());
        r5.put(0, 8);
        r5.put(1, "xiaohei");
        writer.write(r5);
        DBTestUtils.assertSuccessRecord(writer, r5);
        writer.close();
    } finally {
        writer.close();
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, schema, definition1, properties1);
    assertThat(records, hasSize(8));
    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));
    Assert.assertEquals(new Integer(6), records.get(5).get(0));
    Assert.assertEquals("xiaohong", records.get(5).get(1));
    Assert.assertEquals(new Integer(7), records.get(6).get(0));
    Assert.assertEquals("xiaored", records.get(6).get(1));
    Assert.assertEquals(new Integer(8), records.get(7).get(0));
    Assert.assertEquals("xiaohei", records.get(7).get(1));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) Schema(org.apache.avro.Schema) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCOutputWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputWriter) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 8 with TJDBCOutputDefinition

use of org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition in project components by Talend.

the class JDBCOutputTestIT method testDeleteReject.

// TODO how to make a delete action fail?
@Test
public void testDeleteReject() throws Exception {
    TJDBCOutputDefinition definition = new TJDBCOutputDefinition();
    TJDBCOutputProperties properties = DBTestUtils.createCommonJDBCOutputProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema2(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.dataAction.setValue(DataAction.DELETE);
    // reject function can't work with batch function
    properties.useBatch.setValue(false);
    properties.commitEvery.setValue(DBTestUtils.randomInt());
    JDBCOutputWriter writer = DBTestUtils.createCommonJDBCOutputWriter(definition, properties);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 1);
        r1.put(1, "wangwei1");
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 4);
        r2.put(1, "newkey");
        writer.write(r2);
        DBTestUtils.assertSuccessRecord(writer, r2);
        IndexedRecord r3 = new GenericData.Record(properties.main.schema.getValue());
        r3.put(0, 2);
        r3.put(1, "gaoyan1");
        writer.write(r3);
        DBTestUtils.assertSuccessRecord(writer, r3);
        IndexedRecord r4 = new GenericData.Record(properties.main.schema.getValue());
        r4.put(0, 5);
        r4.put(1, "the line is not rejected though it's too long as only key is used by deleting action");
        writer.write(r4);
        DBTestUtils.assertSuccessRecord(writer, r4);
        IndexedRecord r5 = new GenericData.Record(properties.main.schema.getValue());
        r5.put(0, 3);
        r5.put(1, "dabao1");
        writer.write(r5);
        DBTestUtils.assertSuccessRecord(writer, r5);
        writer.close();
    } finally {
        writer.close();
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, schema, definition1, properties1);
    assertThat(records, hasSize(0));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) Schema(org.apache.avro.Schema) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCOutputWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputWriter) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 9 with TJDBCOutputDefinition

use of org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition in project components by Talend.

the class JDBCOutputTestIT method testInsertOrUpdate.

@Test
public void testInsertOrUpdate() throws Exception {
    TJDBCOutputDefinition definition = new TJDBCOutputDefinition();
    TJDBCOutputProperties properties = DBTestUtils.createCommonJDBCOutputProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema2(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.dataAction.setValue(DataAction.INSERT_OR_UPDATE);
    properties.dieOnError.setValue(true);
    properties.commitEvery.setValue(DBTestUtils.randomInt());
    JDBCOutputWriter writer = DBTestUtils.createCommonJDBCOutputWriter(definition, properties);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 1);
        r1.put(1, "wangwei1");
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 2);
        r2.put(1, "gaoyan1");
        writer.write(r2);
        DBTestUtils.assertSuccessRecord(writer, r2);
        IndexedRecord r3 = new GenericData.Record(properties.main.schema.getValue());
        r3.put(0, 4);
        r3.put(1, "new one");
        writer.write(r3);
        DBTestUtils.assertSuccessRecord(writer, r3);
        writer.close();
    } finally {
        writer.close();
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, schema, definition1, properties1);
    assertThat(records, hasSize(4));
    Assert.assertEquals(new Integer(1), records.get(0).get(0));
    Assert.assertEquals("wangwei1", records.get(0).get(1));
    Assert.assertEquals(new Integer(2), records.get(1).get(0));
    Assert.assertEquals("gaoyan1", records.get(1).get(1));
    Assert.assertEquals(new Integer(3), records.get(2).get(0));
    Assert.assertEquals("dabao", records.get(2).get(1));
    Assert.assertEquals(new Integer(4), records.get(3).get(0));
    Assert.assertEquals("new one", records.get(3).get(1));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) Schema(org.apache.avro.Schema) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCOutputWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputWriter) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 10 with TJDBCOutputDefinition

use of org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition 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

IndexedRecord (org.apache.avro.generic.IndexedRecord)15 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)15 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)15 Test (org.junit.Test)14 Schema (org.apache.avro.Schema)13 JDBCOutputWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputWriter)12 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)12 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)12 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)3 JDBCSink (org.talend.components.jdbc.runtime.JDBCSink)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 JDBCSourceOrSink (org.talend.components.jdbc.runtime.JDBCSourceOrSink)2 JDBCOutputInsertWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter)2 TJDBCConnectionDefinition (org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition)2 TJDBCConnectionProperties (org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties)2 DataAction (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties.DataAction)2 ValidationResult (org.talend.daikon.properties.ValidationResult)2 IOException (java.io.IOException)1