Search in sources :

Example 1 with ProcessingException

use of com.hortonworks.streamline.streams.exception.ProcessingException in project streamline by hortonworks.

the class CustomProcessorBolt method process.

@Override
protected void process(Tuple input, StreamlineEvent event) {
    try {
        StreamlineEvent toProcess = null;
        if (inputSchemaMap == null || inputSchemaMap.isEmpty() || !inputSchemaMap.containsKey(input.getSourceStreamId())) {
            toProcess = event;
        } else {
            // Create a new mapped event based on mapping to pass it to CP implementation
            Map<String, Object> mappedEventMap = new HashMap<>();
            for (Map.Entry<String, String> entry : inputSchemaMap.get(input.getSourceStreamId()).entrySet()) {
                if (event.get(entry.getValue()) != null) {
                    mappedEventMap.put(entry.getKey(), event.get(entry.getValue()));
                }
            }
            toProcess = StreamlineEventImpl.builder().from(event).sourceStream(input.getSourceStreamId()).fieldsAndValues(mappedEventMap).build();
        }
        List<StreamlineEvent> results = customProcessorRuntime.process(toProcess);
        if (results != null) {
            Schema schema = outputSchema.values().iterator().next();
            String outputStream = outputSchema.keySet().iterator().next();
            for (StreamlineEvent e : results) {
                Map<String, Object> newFieldsAndValues = new HashMap<>();
                // event and CP defined output schema. UI will make sure that the fields are from one of the two sets.
                for (Schema.Field field : schema.getFields()) {
                    // value has to be present either in the input event
                    newFieldsAndValues.put(field.getName(), e.containsKey(field.getName()) ? e.get(field.getName()) : event.get(field.getName()));
                }
                StreamlineEvent toEmit = StreamlineEventImpl.builder().from(e).sourceStream(outputStream).fieldsAndValues(newFieldsAndValues).build();
                collector.emit(outputStream, input, new Values(toEmit));
            }
        }
    } catch (ProcessingException e) {
        LOG.error("Custom Processor threw a ProcessingException. ", e);
        throw new RuntimeException(e);
    }
}
Also used : StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Schema(com.hortonworks.registries.common.Schema) Values(org.apache.storm.tuple.Values) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ProcessingException(com.hortonworks.streamline.streams.exception.ProcessingException)

Example 2 with ProcessingException

use of com.hortonworks.streamline.streams.exception.ProcessingException in project streamline by hortonworks.

the class WindowRulesBolt method processAndEmit.

private void processAndEmit(StreamlineEvent event, Map<String, Tuple> curGroup) throws ProcessingException {
    List<Result> results = ruleProcessorRuntime.process(eventWithWindowId(event));
    for (Result result : results) {
        for (StreamlineEvent e : result.events) {
            // anchor parent events if such information is available
            if (EventCorrelationInjector.containsParentIds(e)) {
                Set<String> parentIds = EventCorrelationInjector.getParentIds(e);
                List<Tuple> parents = parentIds.stream().map(pid -> {
                    if (!curGroup.containsKey(pid)) {
                        throw new IllegalStateException("parents should be in grouped tuples");
                    }
                    return curGroup.get(pid);
                }).collect(Collectors.toList());
                collector.emit(result.stream, parents, new Values(updateHeaders(e, parents)));
            } else {
                // put all events in current group if there's no information
                collector.emit(result.stream, curGroup.values(), new Values(updateHeaders(e, curGroup.values())));
            }
        }
    }
}
Also used : OutputFieldsDeclarer(org.apache.storm.topology.OutputFieldsDeclarer) IdPreservedStreamlineEvent(com.hortonworks.streamline.streams.common.IdPreservedStreamlineEvent) Utils(com.hortonworks.streamline.common.util.Utils) TopologyContext(org.apache.storm.task.TopologyContext) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) RulesProcessor(com.hortonworks.streamline.streams.layout.component.impl.RulesProcessor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HEADER_FIELD_EVENT_IDS(com.hortonworks.streamline.streams.runtime.transform.AddHeaderTransformRuntime.HEADER_FIELD_EVENT_IDS) AbstractWindowedProcessorBolt(com.hortonworks.streamline.streams.runtime.storm.bolt.AbstractWindowedProcessorBolt) Result(com.hortonworks.streamline.streams.Result) RuleProcessorRuntime(com.hortonworks.streamline.streams.runtime.processor.RuleProcessorRuntime) Values(org.apache.storm.tuple.Values) GROUP_BY_TRIGGER_EVENT(com.hortonworks.streamline.streams.common.StreamlineEventImpl.GROUP_BY_TRIGGER_EVENT) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) Tuple(org.apache.storm.tuple.Tuple) TupleWindow(org.apache.storm.windowing.TupleWindow) OutputCollector(org.apache.storm.task.OutputCollector) Map(java.util.Map) StreamlineEventImpl(com.hortonworks.streamline.streams.common.StreamlineEventImpl) Logger(org.slf4j.Logger) Stream(com.hortonworks.streamline.streams.layout.component.Stream) ProcessingException(com.hortonworks.streamline.streams.exception.ProcessingException) Collection(java.util.Collection) StreamlineWindowedBolt(com.hortonworks.streamline.streams.runtime.storm.bolt.StreamlineWindowedBolt) Set(java.util.Set) Fields(org.apache.storm.tuple.Fields) Collectors(java.util.stream.Collectors) HEADER_FIELD_DATASOURCE_IDS(com.hortonworks.streamline.streams.runtime.transform.AddHeaderTransformRuntime.HEADER_FIELD_DATASOURCE_IDS) List(java.util.List) Window(com.hortonworks.streamline.streams.layout.component.rule.expression.Window) Collections(java.util.Collections) EventCorrelationInjector(com.hortonworks.streamline.streams.common.event.correlation.EventCorrelationInjector) IdPreservedStreamlineEvent(com.hortonworks.streamline.streams.common.IdPreservedStreamlineEvent) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) Values(org.apache.storm.tuple.Values) Tuple(org.apache.storm.tuple.Tuple) Result(com.hortonworks.streamline.streams.Result)

Example 3 with ProcessingException

use of com.hortonworks.streamline.streams.exception.ProcessingException in project streamline by hortonworks.

the class RuleRuntime method process.

/**
 * Executes a {@link Rule}'s Action
 *
 * @param event runtime input to this rule
 */
@Override
public List<Result> process(StreamlineEvent event) throws ProcessingException {
    LOG.debug("process invoked with StreamlineEvent {}", event);
    List<Result> allResults = new ArrayList<>();
    try {
        for (ActionRuntime action : actions) {
            List<Result> actionResults = action.execute(event);
            LOG.debug("Applied action {}, Result {}", action, actionResults);
            if (actionResults != null) {
                allResults.addAll(actionResults);
            }
        }
    } catch (Exception e) {
        String message = "Error evaluating rule with id:" + rule.getId();
        LOG.error(message);
        throw new ProcessingException(message, e);
    }
    LOG.debug("Returning allResults {}", allResults);
    return allResults;
}
Also used : ArrayList(java.util.ArrayList) ActionRuntime(com.hortonworks.streamline.streams.runtime.rule.action.ActionRuntime) ProcessingException(com.hortonworks.streamline.streams.exception.ProcessingException) ConditionEvaluationException(com.hortonworks.streamline.streams.layout.component.rule.exception.ConditionEvaluationException) ScriptException(javax.script.ScriptException) Result(com.hortonworks.streamline.streams.Result) ProcessingException(com.hortonworks.streamline.streams.exception.ProcessingException)

Example 4 with ProcessingException

use of com.hortonworks.streamline.streams.exception.ProcessingException in project streamline by hortonworks.

the class CustomProcessorBoltTest method testExecute.

private void testExecute(boolean isSuccess) throws ProcessingException, ClassNotFoundException, MalformedURLException, InstantiationException, IllegalAccessException {
    customProcessorBolt.customProcessorImpl(ConsoleCustomProcessor.class.getCanonicalName());
    customProcessorBolt.outputSchema(outputStreamToSchema);
    Map<String, Object> data = new HashMap<>();
    data.put("key", "value");
    final StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(data).dataSourceId("dsrcid").build();
    final List<StreamlineEvent> result = Arrays.asList(event);
    final ProcessingException pe = new ProcessingException("Test");
    new Expectations() {

        {
            tuple.getSourceComponent();
            returns("datasource");
            tuple.getSourceStreamId();
            returns(stream);
            tuple.getValueByField(StreamlineEvent.STREAMLINE_EVENT);
            returns(event);
        }
    };
    if (!isSuccess) {
        new Expectations() {

            {
                customProcessorRuntime.process(event);
                result = pe;
            }
        };
    } else {
        new Expectations() {

            {
                customProcessorRuntime.process(event);
                returns(result);
            }
        };
    }
    Map<Object, Object> conf = new HashMap<>();
    customProcessorBolt.prepare(conf, null, mockOutputCollector);
    customProcessorBolt.execute(tuple);
    if (!isSuccess) {
        new VerificationsInOrder() {

            {
                RuntimeException e;
                customProcessorRuntime.process(event);
                times = 1;
                mockOutputCollector.fail(tuple);
                times = 1;
                mockOutputCollector.reportError(e = withCapture());
                assertTrue(e.getCause() == pe);
            }
        };
    } else {
        new VerificationsInOrder() {

            {
                tuple.getSourceComponent();
                times = 1;
                tuple.getSourceStreamId();
                times = 1;
                StreamlineEvent actual;
                customProcessorRuntime.process(actual = withCapture());
                times = 1;
                Assert.assertEquals(actual.getSourceStream(), stream);
                Assert.assertEquals(actual, event);
                Values actualValues;
                mockOutputCollector.emit(outputStream, tuple, actualValues = withCapture());
                times = 1;
                mockOutputCollector.ack(tuple);
                times = 1;
            }
        };
    }
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) ConsoleCustomProcessor(com.hortonworks.streamline.examples.processors.ConsoleCustomProcessor) Values(org.apache.storm.tuple.Values) VerificationsInOrder(mockit.VerificationsInOrder) ProcessingException(com.hortonworks.streamline.streams.exception.ProcessingException)

Example 5 with ProcessingException

use of com.hortonworks.streamline.streams.exception.ProcessingException in project streamline by hortonworks.

the class RuleProcessorRuntime method process.

@Override
public List<Result> process(StreamlineEvent event) throws ProcessingException {
    List<Result> results = new ArrayList<>();
    try {
        List<RuleRuntime> ruleRuntimes = getRulesRuntime(event);
        LOG.debug("Process event {}, rule runtimes {}", event, ruleRuntimes);
        for (RuleRuntime rr : ruleRuntimes) {
            boolean succeeded = false;
            for (StreamlineEvent result : rr.evaluate(event)) {
                if (result != null) {
                    results.addAll(rr.process(result));
                    succeeded = true;
                }
            }
            if (!processAll && succeeded)
                break;
        }
    } catch (Exception e) {
        String message = String.format("Error evaluating rule processor with id: %s, error: %s", rulesProcessor.getId(), e.getMessage());
        LOG.error(message, e);
        throw new ProcessingException(message, e);
    }
    return results;
}
Also used : StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) ArrayList(java.util.ArrayList) RuleRuntime(com.hortonworks.streamline.streams.runtime.rule.RuleRuntime) ScriptException(javax.script.ScriptException) ProcessingException(com.hortonworks.streamline.streams.exception.ProcessingException) Result(com.hortonworks.streamline.streams.Result) ProcessingException(com.hortonworks.streamline.streams.exception.ProcessingException)

Aggregations

ProcessingException (com.hortonworks.streamline.streams.exception.ProcessingException)8 Result (com.hortonworks.streamline.streams.Result)5 StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)5 HashMap (java.util.HashMap)4 Values (org.apache.storm.tuple.Values)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 IOException (java.io.IOException)2 List (java.util.List)2 ScriptException (javax.script.ScriptException)2 Schema (com.hortonworks.registries.common.Schema)1 ConfigException (com.hortonworks.streamline.common.exception.ConfigException)1 Utils (com.hortonworks.streamline.common.util.Utils)1 ConsoleCustomProcessor (com.hortonworks.streamline.examples.processors.ConsoleCustomProcessor)1 IdPreservedStreamlineEvent (com.hortonworks.streamline.streams.common.IdPreservedStreamlineEvent)1 StreamlineEventImpl (com.hortonworks.streamline.streams.common.StreamlineEventImpl)1 GROUP_BY_TRIGGER_EVENT (com.hortonworks.streamline.streams.common.StreamlineEventImpl.GROUP_BY_TRIGGER_EVENT)1 EventCorrelationInjector (com.hortonworks.streamline.streams.common.event.correlation.EventCorrelationInjector)1 ProcessorMsg (com.hortonworks.streamline.streams.common.utils.ProcessorMsg)1 ShellMsg (com.hortonworks.streamline.streams.common.utils.ShellMsg)1