Search in sources :

Example 1 with JDBCInputReader

use of org.talend.components.jdbc.runtime.reader.JDBCInputReader in project components by Talend.

the class JDBCDatasetRuntime method getSample.

@Override
public void getSample(int limit, Consumer<IndexedRecord> consumer) {
    JDBCSource js = new JDBCSource();
    throwExceptionIfValidationResultIsError(js.initialize(container, dataset));
    throwExceptionIfValidationResultIsError(js.validate(container));
    JDBCInputReader reader = (JDBCInputReader) js.createReader(container);
    ReaderDataProvider<IndexedRecord> readerDataProvider = new ReaderDataProvider<>(reader, limit, consumer);
    readerDataProvider.retrieveData();
}
Also used : ReaderDataProvider(org.talend.components.api.component.runtime.ReaderDataProvider) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCSource(org.talend.components.jdbc.runtime.JDBCSource) JDBCInputReader(org.talend.components.jdbc.runtime.reader.JDBCInputReader)

Example 2 with JDBCInputReader

use of org.talend.components.jdbc.runtime.reader.JDBCInputReader in project components by Talend.

the class JDBCInputTestIT method testReaderAllTypesString.

/**
 * Checks {@link JDBCInputReader} outputs {@link IndexedRecord} which contains nullable String type data for every SQL/JDBC
 * type
 */
@Test
public void testReaderAllTypesString() throws IOException {
    TJDBCInputDefinition definition = new TJDBCInputDefinition();
    TJDBCInputProperties properties = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition);
    properties.main.schema.setValue(DBTestUtils.createAllTypesSchema(tablename_all_type));
    properties.tableSelection.tablename.setValue(tablename_all_type);
    properties.sql.setValue(DBTestUtils.getSQL(tablename_all_type));
    Reader reader = DBTestUtils.createCommonJDBCInputReader(properties);
    reader.start();
    IndexedRecord record = (IndexedRecord) reader.getCurrent();
    Short col0 = (Short) record.get(0);
    Integer col1 = (Integer) record.get(1);
    Long col2 = (Long) record.get(2);
    Float col3 = (Float) record.get(3);
    Double col4 = (Double) record.get(4);
    BigDecimal col5 = (BigDecimal) record.get(5);
    String col6 = (String) record.get(6);
    String col7 = (String) record.get(7);
    String col8 = (String) record.get(8);
    String col9 = (String) record.get(9);
    Long col10 = (Long) record.get(10);
    Long col11 = (Long) record.get(11);
    Long col12 = (Long) record.get(12);
    Boolean col13 = (Boolean) record.get(13);
    assertEquals(32767, col0.shortValue());
    assertEquals(2147483647, col1.intValue());
    assertEquals(9223372036854775807l, col2.longValue());
    assertTrue(col3 > 1);
    assertTrue(col4 > 2);
    assertEquals(new BigDecimal("1234567890.1234567890"), col5);
    assertEquals("abcd", col6);
    assertEquals("abcdefg", col7);
    assertEquals("00010203040506070809", col8);
    assertEquals("abcdefg", col9);
    assertEquals("2016-12-28", new SimpleDateFormat("yyyy-MM-dd").format(new Date(col10)));
    assertEquals("14:30:33", new SimpleDateFormat("HH:mm:ss").format(new Date(col11)));
    assertEquals("2016-12-28 14:31:56.123", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(col12)));
    assertEquals(true, col13);
    Schema actualSchema = record.getSchema();
    List<Field> actualFields = actualSchema.getFields();
    assertEquals(14, actualFields.size());
    reader.close();
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) JDBCInputReader(org.talend.components.jdbc.runtime.reader.JDBCInputReader) Reader(org.talend.components.api.component.runtime.Reader) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Field(org.apache.avro.Schema.Field) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) SimpleDateFormat(java.text.SimpleDateFormat) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Aggregations

IndexedRecord (org.apache.avro.generic.IndexedRecord)2 JDBCInputReader (org.talend.components.jdbc.runtime.reader.JDBCInputReader)2 BigDecimal (java.math.BigDecimal)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Schema (org.apache.avro.Schema)1 Field (org.apache.avro.Schema.Field)1 Test (org.junit.Test)1 Reader (org.talend.components.api.component.runtime.Reader)1 ReaderDataProvider (org.talend.components.api.component.runtime.ReaderDataProvider)1 JDBCSource (org.talend.components.jdbc.runtime.JDBCSource)1 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)1 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)1