Search in sources :

Example 16 with WriteOperation

use of org.talend.components.api.component.runtime.WriteOperation in project components by Talend.

the class JDBCRowTestIT method test_die_on_error_as_output.

@SuppressWarnings("rawtypes")
@Test
public void test_die_on_error_as_output() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(?,?)");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1, 2));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name(), PreparedStatementTable.Type.String.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(4, "a too long value"));
    JDBCRowSink sink = new JDBCRowSink();
    sink.initialize(null, properties);
    ValidationResult result = sink.validate(null);
    Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    WriteOperation operation = sink.createWriteOperation();
    JDBCRowWriter writer = (JDBCRowWriter) operation.createWriter(null);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 4);
        r1.put(1, "xiaoming");
        writer.write(r1);
        writer.close();
    } 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());
    } finally {
        writer.close();
    }
}
Also used : TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ExceptionContext(org.talend.daikon.exception.ExceptionContext) ComponentException(org.talend.components.api.exception.ComponentException) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Aggregations

WriteOperation (org.talend.components.api.component.runtime.WriteOperation)16 IndexedRecord (org.apache.avro.generic.IndexedRecord)14 ValidationResult (org.talend.daikon.properties.ValidationResult)13 Test (org.junit.Test)12 Schema (org.apache.avro.Schema)9 JDBCRowSink (org.talend.components.jdbc.runtime.JDBCRowSink)8 JDBCRowWriter (org.talend.components.jdbc.runtime.writer.JDBCRowWriter)8 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)7 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)7 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)5 ResultSet (java.sql.ResultSet)4 JDBCSink (org.talend.components.jdbc.runtime.JDBCSink)4 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)3 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)3 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)3 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)3 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 Sink (org.talend.components.api.component.runtime.Sink)2 ComponentException (org.talend.components.api.exception.ComponentException)2