Search in sources :

Example 16 with Reader

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

the class JDBCInputTestIT method testReader.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testReader() {
    Reader reader = null;
    try {
        TJDBCInputDefinition definition = new TJDBCInputDefinition();
        TJDBCInputProperties properties = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition);
        properties.main.schema.setValue(DBTestUtils.createTestSchema(tablename));
        properties.tableSelection.tablename.setValue(tablename);
        properties.sql.setValue(DBTestUtils.getSQL(tablename));
        reader = DBTestUtils.createCommonJDBCInputReader(properties);
        reader.start();
        IndexedRecord row = (IndexedRecord) reader.getCurrent();
        Integer id = (Integer) row.get(0);
        String name = (String) row.get(1);
        assertEquals(1, id.intValue());
        assertEquals("wangwei", name);
        reader.advance();
        row = (IndexedRecord) reader.getCurrent();
        id = (Integer) row.get(0);
        name = (String) row.get(1);
        assertEquals(2, id.intValue());
        assertEquals("gaoyan", name);
        reader.advance();
        row = (IndexedRecord) reader.getCurrent();
        id = (Integer) row.get(0);
        name = (String) row.get(1);
        assertEquals(3, id.intValue());
        assertEquals("dabao", name);
        reader.close();
        Map<String, Object> returnMap = reader.getReturnValues();
        Assert.assertEquals(3, returnMap.get(ComponentDefinition.RETURN_TOTAL_RECORD_COUNT));
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                Assert.fail(e.getMessage());
            }
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCInputReader(org.talend.components.jdbc.runtime.reader.JDBCInputReader) Reader(org.talend.components.api.component.runtime.Reader) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) IOException(java.io.IOException) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) SQLException(java.sql.SQLException) ComponentException(org.talend.components.api.exception.ComponentException) IOException(java.io.IOException) Test(org.junit.Test)

Example 17 with Reader

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

the class SalesforceRuntimeTestUtil method initReader.

public Reader initReader(TSalesforceBulkExecDefinition definition, String data_file, TSalesforceBulkExecProperties modelProperties, Schema schema, Schema output) {
    modelProperties.connection.userPassword.userId.setValue(username);
    modelProperties.connection.userPassword.password.setValue(password);
    modelProperties.connection.userPassword.securityKey.setValue(securityKey);
    modelProperties.connection.timeout.setValue(60000);
    modelProperties.connection.bulkConnection.setValue(true);
    modelProperties.bulkFilePath.setValue(data_file);
    modelProperties.bulkProperties.bytesToCommit.setValue(10 * 1024 * 1024);
    modelProperties.bulkProperties.rowsToCommit.setValue(10000);
    modelProperties.bulkProperties.concurrencyMode.setValue(Concurrency.Parallel);
    modelProperties.bulkProperties.waitTimeCheckBatchState.setValue(10000);
    modelProperties.module.moduleName.setValue(module);
    modelProperties.module.main.schema.setValue(schema);
    modelProperties.schemaFlow.schema.setValue(output);
    Source source = new SalesforceSource();
    source.initialize(null, modelProperties);
    ValidationResult vr = source.validate(null);
    if (vr.getStatus() == ValidationResult.Result.ERROR) {
        Assert.fail(vr.getMessage());
    }
    Reader reader = source.createReader(null);
    return reader;
}
Also used : SalesforceSource(org.talend.components.salesforce.runtime.SalesforceSource) Reader(org.talend.components.api.component.runtime.Reader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ValidationResult(org.talend.daikon.properties.ValidationResult) Source(org.talend.components.api.component.runtime.Source) SalesforceSource(org.talend.components.salesforce.runtime.SalesforceSource)

Example 18 with Reader

use of org.talend.components.api.component.runtime.Reader 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 19 with Reader

use of org.talend.components.api.component.runtime.Reader 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 20 with Reader

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

the class JdbcRowTestIT method test_reject_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_reject_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 notexists");
    properties.dieOnError.setValue(false);
    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();
            reader.getCurrent();
            // should go to the exception before current statement
            Assert.fail();
            reader.advance();
            reader.close();
        } catch (DataRejectException e) {
            Map<String, Object> info = e.getRejectInfo();
            IndexedRecord data = (IndexedRecord) info.get("talend_record");
            Assert.assertNull(data.get(0));
            Assert.assertNotNull(data.get(1));
            Assert.assertNotNull(data.get(2));
        } finally {
            reader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException) Schema(org.apache.avro.Schema) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) Map(java.util.Map) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Aggregations

Reader (org.talend.components.api.component.runtime.Reader)35 IndexedRecord (org.apache.avro.generic.IndexedRecord)31 Test (org.junit.Test)28 IOException (java.io.IOException)10 Schema (org.apache.avro.Schema)9 SalesforceInputProperties (org.talend.components.salesforce.dataprep.SalesforceInputProperties)9 ValidationResult (org.talend.daikon.properties.ValidationResult)9 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 DataRejectException (org.talend.components.api.exception.DataRejectException)6 JDBCRowSource (org.talend.components.jdbc.runtime.JDBCRowSource)6 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)6 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)6 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)6 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)6 ResultSet (java.sql.ResultSet)4 HashMap (java.util.HashMap)4 TSalesforceBulkExecDefinition (org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition)4 TSalesforceBulkExecProperties (org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecProperties)4