Search in sources :

Example 6 with WriteOperation

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

the class SalesforceRuntimeTestUtil method initWriter.

private Writer initWriter(TSalesforceOutputBulkDefinition definition, TSalesforceOutputBulkProperties modelProperties) throws IOException {
    // simulate to generate the runtime code
    TSalesforceOutputBulkProperties runtimeProperties = (TSalesforceOutputBulkProperties) definition.createRuntimeProperties();
    // pass all the value from the ui model
    runtimeProperties.schema.schema.setValue(modelProperties.schema.schema.getValue());
    runtimeProperties.bulkFilePath.setValue(modelProperties.bulkFilePath.getStringValue());
    runtimeProperties.append.setValue(modelProperties.append.getValue());
    runtimeProperties.ignoreNull.setValue(modelProperties.ignoreNull.getValue());
    Object obj = modelProperties.upsertRelationTable.columnName.getValue();
    if (obj != null && obj instanceof List && !((List) obj).isEmpty()) {
        runtimeProperties.upsertRelationTable.columnName.setValue(modelProperties.upsertRelationTable.columnName.getValue());
        runtimeProperties.upsertRelationTable.lookupFieldExternalIdName.setValue(modelProperties.upsertRelationTable.lookupFieldExternalIdName.getValue());
        runtimeProperties.upsertRelationTable.lookupFieldModuleName.setValue(modelProperties.upsertRelationTable.lookupFieldModuleName.getValue());
        runtimeProperties.upsertRelationTable.lookupRelationshipFieldName.setValue(modelProperties.upsertRelationTable.lookupRelationshipFieldName.getValue());
        runtimeProperties.upsertRelationTable.polymorphic.setValue(modelProperties.upsertRelationTable.polymorphic.getValue());
    }
    SourceOrSink source_sink = new SalesforceBulkFileSink();
    source_sink.initialize(null, runtimeProperties);
    ValidationResult result = source_sink.validate(null);
    Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    Sink sink = (Sink) source_sink;
    WriteOperation writeOperation = sink.createWriteOperation();
    writeOperation.initialize(null);
    Writer writer = writeOperation.createWriter(null);
    writer.open("component_instance_id");
    return writer;
}
Also used : TSalesforceOutputBulkProperties(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties) SourceOrSink(org.talend.components.api.component.runtime.SourceOrSink) Sink(org.talend.components.api.component.runtime.Sink) SourceOrSink(org.talend.components.api.component.runtime.SourceOrSink) SalesforceBulkFileSink(org.talend.components.salesforce.runtime.SalesforceBulkFileSink) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) SObject(com.sforce.soap.partner.sobject.SObject) ArrayList(java.util.ArrayList) List(java.util.List) SalesforceBulkFileSink(org.talend.components.salesforce.runtime.SalesforceBulkFileSink) ValidationResult(org.talend.daikon.properties.ValidationResult) Writer(org.talend.components.api.component.runtime.Writer)

Example 7 with WriteOperation

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

the class JdbcRowTestIT method test_reject_as_output.

@SuppressWarnings("rawtypes")
@Test
public void test_reject_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(false);
    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, "a too long value"));
    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);
            List<IndexedRecord> rejects = writer.getRejectedWrites();
            assertThat(rejects, hasSize(1));
            IndexedRecord reject = rejects.get(0);
            Assert.assertEquals(4, reject.get(0));
            Assert.assertEquals("xiaoming", reject.get(1));
            Assert.assertNotNull(reject.get(2));
            Assert.assertNotNull(reject.get(3));
            assertThat(writer.getSuccessfulWrites(), empty());
            writer.cleanWrites();
            IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
            r2.put(0, 5);
            r2.put(1, "xiaobai");
            writer.write(r2);
            rejects = writer.getRejectedWrites();
            assertThat(rejects, hasSize(1));
            reject = rejects.get(0);
            Assert.assertEquals(5, reject.get(0));
            Assert.assertEquals("xiaobai", reject.get(1));
            Assert.assertNotNull(reject.get(2));
            Assert.assertNotNull(reject.get(3));
            assertThat(writer.getSuccessfulWrites(), empty());
            writer.cleanWrites();
            writer.close();
        } finally {
            writer.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 8 with WriteOperation

use of org.talend.components.api.component.runtime.WriteOperation 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)

Example 9 with WriteOperation

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

the class JdbcRowTestIT method test_propagate_query_result_set_as_output.

@SuppressWarnings("rawtypes")
@Test
public void test_propagate_query_result_set_as_output() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema5(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);
    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(3));
    properties.propagateQueryResultSet.setValue(true);
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(2).toString());
    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);
            assertThat(writer.getRejectedWrites(), empty());
            List<IndexedRecord> successfulWrites = writer.getSuccessfulWrites();
            assertThat(successfulWrites, hasSize(1));
            IndexedRecord successRecord = successfulWrites.get(0);
            Assert.assertEquals(4, successRecord.get(0));
            Assert.assertEquals("xiaoming", successRecord.get(1));
            ResultSet resultSet = (ResultSet) successRecord.get(2);
            resultSet.next();
            Assert.assertEquals(3, resultSet.getInt(1));
            Assert.assertEquals("dabao", resultSet.getString(2));
            resultSet.close();
            writer.cleanWrites();
            IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
            r2.put(0, 5);
            r2.put(1, "xiaobai");
            writer.write(r2);
            assertThat(writer.getRejectedWrites(), empty());
            successfulWrites = writer.getSuccessfulWrites();
            assertThat(successfulWrites, hasSize(1));
            successRecord = successfulWrites.get(0);
            Assert.assertEquals(5, successRecord.get(0));
            Assert.assertEquals("xiaobai", successRecord.get(1));
            resultSet = (ResultSet) successRecord.get(2);
            resultSet.next();
            Assert.assertEquals(3, resultSet.getInt(1));
            Assert.assertEquals("dabao", resultSet.getString(2));
            resultSet.close();
            writer.cleanWrites();
            writer.close();
        } finally {
            writer.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 10 with WriteOperation

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

the class JDBCSPTestIT method test_basic_as_output_and_input.

@SuppressWarnings("rawtypes")
@Test
public void test_basic_as_output_and_input() throws Exception {
    TJDBCSPDefinition definition = new TJDBCSPDefinition();
    TJDBCSPProperties properties = DBTestUtils.createCommonJDBCSPProperties(allSetting, definition);
    properties.spName.setValue("SYSCS_UTIL.SYSCS_DISABLE_LOG_ARCHIVE_MODE");
    Schema schema = DBTestUtils.createSPSchema3(tablename);
    properties.main.schema.setValue(schema);
    properties.schemaFlow.schema.setValue(schema);
    properties.spParameterTable.parameterTypes.setValue(Arrays.asList(SPParameterTable.ParameterType.IN.name()));
    properties.spParameterTable.schemaColumns.setValue(Arrays.asList("PARAMETER1"));
    JDBCSPSink sink = new JDBCSPSink();
    sink.initialize(null, properties);
    ValidationResult result = sink.validate(null);
    Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    WriteOperation operation = sink.createWriteOperation();
    JDBCSPWriter writer = (JDBCSPWriter) operation.createWriter(null);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 0);
        r1.put(1, "wangwei");
        writer.write(r1);
        List<IndexedRecord> writeResult = writer.getSuccessfulWrites();
        Assert.assertEquals(1, writeResult.size());
        IndexedRecord record = writeResult.get(0);
        Assert.assertEquals(Integer.valueOf(0), record.get(0));
        Assert.assertEquals("wangwei", record.get(1));
        writer.cleanWrites();
    } finally {
        writer.close();
    }
}
Also used : JDBCSPSink(org.talend.components.jdbc.runtime.JDBCSPSink) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) TJDBCSPProperties(org.talend.components.jdbc.tjdbcsp.TJDBCSPProperties) JDBCSPWriter(org.talend.components.jdbc.runtime.writer.JDBCSPWriter) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCSPDefinition(org.talend.components.jdbc.tjdbcsp.TJDBCSPDefinition) ValidationResult(org.talend.daikon.properties.ValidationResult) Test(org.junit.Test)

Aggregations

WriteOperation (org.talend.components.api.component.runtime.WriteOperation)16 IndexedRecord (org.apache.avro.generic.IndexedRecord)14 ValidationResult (org.talend.daikon.properties.ValidationResult)13 Test (org.junit.Test)12 Schema (org.apache.avro.Schema)9 JDBCRowSink (org.talend.components.jdbc.runtime.JDBCRowSink)8 JDBCRowWriter (org.talend.components.jdbc.runtime.writer.JDBCRowWriter)8 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)7 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)7 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)5 ResultSet (java.sql.ResultSet)4 JDBCSink (org.talend.components.jdbc.runtime.JDBCSink)4 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)3 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)3 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)3 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)3 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 Sink (org.talend.components.api.component.runtime.Sink)2 ComponentException (org.talend.components.api.exception.ComponentException)2