Search in sources :

Example 6 with JDBCRowWriter

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

the class JDBCRowTestIT method commonAssert.

private void commonAssert(TJDBCRowProperties properties, Schema schema) throws IOException {
    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);
        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("momo", records.get(3).get(1));
    Assert.assertEquals(new Integer(4), records.get(4).get(0));
    Assert.assertEquals("momo", records.get(4).get(1));
}
Also used : WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)

Example 7 with JDBCRowWriter

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

the class JDBCRowTestIT method test_propagate_query_result_set_as_output.

@SuppressWarnings("rawtypes")
@Test
public void test_propagate_query_result_set_as_output() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema5(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from " + tablename + " where id = ?");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(3));
    properties.propagateQueryResultSet.setValue(true);
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(2).toString());
    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);
        assertThat(writer.getRejectedWrites(), empty());
        List<IndexedRecord> successfulWrites = writer.getSuccessfulWrites();
        assertThat(successfulWrites, hasSize(1));
        IndexedRecord successRecord = successfulWrites.get(0);
        Assert.assertEquals(4, successRecord.get(0));
        Assert.assertEquals("xiaoming", successRecord.get(1));
        ResultSet resultSet = (ResultSet) successRecord.get(2);
        resultSet.next();
        Assert.assertEquals(3, resultSet.getInt(1));
        Assert.assertEquals("dabao", resultSet.getString(2));
        writer.cleanWrites();
        resultSet.close();
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 5);
        r2.put(1, "xiaobai");
        writer.write(r2);
        assertThat(writer.getRejectedWrites(), empty());
        successfulWrites = writer.getSuccessfulWrites();
        assertThat(successfulWrites, hasSize(1));
        successRecord = successfulWrites.get(0);
        Assert.assertEquals(5, successRecord.get(0));
        Assert.assertEquals("xiaobai", successRecord.get(1));
        resultSet = (ResultSet) successRecord.get(2);
        resultSet.next();
        Assert.assertEquals(3, resultSet.getInt(1));
        Assert.assertEquals("dabao", resultSet.getString(2));
        writer.cleanWrites();
        resultSet.close();
        writer.close();
    } 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) ResultSet(java.sql.ResultSet) 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)

Example 8 with JDBCRowWriter

use of org.talend.components.jdbc.runtime.writer.JDBCRowWriter 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

IndexedRecord (org.apache.avro.generic.IndexedRecord)8 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)8 JDBCRowSink (org.talend.components.jdbc.runtime.JDBCRowSink)8 JDBCRowWriter (org.talend.components.jdbc.runtime.writer.JDBCRowWriter)8 ValidationResult (org.talend.daikon.properties.ValidationResult)8 Schema (org.apache.avro.Schema)7 Test (org.junit.Test)7 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)7 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)7 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)4 ResultSet (java.sql.ResultSet)2 ComponentException (org.talend.components.api.exception.ComponentException)2 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)2 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)2 ExceptionContext (org.talend.daikon.exception.ExceptionContext)1