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();
}
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);
}
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;
}
}
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;
}
}
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;
}
Aggregations