Search in sources :

Example 11 with JDBCOutputWriter

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

the class JDBCOutputTestIT method testDelete.

@Test
public void testDelete() 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);
    properties.dieOnError.setValue(true);
    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);
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 2);
        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(1));
    Assert.assertEquals(new Integer(3), records.get(0).get(0));
    Assert.assertEquals("dabao", records.get(0).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 12 with JDBCOutputWriter

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

the class JDBCOutputTestIT method testInsert.

@Test
public void testInsert() 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);
    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);
    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));
}
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 13 with JDBCOutputWriter

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

the class JDBCOutputTestIT method testUpdateReject.

@Test
public void testUpdateReject() 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);
    // 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, "the line should be rejected as it's too long");
        writer.write(r1);
        DBTestUtils.assertRejectRecord(writer);
        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 the key is not found in the table");
        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(3));
    Assert.assertEquals(new Integer(1), records.get(0).get(0));
    Assert.assertEquals("wangwei", 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("dabao1", 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

JDBCOutputWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputWriter)13 Schema (org.apache.avro.Schema)12 IndexedRecord (org.apache.avro.generic.IndexedRecord)12 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)12 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)12 Test (org.junit.Test)11 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)11 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)11 DataAction (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties.DataAction)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 SQLException (java.sql.SQLException)1 Reader (org.talend.components.api.component.runtime.Reader)1 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)1 ComponentException (org.talend.components.api.exception.ComponentException)1 JDBCSink (org.talend.components.jdbc.runtime.JDBCSink)1 ExceptionContext (org.talend.daikon.exception.ExceptionContext)1