Search in sources :

Example 1 with ChainedMapper

use of org.talend.sdk.component.runtime.manager.chain.ChainedMapper in project component-runtime by Talend.

the class DIBatchSimulationTest method doRun.

private void doRun(final ComponentManager manager, final Collection<Object> sourceData, final Collection<Object> processorData, final Map<String, Object> globalMap, final AutoChunkProcessor processorProcessor, final InputsHandler inputsHandlerProcessor, final OutputsHandler outputHandlerProcessor, final InputFactory inputsProcessor, final OutputFactory outputsProcessor, final Mapper tempMapperMapper) {
    row1Struct row1 = new row1Struct();
    tempMapperMapper.start();
    final ChainedMapper mapperMapper;
    try {
        final List<Mapper> splitMappersMapper = tempMapperMapper.split(tempMapperMapper.assess());
        mapperMapper = new ChainedMapper(tempMapperMapper, splitMappersMapper.iterator());
        mapperMapper.start();
        globalMap.put("mapperMapper", mapperMapper);
    } finally {
        try {
            tempMapperMapper.stop();
        } catch (final RuntimeException re) {
            re.printStackTrace();
        }
    }
    final Input inputMapper = mapperMapper.create();
    inputMapper.start();
    globalMap.put("inputMapper", inputMapper);
    final Jsonb jsonbMapper = Jsonb.class.cast(manager.findPlugin(mapperMapper.plugin()).get().get(ComponentManager.AllServices.class).getServices().get(Jsonb.class));
    Object dataMapper;
    while ((dataMapper = inputMapper.next()) != null) {
        final String jsonValueMapper = javax.json.JsonValue.class.isInstance(dataMapper) ? javax.json.JsonValue.class.cast(dataMapper).toString() : jsonbMapper.toJson(dataMapper);
        row1 = jsonbMapper.fromJson(jsonValueMapper, row1.getClass());
        sourceData.add(row1);
        inputsHandlerProcessor.reset();
        inputsHandlerProcessor.setInputValue("FLOW", row1);
        outputHandlerProcessor.reset();
        processorProcessor.onElement(name -> {
            assertEquals(Branches.DEFAULT_BRANCH, name);
            final Object read = inputsProcessor.read(name);
            processorData.add(read);
            return read;
        }, outputsProcessor);
    }
}
Also used : ToString(lombok.ToString) PartitionMapper(org.talend.sdk.component.api.input.PartitionMapper) ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) Mapper(org.talend.sdk.component.runtime.input.Mapper) Input(org.talend.sdk.component.runtime.input.Input) Jsonb(javax.json.bind.Jsonb) ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) JsonObject(javax.json.JsonObject)

Example 2 with ChainedMapper

use of org.talend.sdk.component.runtime.manager.chain.ChainedMapper in project component-runtime by Talend.

the class TaCoKitGuessSchema method guessInputComponentSchemaThroughResult.

private boolean guessInputComponentSchemaThroughResult() throws Exception {
    final Mapper mapper = componentManager.findMapper(family, componentName, 1, configuration).orElseThrow(() -> new IllegalArgumentException("Can't find " + family + "#" + componentName));
    if (JobStateAware.class.isInstance(mapper)) {
        JobStateAware.class.cast(mapper).setState(new JobStateAware.State());
    }
    Input input = null;
    try {
        mapper.start();
        final ChainedMapper chainedMapper = new ChainedMapper(mapper, mapper.split(mapper.assess()).iterator());
        chainedMapper.start();
        input = chainedMapper.create();
        input.start();
        Object rowObject = input.next();
        if (rowObject == null) {
            return false;
        }
        if (rowObject instanceof java.util.Map) {
            return guessInputSchemaThroughResults(input, (java.util.Map) rowObject);
        } else if (rowObject instanceof java.util.Collection) {
            throw new Exception("Can't guess schema from a Collection");
        } else {
            return guessSchemaThroughResultClass(rowObject.getClass());
        }
    } finally {
        if (input != null) {
            try {
                input.stop();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        try {
            mapper.stop();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
Also used : ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) Mapper(org.talend.sdk.component.runtime.input.Mapper) Input(org.talend.sdk.component.runtime.input.Input) JobStateAware(org.talend.sdk.component.runtime.di.JobStateAware) Collection(java.util.Collection) ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) JsonObject(javax.json.JsonObject) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map)

Aggregations

JsonObject (javax.json.JsonObject)2 Input (org.talend.sdk.component.runtime.input.Input)2 Mapper (org.talend.sdk.component.runtime.input.Mapper)2 ChainedMapper (org.talend.sdk.component.runtime.manager.chain.ChainedMapper)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Jsonb (javax.json.bind.Jsonb)1 ToString (lombok.ToString)1 PartitionMapper (org.talend.sdk.component.api.input.PartitionMapper)1 JobStateAware (org.talend.sdk.component.runtime.di.JobStateAware)1