Search in sources :

Example 6 with TJDBCRowDefinition

use of org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition in project components by Talend.

the class JdbcRowTestIT method test_basic_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_basic_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from " + tablename);
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSource source = (JDBCRowSource) sandboxedInstance.getInstance();
        source.initialize(null, properties);
        ValidationResult result = source.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        Reader reader = source.createReader(null);
        try {
            reader.start();
            IndexedRecord row = (IndexedRecord) reader.getCurrent();
            ResultSet resultSet = (ResultSet) row.get(0);
            resultSet.next();
            Assert.assertEquals(1, resultSet.getInt(1));
            Assert.assertEquals("wangwei", resultSet.getString(2));
            resultSet.next();
            Assert.assertEquals(2, resultSet.getInt(1));
            Assert.assertEquals("gaoyan", resultSet.getString(2));
            resultSet.next();
            Assert.assertEquals(3, resultSet.getInt(1));
            Assert.assertEquals("dabao", resultSet.getString(2));
            resultSet.close();
            // only output one row when it works as a input component
            Assert.assertFalse(reader.advance());
            reader.close();
        } finally {
            reader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 7 with TJDBCRowDefinition

use of org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition 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"));
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.INCOMING_AND_OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSink sink = (JDBCRowSink) sandboxedInstance.getInstance();
        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) {
            Assert.assertNotNull(e.getCause());
        } finally {
            writer.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) 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) 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)

Example 8 with TJDBCRowDefinition

use of org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition in project components by Talend.

the class JdbcRowTestIT method test_basic_no_connector.

@Test
public void test_basic_no_connector() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(4, 'momo')");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.NONE), properties.getClass().getClassLoader())) {
        JDBCRowSourceOrSink sourceOrSink = (JDBCRowSourceOrSink) sandboxedInstance.getInstance();
        sourceOrSink.initialize(null, properties);
        ValidationResult result = sourceOrSink.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, DBTestUtils.createTestSchema(tablename), definition1, properties1);
    assertThat(records, hasSize(4));
    Assert.assertEquals(new Integer(4), records.get(3).get(0));
    Assert.assertEquals("momo", records.get(3).get(1));
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCRowSourceOrSink(org.talend.components.jdbc.runtime.JDBCRowSourceOrSink) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 9 with TJDBCRowDefinition

use of org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition in project components by Talend.

the class JdbcRowTestIT method test_use_preparedstatement_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_use_preparedstatement_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(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);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    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(1));
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSource source = (JDBCRowSource) sandboxedInstance.getInstance();
        source.initialize(null, properties);
        ValidationResult result = source.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        Reader reader = source.createReader(null);
        try {
            reader.start();
            IndexedRecord row = (IndexedRecord) reader.getCurrent();
            ResultSet resultSet = (ResultSet) row.get(0);
            resultSet.next();
            Assert.assertEquals(1, resultSet.getInt(1));
            Assert.assertEquals("wangwei", resultSet.getString(2));
            resultSet.close();
            // only output one row when it works as a input component
            Assert.assertFalse(reader.advance());
            reader.close();
        } finally {
            reader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 10 with TJDBCRowDefinition

use of org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition in project components by Talend.

the class JdbcRowTestIT method test_die_on_error_no_connector.

@Test
public void test_die_on_error_no_connector() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(4, 'a too long value')");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.NONE), properties.getClass().getClassLoader())) {
        JDBCRowSourceOrSink sourceOrSink = (JDBCRowSourceOrSink) sandboxedInstance.getInstance();
        sourceOrSink.initialize(null, properties);
        ValidationResult result = sourceOrSink.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.ERROR);
        Assert.assertNotNull(result.getMessage());
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) JDBCRowSourceOrSink(org.talend.components.jdbc.runtime.JDBCRowSourceOrSink) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)21 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)21 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)21 ValidationResult (org.talend.daikon.properties.ValidationResult)19 IndexedRecord (org.apache.avro.generic.IndexedRecord)17 Schema (org.apache.avro.Schema)15 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)10 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)7 JDBCRowSink (org.talend.components.jdbc.runtime.JDBCRowSink)7 JDBCRowWriter (org.talend.components.jdbc.runtime.writer.JDBCRowWriter)7 ResultSet (java.sql.ResultSet)6 Reader (org.talend.components.api.component.runtime.Reader)6 JDBCRowSource (org.talend.components.jdbc.runtime.JDBCRowSource)6 JDBCRowSourceOrSink (org.talend.components.jdbc.runtime.JDBCRowSourceOrSink)6 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)5 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)5 Map (java.util.Map)2 ComponentException (org.talend.components.api.exception.ComponentException)2 DataRejectException (org.talend.components.api.exception.DataRejectException)2 ExceptionContext (org.talend.daikon.exception.ExceptionContext)1