Search in sources :

Example 6 with Rule

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

the class WindowRuleBoltFluxComponent method addWindowConfig.

private String addWindowConfig() {
    String windowId = "window" + UUID_FOR_COMPONENTS;
    String windowClassName = "com.hortonworks.streamline.streams.layout.component.rule.expression.Window";
    ObjectMapper mapper = new ObjectMapper();
    String windowJson = null;
    try {
        Set<Window> windows = new HashSet<>(Collections2.transform(rulesProcessor.getRules(), new Function<Rule, Window>() {

            @Override
            public Window apply(Rule input) {
                return input.getWindow();
            }
        }));
        if (windows.size() != 1) {
            throw new IllegalArgumentException("All the rules in a windowed rule bolt should have the same window config.");
        }
        windowJson = mapper.writeValueAsString(windows.iterator().next());
    } catch (JsonProcessingException e) {
        log.error("Error creating json config string for RulesProcessor", e);
    }
    List<String> constructorArgs = new ArrayList<>();
    constructorArgs.add(windowJson);
    this.addToComponents(this.createComponent(windowId, windowClassName, null, constructorArgs, null));
    return windowId;
}
Also used : Window(com.hortonworks.streamline.streams.layout.component.rule.expression.Window) Function(com.google.common.base.Function) ArrayList(java.util.ArrayList) Rule(com.hortonworks.streamline.streams.layout.component.rule.Rule) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Example 7 with Rule

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

the class RulesProcessorMock method getRule.

private Rule getRule(long ruleId, Condition condition, TransformAction action) {
    Rule rule = new Rule();
    rule.setId(ruleId);
    rule.setName(RULE + "_" + ruleId);
    rule.setDescription(RULE + "_" + ruleId + "_desc");
    rule.setRuleProcessorName(RULE_PROCESSOR + "_" + ruleProcessorId);
    rule.setCondition(condition);
    if (ruleId % 2 == 0) {
        Projection projection = new Projection();
        Expression humidity = new FieldExpression(Field.of("humidity", Schema.Type.INTEGER));
        Expression deviceName = new FieldExpression(Field.of("devicename", Schema.Type.STRING));
        Expression incr = new FunctionExpression("INCR", "com.hortonworks.streamline.streams.runtime.storm.layout.runtime.rule.topology.RulesProcessorMock$Incr", ImmutableList.<Expression>of(humidity, new Literal("10")));
        Expression upper = new FunctionExpression("UPPER", ImmutableList.<Expression>of(deviceName));
        projection.setExpressions(ImmutableList.<Expression>of(humidity, incr, upper));
        rule.setProjection(projection);
    }
    rule.setActions(Collections.singletonList((Action) action));
    return rule;
}
Also used : Action(com.hortonworks.streamline.streams.layout.component.rule.action.Action) TransformAction(com.hortonworks.streamline.streams.layout.component.rule.action.TransformAction) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) Literal(com.hortonworks.streamline.streams.layout.component.rule.expression.Literal) Projection(com.hortonworks.streamline.streams.layout.component.rule.expression.Projection) Rule(com.hortonworks.streamline.streams.layout.component.rule.Rule) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)

Example 8 with Rule

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

the class StormTopologyFluxGenerator method visit.

@Override
public void visit(final RulesProcessor rulesProcessor) {
    rulesProcessor.getConfig().setAny("outputStreams", rulesProcessor.getOutputStreams());
    List<Rule> rulesWithWindow = new ArrayList<>();
    List<Rule> rulesWithoutWindow = new ArrayList<>();
    Set<String> inStreams = topologyDag.getEdgesTo(rulesProcessor).stream().flatMap(e -> e.getStreamGroupings().stream().map(sg -> sg.getStream().getId())).collect(Collectors.toSet());
    for (Rule rule : rulesProcessor.getRules()) {
        if (!inStreams.containsAll(rule.getStreams())) {
            throw new IllegalStateException("Input streams of rules processor " + inStreams + " does not contain rule's input streams " + rule.getStreams() + ". Please delete and recreate the rule.");
        }
        if (rule.getWindow() != null) {
            rulesWithWindow.add(rule);
        } else {
            rulesWithoutWindow.add(rule);
        }
    }
    // assert that RulesProcessor doesn't have mixed kinds of rules.
    if (!rulesWithWindow.isEmpty() && !rulesWithoutWindow.isEmpty()) {
        throw new IllegalStateException("RulesProcessor should have either windowed or non-windowed rules, not both.");
    }
    // associating multiple bolts to a rules processor is not allowed for simplicity.
    if (!rulesWithWindow.isEmpty()) {
        handleWindowedRules(rulesProcessor, rulesWithWindow);
    } else {
        // !rulesWithoutWindow.isEmpty()
        handleNonWindowedRules(rulesProcessor, rulesWithoutWindow);
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Rule(com.hortonworks.streamline.streams.layout.component.rule.Rule) StormTopologyUtil(com.hortonworks.streamline.streams.storm.common.StormTopologyUtil) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Collections2(com.google.common.collect.Collections2) TopologyLayout(com.hortonworks.streamline.streams.layout.component.TopologyLayout) Multimap(com.google.common.collect.Multimap) RulesProcessor(com.hortonworks.streamline.streams.layout.component.impl.RulesProcessor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Edge(com.hortonworks.streamline.streams.layout.component.Edge) TopologyDag(com.hortonworks.streamline.streams.layout.component.TopologyDag) YAML_KEY_ID(com.hortonworks.streamline.streams.layout.storm.StormTopologyLayoutConstants.YAML_KEY_ID) TopologyDagVisitor(com.hortonworks.streamline.streams.layout.component.TopologyDagVisitor) Map(java.util.Map) YAML_KEY_STREAMS(com.hortonworks.streamline.streams.layout.storm.StormTopologyLayoutConstants.YAML_KEY_STREAMS) StreamlineSource(com.hortonworks.streamline.streams.layout.component.StreamlineSource) Component(com.hortonworks.streamline.streams.layout.component.Component) Path(java.nio.file.Path) Config(com.hortonworks.streamline.common.Config) StreamGrouping(com.hortonworks.streamline.streams.layout.component.StreamGrouping) YAML_KEY_FROM(com.hortonworks.streamline.streams.layout.storm.StormTopologyLayoutConstants.YAML_KEY_FROM) Function(com.google.common.base.Function) OutputComponent(com.hortonworks.streamline.streams.layout.component.OutputComponent) Logger(org.slf4j.Logger) Stream(com.hortonworks.streamline.streams.layout.component.Stream) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) InputComponent(com.hortonworks.streamline.streams.layout.component.InputComponent) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractMap(java.util.AbstractMap) List(java.util.List) YAML_KEY_TO(com.hortonworks.streamline.streams.layout.storm.StormTopologyLayoutConstants.YAML_KEY_TO) Window(com.hortonworks.streamline.streams.layout.component.rule.expression.Window) StreamlineProcessor(com.hortonworks.streamline.streams.layout.component.StreamlineProcessor) StreamlineSink(com.hortonworks.streamline.streams.layout.component.StreamlineSink) Collections(java.util.Collections) ArrayList(java.util.ArrayList) Rule(com.hortonworks.streamline.streams.layout.component.rule.Rule)

Example 9 with Rule

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

the class RuleProcessorRuntime method initializeRuleRuntimes.

private void initializeRuleRuntimes(Map<String, Object> config) {
    List<Rule> rules = rulesProcessor.getRules();
    if (rules != null) {
        for (Rule rule : rules) {
            RuleRuntime ruleRuntime;
            Script script = null;
            if (ScriptType.GROOVY.equals(scriptType)) {
                script = createGroovyScript(rule);
            } else if (ScriptType.SQL.equals(scriptType)) {
                script = createSqlScript(rule);
            } else {
                throw new RuntimeException("Ruleruntime scriptType unsupported: " + scriptType);
            }
            ruleRuntime = new RuleRuntime(rule, script, createActionRuntimes(rule));
            rulesRuntime.add(ruleRuntime);
            ruleRuntime.initialize(config);
        }
        LOG.info("ruleRuntimes [{}]", rulesRuntime);
    }
}
Also used : Script(com.hortonworks.streamline.streams.runtime.script.Script) GroovyScript(com.hortonworks.streamline.streams.runtime.script.GroovyScript) SqlScript(com.hortonworks.streamline.streams.runtime.rule.sql.SqlScript) Rule(com.hortonworks.streamline.streams.layout.component.rule.Rule) RuleRuntime(com.hortonworks.streamline.streams.runtime.rule.RuleRuntime)

Example 10 with Rule

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

the class Utils method createTrueRule.

/**
 * Returns a rule with no condition(which is equivalent to true) with the given action.
 *
 * @param action Action to be set on the rule
 */
public static Rule createTrueRule(final Action action) {
    Rule rule = new Rule() {

        {
            setName(action.getName() + "-rule");
            setId(System.currentTimeMillis());
        }
    };
    rule.setActions(Collections.singletonList(action));
    return rule;
}
Also used : Rule(com.hortonworks.streamline.streams.layout.component.rule.Rule)

Aggregations

Rule (com.hortonworks.streamline.streams.layout.component.rule.Rule)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 TopologyBranchRule (com.hortonworks.streamline.streams.catalog.TopologyBranchRule)7 TopologyRule (com.hortonworks.streamline.streams.catalog.TopologyRule)7 Function (com.google.common.base.Function)5 HashSet (java.util.HashSet)5 BaseTopologyRule (com.hortonworks.streamline.streams.catalog.BaseTopologyRule)4 Stream (com.hortonworks.streamline.streams.layout.component.Stream)4 RulesProcessor (com.hortonworks.streamline.streams.layout.component.impl.RulesProcessor)4 ArrayList (java.util.ArrayList)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Collections2 (com.google.common.collect.Collections2)2 QueryParam (com.hortonworks.registries.common.QueryParam)2 TopologyWindow (com.hortonworks.streamline.streams.catalog.TopologyWindow)2 UDF (com.hortonworks.streamline.streams.catalog.UDF)2 Window (com.hortonworks.streamline.streams.layout.component.rule.expression.Window)2 List (java.util.List)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1