Search in sources :

Example 26 with Stream

use of com.hortonworks.streamline.streams.layout.component.Stream in project streamline by hortonworks.

the class StreamlineJPMMLModelRunner method toStreamLineEvents.

private Map<String, List<Object>> toStreamLineEvents(Map<FieldName, ?> predScores, final Tuple input) {
    LOG.debug("Processing tuple {}", input);
    final Set<String> inserted = new HashSet<>();
    final Map<String, List<Object>> streamsToEvents = new HashMap<>();
    final StreamlineEventImpl.Builder eventBuilder = StreamlineEventImpl.builder();
    // add to StreamlineEvent the predicted scores for PMML model predicted fields
    putPmmlScoresInEvent(predScores, inserted, eventBuilder, getPredictedFields(), "Added PMML predicted (field,val)=({},{}) to StreamlineEvent");
    // add to StreamlineEvent the predicted scores for PMML model output fields
    putPmmlScoresInEvent(predScores, inserted, eventBuilder, getOutputFields(), "Added PMML output (field,val)=({},{}) to StreamlineEvent");
    final StreamlineEvent scoredEvent = eventBuilder.build();
    LOG.debug("Scored StreamlineEvent {}", scoredEvent);
    final StreamlineEvent eventInTuple = getStreamlineEventFromTuple(input);
    for (Stream stream : outputStreams) {
        // Will contain scored and non scored events that match output fields
        final StreamlineEventImpl.Builder finalEventBuilder = StreamlineEventImpl.builder();
        finalEventBuilder.putAll(scoredEvent);
        if (eventInTuple != null) {
            // Add previous tuple's StreamlineEvent to this tuple's StreamlineEvent to pass it downstream
            Map<String, Object> nonScoredFieldsEvent = eventInTuple.entrySet().stream().filter((e) -> !inserted.contains(e.getKey()) && // include only tuple fields that are in output fields, i.e. were chosen by the user in the UI
            stream.getSchema().getFields().stream().anyMatch((of) -> of.getName().equals(e.getKey()))).peek((e) -> LOG.debug("Adding entry {} to StreamlineEvent", e)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
            if (nonScoredFieldsEvent != null) {
                finalEventBuilder.putAll(nonScoredFieldsEvent);
            }
        }
        streamsToEvents.put(stream.getId(), Collections.singletonList(finalEventBuilder.dataSourceId(modelId).build()));
    }
    return streamsToEvents;
}
Also used : Evaluator(org.jpmml.evaluator.Evaluator) Logger(org.slf4j.Logger) Stream(com.hortonworks.streamline.streams.layout.component.Stream) JPmmlModelRunner(org.apache.storm.pmml.runner.jpmml.JPmmlModelRunner) LoggerFactory(org.slf4j.LoggerFactory) ModelOutputs(org.apache.storm.pmml.model.ModelOutputs) Set(java.util.Set) HashMap(java.util.HashMap) EvaluatorUtil(org.jpmml.evaluator.EvaluatorUtil) Collectors(java.util.stream.Collectors) FieldName(org.dmg.pmml.FieldName) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) FieldValue(org.jpmml.evaluator.FieldValue) List(java.util.List) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) Tuple(org.apache.storm.tuple.Tuple) Map(java.util.Map) StreamlineEventImpl(com.hortonworks.streamline.streams.common.StreamlineEventImpl) Collections(java.util.Collections) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) StreamlineEventImpl(com.hortonworks.streamline.streams.common.StreamlineEventImpl) List(java.util.List) Stream(com.hortonworks.streamline.streams.layout.component.Stream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 27 with Stream

use of com.hortonworks.streamline.streams.layout.component.Stream in project streamline by hortonworks.

the class RulesProcessor method setRules.

public void setRules(List<Rule> rules) {
    this.rules = rules;
    Set<String> streamIds = new HashSet<>(Collections2.transform(getOutputStreams(), new Function<Stream, String>() {

        @Override
        public String apply(Stream input) {
            return input.getId();
        }
    }));
    for (Rule rule : rules) {
        for (Action action : rule.getActions()) {
            for (String stream : action.getOutputStreams()) {
                if (!streamIds.contains(stream)) {
                    String errorMsg = String.format("Action [%s] stream [%s] does not exist in processor [%s]'s output streams [%s]", action, stream, getName(), streamIds);
                    log.error(errorMsg);
                    throw new IllegalArgumentException(errorMsg);
                }
            }
        }
    }
}
Also used : Function(com.google.common.base.Function) Action(com.hortonworks.streamline.streams.layout.component.rule.action.Action) Stream(com.hortonworks.streamline.streams.layout.component.Stream) Rule(com.hortonworks.streamline.streams.layout.component.rule.Rule) HashSet(java.util.HashSet)

Aggregations

Stream (com.hortonworks.streamline.streams.layout.component.Stream)27 HashMap (java.util.HashMap)16 List (java.util.List)14 Map (java.util.Map)14 Test (org.junit.Test)13 StreamlineProcessor (com.hortonworks.streamline.streams.layout.component.StreamlineProcessor)12 Collections (java.util.Collections)12 Edge (com.hortonworks.streamline.streams.layout.component.Edge)11 StreamlineSource (com.hortonworks.streamline.streams.layout.component.StreamlineSource)11 TopologyDag (com.hortonworks.streamline.streams.layout.component.TopologyDag)11 StreamlineSink (com.hortonworks.streamline.streams.layout.component.StreamlineSink)10 RulesProcessor (com.hortonworks.streamline.streams.layout.component.impl.RulesProcessor)10 TestRunSource (com.hortonworks.streamline.streams.layout.component.impl.testing.TestRunSource)10 Optional (java.util.Optional)10 TopologyTestHelper (com.hortonworks.streamline.streams.actions.utils.TopologyTestHelper)9 TopologyStream (com.hortonworks.streamline.streams.catalog.TopologyStream)9 InputComponent (com.hortonworks.streamline.streams.layout.component.InputComponent)8 OutputComponent (com.hortonworks.streamline.streams.layout.component.OutputComponent)8 ArrayList (java.util.ArrayList)8 TestRunProcessor (com.hortonworks.streamline.streams.layout.component.impl.testing.TestRunProcessor)7