Search in sources :

Example 1 with JDBCSource

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

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

the class JDBCInputTestIT method testGetSchema.

@Test
public void testGetSchema() throws Exception {
    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));
    JDBCSource source = DBTestUtils.createCommonJDBCSource(properties);
    RuntimeContainer container = new DefaultComponentRuntimeContainerImpl() {

        @Override
        public String getCurrentComponentId() {
            return "tJDBCInput1";
        }
    };
    // the getResource method will convert "@" to "%40" when work with maven together, not sure the bug appear where, bug make
    // sure it come from the env, not the function code, so only convert here
    // in the product env, the mappings_url is passed from the platform
    java.net.URL mappings_url = this.getClass().getResource("/mappings");
    mappings_url = DBTestUtils.correctURL(mappings_url);
    container.setComponentData(container.getCurrentComponentId(), ComponentConstants.MAPPING_URL_SUBFIX, mappings_url);
    Schema schema = source.getEndpointSchema(container, tablename);
    assertEquals(tablename, schema.getName().toUpperCase());
    List<Field> columns = schema.getFields();
    DBTestUtils.testMetadata(columns);
}
Also used : Field(org.apache.avro.Schema.Field) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) Schema(org.apache.avro.Schema) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCSource(org.talend.components.jdbc.runtime.JDBCSource) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Test(org.junit.Test)

Example 3 with JDBCSource

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

the class JDBCInputTestIT method testGetSchemaNamesWithException.

@Test(expected = ComponentException.class)
public void testGetSchemaNamesWithException() throws Exception {
    TJDBCInputDefinition definition = new TJDBCInputDefinition();
    TJDBCInputProperties properties = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition);
    properties.connection.driverClass.setValue("notexist");
    JDBCSource source = DBTestUtils.createCommonJDBCSource(properties);
    try {
        source.getSchemaNames(null);
    } catch (ComponentException e) {
        String message = CommonUtils.getClearExceptionInfo(e);
        assertTrue(message.contains("notexist"));
        throw e;
    }
}
Also used : ComponentException(org.talend.components.api.exception.ComponentException) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCSource(org.talend.components.jdbc.runtime.JDBCSource) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 4 with JDBCSource

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

the class JDBCInputTestIT method testGetSchemaFromQueryWithException1.

@Test(expected = ComponentException.class)
public void testGetSchemaFromQueryWithException1() throws Exception {
    TJDBCInputDefinition definition = new TJDBCInputDefinition();
    TJDBCInputProperties properties = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition);
    JDBCSource source = DBTestUtils.createCommonJDBCSource(properties);
    try {
        source.getSchemaFromQuery(null, "select * from notexist");
    } catch (ComponentException e) {
        String message = CommonUtils.getClearExceptionInfo(e);
        assertTrue(message != null && !message.isEmpty());
        throw e;
    }
}
Also used : ComponentException(org.talend.components.api.exception.ComponentException) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCSource(org.talend.components.jdbc.runtime.JDBCSource) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 5 with JDBCSource

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

the class DBTestUtils method fetchDataByReaderFromTable.

@SuppressWarnings("rawtypes")
public static List<IndexedRecord> fetchDataByReaderFromTable(String tablename, Schema schema, TJDBCInputDefinition definition, TJDBCInputProperties properties) {
    List<IndexedRecord> result = new ArrayList<IndexedRecord>();
    properties.main.schema.setValue(schema);
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select * from " + tablename);
    JDBCSource source = new JDBCSource();
    source.initialize(null, properties);
    Reader reader = null;
    try {
        reader = source.createReader(null);
        boolean haveNext = reader.start();
        while (haveNext) {
            IndexedRecord row = (IndexedRecord) reader.getCurrent();
            result.add(copyValueFrom(row));
            haveNext = reader.advance();
        }
        reader.close();
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                Assert.fail(e.getMessage());
            }
        }
    }
    return result;
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) ArrayList(java.util.ArrayList) Reader(org.talend.components.api.component.runtime.Reader) JDBCSource(org.talend.components.jdbc.runtime.JDBCSource) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

JDBCSource (org.talend.components.jdbc.runtime.JDBCSource)12 Test (org.junit.Test)9 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)9 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)9 ComponentException (org.talend.components.api.exception.ComponentException)5 Schema (org.apache.avro.Schema)3 Field (org.apache.avro.Schema.Field)3 DefaultComponentRuntimeContainerImpl (org.talend.components.api.container.DefaultComponentRuntimeContainerImpl)3 RuntimeContainer (org.talend.components.api.container.RuntimeContainer)3 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Reader (org.talend.components.api.component.runtime.Reader)1 ReaderDataProvider (org.talend.components.api.component.runtime.ReaderDataProvider)1 JDBCInputReader (org.talend.components.jdbc.runtime.reader.JDBCInputReader)1 NamedThing (org.talend.daikon.NamedThing)1