Search in sources :

Example 1 with Sink

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

the class TFilterRowDefinitionTest method testGetRuntime.

/**
 * Check {@link TFilterRowDefinition#getRuntime()} returns instance of {@link JiraSource}
 */
@Test
public void testGetRuntime() {
    TFilterRowDefinition definition = new TFilterRowDefinition();
    RuntimeInfo runtimeInfo = definition.getRuntimeInfo(ExecutionEngine.DI, null, ConnectorTopology.INCOMING_AND_OUTGOING);
    SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(runtimeInfo, definition.getClass().getClassLoader());
    Sink source = (Sink) sandboxedInstance.getInstance();
    assertThat(source, is(instanceOf(TFilterRowSink.class)));
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) RuntimeInfo(org.talend.daikon.runtime.RuntimeInfo) Sink(org.talend.components.api.component.runtime.Sink) TFilterRowSink(org.talend.components.filterrow.runtime.TFilterRowSink) Test(org.junit.Test)

Example 2 with Sink

use of org.talend.components.api.component.runtime.Sink 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) SObject(com.sforce.soap.partner.sobject.SObject) ArrayList(java.util.ArrayList) List(java.util.List) ValidationResult(org.talend.daikon.properties.ValidationResult)

Example 3 with Sink

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

the class RuntimeControllerImpl method writeData.

@Override
public void writeData(InputStream rawPayload) throws IOException {
    // 1) Read payload (with data as a stream of course)
    DatasetWritePayload payload = DatasetWritePayload.readData(rawPayload, mapper);
    String definitionName = payload.getConfiguration().getDefinitionName();
    // 2) Create properties
    Properties properties = propertiesHelpers.propertiesFromDto(payload.getConfiguration());
    if (properties instanceof ComponentProperties) {
        ComponentProperties componentProperties = (ComponentProperties) properties;
        // 3) Retrieve component definition to be able to create the runtime
        final ComponentDefinition definition = propertiesHelpers.getDefinition(ComponentDefinition.class, definitionName);
        // 4) Get the execution engine
        ExecutionEngine executionEngine;
        if (definition.isSupportingExecutionEngines(DI)) {
            executionEngine = DI;
            // 5) Create the sandbox
            try (SandboxedInstance instance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(executionEngine, componentProperties, INCOMING), definition.getClass().getClassLoader())) {
                Sink datasetRuntimeInstance = (Sink) instance.getInstance();
                datasetRuntimeInstance.initialize(null, componentProperties);
                Iterator<IndexedRecord> data = payload.getData();
                WriteOperation writeOperation = datasetRuntimeInstance.createWriteOperation();
                // Supplier return null to signify end of data stream => see WriterDataSupplier.writeData
                WriterDataSupplier<?, IndexedRecord> stringWriterDataSupplier = new WriterDataSupplier<Object, IndexedRecord>(writeOperation, () -> data.hasNext() ? data.next() : null, null);
                stringWriterDataSupplier.writeData();
            }
        } else if (definition.isSupportingExecutionEngines(BEAM)) {
            throw new UnsupportedOperationException("Beam runtime is not available for dataset write through HTTP API.");
        } else {
            throw new TalendRuntimeException(CommonErrorCodes.UNREGISTERED_DEFINITION);
        }
    } else if (properties instanceof DatasetProperties) {
        throw new UnsupportedOperationException("HTTP API is only able to write using component implementations. Not " + properties.getClass());
    }
}
Also used : ComponentProperties(org.talend.components.api.properties.ComponentProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) DatasetProperties(org.talend.components.common.dataset.DatasetProperties) DatasetProperties(org.talend.components.common.dataset.DatasetProperties) DatastoreProperties(org.talend.components.common.datastore.DatastoreProperties) ComponentProperties(org.talend.components.api.properties.ComponentProperties) Properties(org.talend.daikon.properties.Properties) SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) ExecutionEngine(org.talend.components.api.component.runtime.ExecutionEngine) Sink(org.talend.components.api.component.runtime.Sink) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) WriterDataSupplier(org.talend.components.api.component.runtime.WriterDataSupplier) ComponentDefinition(org.talend.components.api.component.ComponentDefinition)

Example 4 with Sink

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

the class TFilterRowTest method testTFilterRowRuntime.

@Test
public void testTFilterRowRuntime() throws Exception {
    TFilterRowProperties props = (TFilterRowProperties) getComponentService().getComponentProperties("tFilterRow_POC");
    props.schemaListener.afterSchema();
    assertThat(props.conditionsTable.isEditable(), is(false));
    Schema schema = SchemaBuilder.builder().record("testRecord").fields().name("field1").type().stringType().noDefault().endRecord();
    props.schemaMain.schema.setValue(schema);
    final List<Schema.Field> additionalRejectFields = new ArrayList<Schema.Field>();
    Schema.Field field = null;
    field = new Schema.Field(TFilterRowProperties.FIELD_ERROR_MESSAGE, Schema.create(Schema.Type.STRING), null, (Object) null);
    field.addProp(SchemaConstants.TALEND_IS_LOCKED, "false");
    field.addProp(SchemaConstants.TALEND_FIELD_GENERATED, "true");
    field.addProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH, "255");
    additionalRejectFields.add(field);
    Schema rejectSchema = newSchema(schema, "rejectOutput", additionalRejectFields);
    props.schemaListener.afterSchema();
    assertThat(props.schemaReject.schema.getValue(), is((Schema) rejectSchema));
    assertThat(props.schemaFlow.schema.getValue(), is((Schema) schema));
    assertThat(props.conditionsTable.isEditable(), is(true));
    props.conditionsTable.columnName.setValue(Arrays.asList("field1"));
    props.conditionsTable.function.setValue(Arrays.asList(FunctionType.EMPTY));
    props.conditionsTable.operator.setValue(Arrays.asList(OperatorType.EQUALS));
    props.conditionsTable.value.setValue(Arrays.asList((Object) "test"));
    props.logicalOperator.setValue(LogicalOperator.Or);
    Sink sink = new TFilterRowSink();
    sink.initialize(null, props);
    WriteOperation<?> writeOperation = sink.createWriteOperation();
    writeOperation.initialize(null);
    WriterWithFeedback<?, ?, ?> writer = (WriterWithFeedback<?, ?, ?>) writeOperation.createWriter(null);
    writer.open("uId");
    IndexedRecord record = new GenericData.Record(schema);
    record.put(schema.getField("field1").pos(), "test");
    writer.write(record);
    Iterable<?> success = writer.getSuccessfulWrites();
    Iterator<?> successIterator = success.iterator();
    assertThat(successIterator.hasNext(), is(true));
    IndexedRecord successWrite = (IndexedRecord) successIterator.next();
    assertThat(successWrite.getSchema(), is(schema));
    assertThat(successWrite, is(record));
    writer.cleanWrites();
    record = new GenericData.Record(schema);
    record.put(schema.getField("field1").pos(), "test123");
    writer.write(record);
    success = writer.getSuccessfulWrites();
    successIterator = success.iterator();
    assertThat(successIterator.hasNext(), is(false));
    Iterable<?> reject = writer.getRejectedWrites();
    Iterator<?> rejectIterator = reject.iterator();
    assertThat(rejectIterator.hasNext(), is(true));
    IndexedRecord rejectWrite = (IndexedRecord) rejectIterator.next();
    assertThat(rejectWrite.getSchema(), is(rejectSchema));
    Result result = (Result) writer.close();
    assertThat(result.totalCount, is(2));
    assertThat(result.successCount, is(1));
    assertThat(result.rejectCount, is(1));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) Result(org.talend.components.api.component.runtime.Result) TFilterRowSink(org.talend.components.filterrow.runtime.TFilterRowSink) Sink(org.talend.components.api.component.runtime.Sink) IndexedRecord(org.apache.avro.generic.IndexedRecord) TFilterRowSink(org.talend.components.filterrow.runtime.TFilterRowSink) WriterWithFeedback(org.talend.components.api.component.runtime.WriterWithFeedback) Test(org.junit.Test)

Example 5 with Sink

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

Aggregations

Sink (org.talend.components.api.component.runtime.Sink)5 ArrayList (java.util.ArrayList)3 SObject (com.sforce.soap.partner.sobject.SObject)2 List (java.util.List)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 Test (org.junit.Test)2 SourceOrSink (org.talend.components.api.component.runtime.SourceOrSink)2 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)2 TFilterRowSink (org.talend.components.filterrow.runtime.TFilterRowSink)2 TSalesforceOutputBulkProperties (org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties)2 ValidationResult (org.talend.daikon.properties.ValidationResult)2 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)2 Schema (org.apache.avro.Schema)1 GenericData (org.apache.avro.generic.GenericData)1 ComponentDefinition (org.talend.components.api.component.ComponentDefinition)1 ExecutionEngine (org.talend.components.api.component.runtime.ExecutionEngine)1 Result (org.talend.components.api.component.runtime.Result)1 Writer (org.talend.components.api.component.runtime.Writer)1 WriterDataSupplier (org.talend.components.api.component.runtime.WriterDataSupplier)1 WriterWithFeedback (org.talend.components.api.component.runtime.WriterWithFeedback)1