Search in sources :

Example 11 with Reader

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

the class JDBCTypeMappingTestIT method doWriteWithAllType.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void doWriteWithAllType(boolean nullableForAnyColumn) throws IOException {
    TJDBCOutputDefinition definition = new TJDBCOutputDefinition();
    TJDBCOutputProperties properties = DBTestUtils.createCommonJDBCOutputProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema3(nullableForAnyColumn, tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.dataAction.setValue(DataAction.INSERT);
    JDBCOutputWriter writer = DBTestUtils.createCommonJDBCOutputWriter(definition, properties);
    try {
        writer.open("wid");
        List<IndexedRecord> inputRecords = DBTestUtils.prepareIndexRecords(nullableForAnyColumn, tablename);
        for (IndexedRecord inputRecord : inputRecords) {
            writer.write(inputRecord);
            DBTestUtils.assertSuccessRecord(writer, inputRecord);
        }
        writer.close();
    } finally {
        writer.close();
    }
    // read the inserted data from the target table by the reader
    Reader reader = null;
    try {
        TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
        TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
        properties1.main.schema.setValue(DBTestUtils.createTestSchema3(nullableForAnyColumn, tablename));
        properties1.tableSelection.tablename.setValue(tablename);
        properties1.sql.setValue(DBTestUtils.getSQL(tablename));
        reader = DBTestUtils.createCommonJDBCInputReader(properties1);
        reader.start();
        int i = 0;
        while ((i++) < 5) {
            // skip the 5 rows at the head
            reader.advance();
        }
        IndexedRecord row = (IndexedRecord) reader.getCurrent();
        Integer c1 = (Integer) row.get(0);
        Short c2 = (Short) row.get(1);
        Long c3 = (Long) row.get(2);
        Float c4 = (Float) row.get(3);
        Double c5 = (Double) row.get(4);
        Float c6 = (Float) row.get(5);
        BigDecimal c7 = (BigDecimal) row.get(6);
        BigDecimal c8 = (BigDecimal) row.get(7);
        Boolean c9 = (Boolean) row.get(8);
        String c10 = (String) row.get(9);
        Long c11 = (Long) row.get(10);
        Long c12 = (Long) row.get(11);
        Long c13 = (Long) row.get(12);
        String c14 = (String) row.get(13);
        String c15 = (String) row.get(14);
        assertEquals(1, c1.intValue());
        assertEquals(2, c2.intValue());
        assertEquals(3, c3.intValue());
        Assert.assertNotNull(c4);
        Assert.assertNotNull(c5);
        Assert.assertNotNull(c6);
        assertEquals(new BigDecimal("7.01"), c7);
        assertEquals(new BigDecimal("8.01"), c8);
        assertEquals(true, c9);
        assertEquals("content : 1", c10.trim());
        Assert.assertNotNull(c11);
        Assert.assertNotNull(c12);
        Assert.assertNotNull(c13);
        assertEquals("wangwei", c14);
        assertEquals("long content : 1", c15);
        reader.advance();
        row = (IndexedRecord) reader.getCurrent();
        c1 = (Integer) row.get(0);
        c2 = (Short) row.get(1);
        c3 = (Long) row.get(2);
        c4 = (Float) row.get(3);
        c5 = (Double) row.get(4);
        c6 = (Float) row.get(5);
        c7 = (BigDecimal) row.get(6);
        c8 = (BigDecimal) row.get(7);
        c9 = (Boolean) row.get(8);
        c10 = (String) row.get(9);
        c11 = (Long) row.get(10);
        c12 = (Long) row.get(11);
        c13 = (Long) row.get(12);
        c14 = (String) row.get(13);
        c15 = (String) row.get(14);
        assertEquals(1, c1.intValue());
        assertEquals(2, c2.intValue());
        assertEquals(3, c3.intValue());
        Assert.assertNotNull(c4);
        Assert.assertNotNull(c5);
        Assert.assertNotNull(c6);
        assertEquals(new BigDecimal("7.01"), c7);
        assertEquals(new BigDecimal("8.01"), c8);
        assertEquals(true, c9);
        assertEquals("content : 2", c10.trim());
        Assert.assertNotNull(c11);
        Assert.assertNotNull(c12);
        Assert.assertNotNull(c13);
        assertEquals("gaoyan", c14);
        assertEquals("long content : 2", c15);
        reader.advance();
        row = (IndexedRecord) reader.getCurrent();
        c1 = (Integer) row.get(0);
        c2 = (Short) row.get(1);
        c3 = (Long) row.get(2);
        c4 = (Float) row.get(3);
        c5 = (Double) row.get(4);
        c6 = (Float) row.get(5);
        c7 = (BigDecimal) row.get(6);
        c8 = (BigDecimal) row.get(7);
        c9 = (Boolean) row.get(8);
        c10 = (String) row.get(9);
        c11 = (Long) row.get(10);
        c12 = (Long) row.get(11);
        c13 = (Long) row.get(12);
        c14 = (String) row.get(13);
        c15 = (String) row.get(14);
        assertEquals(1, c1.intValue());
        assertEquals(2, c2.intValue());
        assertEquals(3, c3.intValue());
        Assert.assertNotNull(c4);
        Assert.assertNotNull(c5);
        Assert.assertNotNull(c6);
        assertEquals(new BigDecimal("7.01"), c7);
        assertEquals(new BigDecimal("8.01"), c8);
        assertEquals(true, c9);
        assertEquals("content : 3", c10.trim());
        Assert.assertNotNull(c11);
        Assert.assertNotNull(c12);
        Assert.assertNotNull(c13);
        assertEquals("dabao", c14);
        assertEquals("long content : 3", c15);
        reader.advance();
        row = (IndexedRecord) reader.getCurrent();
        c1 = (Integer) row.get(0);
        c2 = (Short) row.get(1);
        c3 = (Long) row.get(2);
        c4 = (Float) row.get(3);
        c5 = (Double) row.get(4);
        c6 = (Float) row.get(5);
        c7 = (BigDecimal) row.get(6);
        c8 = (BigDecimal) row.get(7);
        c9 = (Boolean) row.get(8);
        c10 = (String) row.get(9);
        c11 = (Long) row.get(10);
        c12 = (Long) row.get(11);
        c13 = (Long) row.get(12);
        c14 = (String) row.get(13);
        c15 = (String) row.get(14);
        assertEquals(1, c1.intValue());
        Assert.assertNull(c2);
        Assert.assertNull(c3);
        Assert.assertNull(c4);
        Assert.assertNull(c5);
        Assert.assertNull(c6);
        Assert.assertNull(c7);
        Assert.assertNull(c8);
        Assert.assertNull(c9);
        Assert.assertNull(c10);
        Assert.assertNull(c11);
        Assert.assertNull(c12);
        // some database set default value for this column as default
        // Assert.assertNull(c13);
        Assert.assertNull(c14);
        Assert.assertNull(c15);
        reader.advance();
        row = (IndexedRecord) reader.getCurrent();
        c1 = (Integer) row.get(0);
        c2 = (Short) row.get(1);
        c3 = (Long) row.get(2);
        c4 = (Float) row.get(3);
        c5 = (Double) row.get(4);
        c6 = (Float) row.get(5);
        c7 = (BigDecimal) row.get(6);
        c8 = (BigDecimal) row.get(7);
        c9 = (Boolean) row.get(8);
        c10 = (String) row.get(9);
        c11 = (Long) row.get(10);
        c12 = (Long) row.get(11);
        c13 = (Long) row.get(12);
        c14 = (String) row.get(13);
        c15 = (String) row.get(14);
        Assert.assertNull(c1);
        Assert.assertNull(c2);
        Assert.assertNull(c3);
        Assert.assertNull(c4);
        Assert.assertNull(c5);
        Assert.assertNull(c6);
        Assert.assertNull(c7);
        Assert.assertNull(c8);
        Assert.assertNull(c9);
        Assert.assertNull(c10);
        Assert.assertNull(c11);
        Assert.assertNull(c12);
        // some database set default value for this column as default
        // Assert.assertNull(c13);
        Assert.assertEquals("good luck", c14);
        Assert.assertNull(c15);
        reader.close();
        Map<String, Object> returnMap = reader.getReturnValues();
        Assert.assertEquals(10, 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) Schema(org.apache.avro.Schema) Reader(org.talend.components.api.component.runtime.Reader) JDBCOutputWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputWriter) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) SQLException(java.sql.SQLException) IOException(java.io.IOException) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)

Example 12 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());
    JDBCRowSource source = new JDBCRowSource();
    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 : 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)

Example 13 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));
    JDBCRowSource source = new JDBCRowSource();
    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 : 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 14 with Reader

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

the class JDBCInputTestIT method testType.

@SuppressWarnings({ "rawtypes" })
@Test
public void testType() throws Exception {
    JDBCInputDefinition definition = new JDBCInputDefinition();
    JDBCInputProperties properties = createCommonJDBCInputProperties(definition);
    Reader reader = DBTestUtils.createCommonJDBCInputReader(properties);
    try {
        IndexedRecordConverter<Object, ? extends IndexedRecord> converter = null;
        for (boolean available = reader.start(); available; available = reader.advance()) {
            converter = DBTestUtils.getIndexRecordConverter(reader, converter);
            IndexedRecord record = converter.convertToAvro(reader.getCurrent());
            assertEquals(String.class, record.get(0).getClass());
            assertEquals(String.class, record.get(1).getClass());
        }
        reader.close();
    } finally {
        reader.close();
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Reader(org.talend.components.api.component.runtime.Reader) Test(org.junit.Test)

Example 15 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 {
        JDBCInputDefinition definition = new JDBCInputDefinition();
        JDBCInputProperties properties = createCommonJDBCInputProperties(definition);
        reader = DBTestUtils.createCommonJDBCInputReader(properties);
        reader.start();
        IndexedRecord row = (IndexedRecord) reader.getCurrent();
        String id = (String) row.get(0);
        String name = (String) row.get(1);
        assertEquals("1", id);
        assertEquals("wangwei", name);
        reader.advance();
        row = (IndexedRecord) reader.getCurrent();
        id = (String) row.get(0);
        name = (String) row.get(1);
        assertEquals("2", id);
        assertEquals("gaoyan", name);
        reader.advance();
        row = (IndexedRecord) reader.getCurrent();
        id = (String) row.get(0);
        name = (String) row.get(1);
        assertEquals("3", id);
        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) Reader(org.talend.components.api.component.runtime.Reader) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) 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