Search in sources :

Example 1 with DataAction

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

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

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

Aggregations

DataAction (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties.DataAction)3 Schema (org.apache.avro.Schema)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 Test (org.junit.Test)2 JDBCOutputWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputWriter)2 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)2 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)2 ComponentException (org.talend.components.api.exception.ComponentException)1 RuntimeSettingProvider (org.talend.components.jdbc.RuntimeSettingProvider)1 JDBCOutputDeleteWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputDeleteWriter)1 JDBCOutputInsertOrUpdateWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputInsertOrUpdateWriter)1 JDBCOutputInsertWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter)1 JDBCOutputUpdateOrInsertWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputUpdateOrInsertWriter)1 JDBCOutputUpdateWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputUpdateWriter)1 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)1 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)1 ExceptionContext (org.talend.daikon.exception.ExceptionContext)1