use of org.talend.components.jdbc.runtime.writer.JDBCOutputWriter 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));
}
use of org.talend.components.jdbc.runtime.writer.JDBCOutputWriter 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));
}
use of org.talend.components.jdbc.runtime.writer.JDBCOutputWriter in project components by Talend.
the class DBTestUtils method createCommonJDBCOutputWriter.
@SuppressWarnings("rawtypes")
public static JDBCOutputWriter createCommonJDBCOutputWriter(TJDBCOutputDefinition definition, TJDBCOutputProperties properties, RuntimeContainer container) {
JDBCSink sink = new JDBCSink();
sink.initialize(container, properties);
WriteOperation writerOperation = sink.createWriteOperation();
writerOperation.initialize(container);
JDBCOutputWriter writer = (JDBCOutputWriter) writerOperation.createWriter(container);
return writer;
}
use of org.talend.components.jdbc.runtime.writer.JDBCOutputWriter 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();
}
}
use of org.talend.components.jdbc.runtime.writer.JDBCOutputWriter 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));
}
Aggregations