Search in sources :

Example 11 with TJDBCOutputProperties

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

the class CommonUtilsTest method testGetMainSchemaFromInputConnector.

@Test
public void testGetMainSchemaFromInputConnector() {
    TJDBCOutputProperties properties = new TJDBCOutputProperties("output");
    properties.init();
    Schema schema = CommonUtils.getMainSchemaFromInputConnector(properties);
    Assert.assertNotNull(schema);
}
Also used : Schema(org.apache.avro.Schema) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) Test(org.junit.Test)

Example 12 with TJDBCOutputProperties

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

the class DBTestUtils method createCommonJDBCOutputProperties.

public static TJDBCOutputProperties createCommonJDBCOutputProperties(AllSetting allSetting, TJDBCOutputDefinition definition) {
    TJDBCOutputProperties properties = (TJDBCOutputProperties) definition.createRuntimeProperties();
    // TODO now framework doesn't support to load the JDBC jar by the setting
    // properties.connection.driverJar.setValue(null);
    properties.connection.driverClass.setValue(allSetting.getDriverClass());
    properties.connection.jdbcUrl.setValue(allSetting.getJdbcUrl());
    properties.connection.userPassword.userId.setValue(allSetting.getUsername());
    properties.connection.userPassword.password.setValue(allSetting.getPassword());
    return properties;
}
Also used : TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)

Example 13 with TJDBCOutputProperties

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

Example 14 with TJDBCOutputProperties

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

the class JDBCOutputTestIT method testDieOnError.

@Test(expected = ComponentException.class)
public void testDieOnError() 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.randomDataActionExceptDelete();
    properties.dataAction.setValue(action);
    properties.dieOnError.setValue(true);
    properties.useBatch.setValue(DBTestUtils.randomBoolean());
    properties.batchSize.setValue(DBTestUtils.randomInt());
    // we set it like this to avoid the dead lock when this case :
    // when die on error and not auto commit mode, we throw the exception, but not call commit or rollback in the finally
    // part, it may make the dead lock for derby
    // in all the javajet db components, we have this issue too, but different db, different result, in my view, we should
    // process
    // it, will create another test to show the dead lock issue for derby
    // or set value to 0 mean use the default commit mode, for derby, it's auto commit
    properties.commitEvery.setValue(null);
    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, "xiaoming");
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 2);
        r2.put(1, "too long value");
        writer.write(r2);
        writer.close();
        Assert.fail("should not run here");
    } catch (ComponentException e) {
        ExceptionContext context = e.getContext();
        Assert.assertTrue(context != null);
        String contextMessage = context.toString();
        Assert.assertTrue(contextMessage != null && !contextMessage.isEmpty());
        Assert.assertNotNull(e.getCause());
        throw e;
    } finally {
        writer.close();
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) Schema(org.apache.avro.Schema) ExceptionContext(org.talend.daikon.exception.ExceptionContext) ComponentException(org.talend.components.api.exception.ComponentException) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) DataAction(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties.DataAction) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCOutputWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputWriter) Test(org.junit.Test)

Example 15 with TJDBCOutputProperties

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

the class JDBCOutputTestIT method testUpdate.

@Test
public void testUpdate() 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.UPDATE);
    properties.dieOnError.setValue(true);
    String randomInfo = randomBatchAndCommit(properties);
    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);
        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(3));
    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(randomInfo, "gaoyan1", records.get(1).get(1));
    Assert.assertEquals(new Integer(3), records.get(2).get(0));
    Assert.assertEquals("dabao", records.get(2).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)

Aggregations

TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)18 Test (org.junit.Test)16 Schema (org.apache.avro.Schema)15 IndexedRecord (org.apache.avro.generic.IndexedRecord)15 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)15 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