Search in sources :

Example 11 with TJDBCInputDefinition

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition in project components by Talend.

the class JDBCRowTestIT method test_basic_no_connector.

@Test
public void test_basic_no_connector() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(4, 'momo')");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    JDBCRowSourceOrSink sourceOrSink = new JDBCRowSourceOrSink();
    sourceOrSink.initialize(null, properties);
    ValidationResult result = sourceOrSink.validate(null);
    Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, DBTestUtils.createTestSchema(tablename), definition1, properties1);
    assertThat(records, hasSize(4));
    Assert.assertEquals(new Integer(4), records.get(3).get(0));
    Assert.assertEquals("momo", records.get(3).get(1));
}
Also used : TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCRowSourceOrSink(org.talend.components.jdbc.runtime.JDBCRowSourceOrSink) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 12 with TJDBCInputDefinition

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition 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 13 with TJDBCInputDefinition

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition 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 14 with TJDBCInputDefinition

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition in project components by Talend.

the class JdbcRowTestIT method test_basic_no_connector.

@Test
public void test_basic_no_connector() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(4, 'momo')");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.NONE), properties.getClass().getClassLoader())) {
        JDBCRowSourceOrSink sourceOrSink = (JDBCRowSourceOrSink) sandboxedInstance.getInstance();
        sourceOrSink.initialize(null, properties);
        ValidationResult result = sourceOrSink.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, DBTestUtils.createTestSchema(tablename), definition1, properties1);
    assertThat(records, hasSize(4));
    Assert.assertEquals(new Integer(4), records.get(3).get(0));
    Assert.assertEquals("momo", records.get(3).get(1));
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCRowSourceOrSink(org.talend.components.jdbc.runtime.JDBCRowSourceOrSink) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 15 with TJDBCInputDefinition

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition in project components by Talend.

the class JdbcRowTestIT method test_basic_as_output.

@SuppressWarnings("rawtypes")
@Test
public void test_basic_as_output() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(?,?)");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1, 2));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name(), PreparedStatementTable.Type.String.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(4, "momo"));
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.INCOMING_AND_OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSink sink = (JDBCRowSink) sandboxedInstance.getInstance();
        sink.initialize(null, properties);
        ValidationResult result = sink.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        WriteOperation operation = sink.createWriteOperation();
        JDBCRowWriter writer = (JDBCRowWriter) operation.createWriter(null);
        try {
            writer.open("wid");
            IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
            r1.put(0, 4);
            r1.put(1, "xiaoming");
            writer.write(r1);
            DBTestUtils.assertSuccessRecord(writer, r1);
            IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
            r2.put(0, 5);
            r2.put(1, "xiaobai");
            writer.write(r2);
            DBTestUtils.assertSuccessRecord(writer, r2);
            writer.close();
        } finally {
            writer.close();
        }
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, schema, definition1, properties1);
    assertThat(records, hasSize(5));
    Assert.assertEquals(new Integer(4), records.get(3).get(0));
    Assert.assertEquals("momo", records.get(3).get(1));
    Assert.assertEquals(new Integer(4), records.get(4).get(0));
    Assert.assertEquals("momo", records.get(4).get(1));
}
Also used : TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Aggregations

TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)32 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)32 Test (org.junit.Test)28 IndexedRecord (org.apache.avro.generic.IndexedRecord)23 Schema (org.apache.avro.Schema)17 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)12 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)12 JDBCOutputWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputWriter)11 JDBCSource (org.talend.components.jdbc.runtime.JDBCSource)9 Reader (org.talend.components.api.component.runtime.Reader)6 ComponentException (org.talend.components.api.exception.ComponentException)6 ValidationResult (org.talend.daikon.properties.ValidationResult)6 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)5 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)5 Field (org.apache.avro.Schema.Field)4 JDBCRowSourceOrSink (org.talend.components.jdbc.runtime.JDBCRowSourceOrSink)4 IOException (java.io.IOException)3 BigDecimal (java.math.BigDecimal)3 SQLException (java.sql.SQLException)3 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)3