Search in sources :

Example 1 with MapVariableResolver

use of org.apache.metron.stellar.dsl.MapVariableResolver in project metron by apache.

the class TransformFilterExtractorDecorator method updateLookupKV.

/**
 * Returns true if lookupkv is not null after transforms and filtering on the value and indicator key
 * @param lkv LookupKV to transform and filter
 * @return true if lkv is not null after transform/filter
 */
private boolean updateLookupKV(LookupKV lkv, AtomicReference<Object> state) {
    Map<String, Object> ret = lkv.getValue().getMetadata();
    Map<String, Object> ind = new LinkedHashMap<>();
    String indicator = lkv.getKey().getIndicator();
    // add indicator as a resolvable variable. Also enable using resolved/transformed variables and values from operating on the value metadata
    ind.put(INDICATOR.toString(), indicator);
    Map<String, Object> stateMap = new LinkedHashMap<>();
    stateMap.put(STATE_KEY, state.get());
    MapVariableResolver resolver = new MapVariableResolver(ret, ind, globalConfig, stateMap);
    transform(valueTransforms, ret, resolver);
    transform(indicatorTransforms, ind, resolver);
    // update indicator
    Object updatedIndicator = ind.get(INDICATOR.toString());
    if (updatedIndicator != null || getCapabilities().contains(ExtractorCapabilities.STATEFUL)) {
        if (!(updatedIndicator instanceof String)) {
            throw new UnsupportedOperationException("Indicator transform must return String type");
        }
        lkv.getKey().setIndicator((String) updatedIndicator);
        boolean update = filter(indicatorFilter, resolver) && filter(valueFilter, resolver);
        if (update && !stateUpdate.isEmpty()) {
            transform(stateUpdate, stateMap, resolver);
            state.set(stateMap.get(STATE_KEY));
        }
        return update;
    } else {
        return false;
    }
}
Also used : MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver)

Example 2 with MapVariableResolver

use of org.apache.metron.stellar.dsl.MapVariableResolver in project metron by apache.

the class StellarAdapter method enrich.

@Override
public JSONObject enrich(CacheKey value) {
    Context stellarContext = (Context) value.getConfig().getConfiguration().get(STELLAR_CONTEXT_CONF);
    ConfigHandler handler = getHandler.apply(value.getConfig());
    Map<String, Object> globalConfig = value.getConfig().getConfiguration();
    Map<String, Object> sensorConfig = value.getConfig().getEnrichment().getConfig();
    if (handler == null) {
        _LOG.trace("Stellar ConfigHandler is null.");
        return new JSONObject();
    }
    Long slowLogThreshold = null;
    if (_PERF_LOG.isDebugEnabled()) {
        slowLogThreshold = ConversionUtils.convert(globalConfig.getOrDefault(STELLAR_SLOW_LOG, STELLAR_SLOW_LOG_DEFAULT), Long.class);
    }
    // Ensure that you clone the message, because process will modify the message.  If the message object is modified
    // then cache misses will happen because the cache will be modified.
    Map<String, Object> message = new HashMap<>(value.getValue(Map.class));
    VariableResolver resolver = new MapVariableResolver(message, sensorConfig, globalConfig);
    StellarProcessor processor = new StellarProcessor();
    JSONObject enriched = process(message, handler, value.getField(), slowLogThreshold, processor, resolver, stellarContext);
    _LOG.trace("Stellar Enrichment Success: {}", enriched);
    return enriched;
}
Also used : Context(org.apache.metron.stellar.dsl.Context) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) JSONObject(org.json.simple.JSONObject) VariableResolver(org.apache.metron.stellar.dsl.VariableResolver) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) HashMap(java.util.HashMap) Map(java.util.Map) ConfigHandler(org.apache.metron.common.configuration.enrichment.handler.ConfigHandler)

Example 3 with MapVariableResolver

use of org.apache.metron.stellar.dsl.MapVariableResolver in project metron by apache.

the class HdfsWriter method getHdfsPathExtension.

public String getHdfsPathExtension(String sourceType, String stellarFunction, JSONObject message) {
    // If no function is provided, just use the sourceType directly
    if (stellarFunction == null || stellarFunction.trim().isEmpty()) {
        return sourceType;
    }
    // processor is a StellarProcessor();
    VariableResolver resolver = new MapVariableResolver(message);
    Object objResult = stellarProcessor.parse(stellarFunction, resolver, StellarFunctions.FUNCTION_RESOLVER(), Context.EMPTY_CONTEXT());
    if (objResult != null && !(objResult instanceof String)) {
        throw new IllegalArgumentException("Stellar Function <" + stellarFunction + "> did not return a String value. Returned: " + objResult);
    }
    return objResult == null ? "" : (String) objResult;
}
Also used : MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) JSONObject(org.json.simple.JSONObject) VariableResolver(org.apache.metron.stellar.dsl.VariableResolver) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver)

Example 4 with MapVariableResolver

use of org.apache.metron.stellar.dsl.MapVariableResolver in project metron by apache.

the class DefaultStellarStatefulExecutor method execute.

/**
 * Execute a Stellar expression.
 *
 * @param expression     The expression to execute.
 * @param transientState Additional state available to the expression.  This most often represents
 *                       the values available to the expression from an individual message. The state
 *                       maps a variable name to a variable's value.
 */
private Object execute(String expression, Map<String, Object> transientState) {
    VariableResolver variableResolver = new MapVariableResolver(state, transientState);
    StellarProcessor processor = new StellarProcessor();
    return processor.parse(expression, variableResolver, functionResolver, context);
}
Also used : MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) VariableResolver(org.apache.metron.stellar.dsl.VariableResolver) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver)

Example 5 with MapVariableResolver

use of org.apache.metron.stellar.dsl.MapVariableResolver in project metron by apache.

the class StellarMicrobenchmark method main.

public static void main(String... argv) throws IOException {
    CommandLine cli = BenchmarkOptions.parse(new PosixParser(), argv);
    if (!BenchmarkOptions.EXPRESSIONS.has(cli)) {
        throw new IllegalStateException("You must at least specify an expressions file.");
    }
    File expressionsFile = new File(BenchmarkOptions.EXPRESSIONS.get(cli));
    Optional<File> variablesFile = Optional.ofNullable(!BenchmarkOptions.VARIABLES.has(cli) ? null : new File(BenchmarkOptions.VARIABLES.get(cli)));
    Optional<File> output = Optional.ofNullable(!BenchmarkOptions.OUTPUT.has(cli) ? null : new File(BenchmarkOptions.OUTPUT.get(cli)));
    List<String> lines = Files.readLines(expressionsFile, Charset.defaultCharset());
    Map<String, Object> variables = new HashMap<>();
    if (variablesFile.isPresent()) {
        variables = JSONUtils.INSTANCE.load(new FileInputStream(variablesFile.get()), JSONUtils.MAP_SUPPLIER);
    }
    int numTimes = DEFAULT_NUM_TIMES;
    if (BenchmarkOptions.NUM_TIMES.has(cli)) {
        numTimes = Integer.parseInt(BenchmarkOptions.NUM_TIMES.get(cli));
    }
    int warmup = DEFAULT_WARMUP;
    if (BenchmarkOptions.WARMUP.has(cli)) {
        warmup = Integer.parseInt(BenchmarkOptions.WARMUP.get(cli));
    }
    Double[] percentiles = DEFAULT_PERCENTILES;
    if (BenchmarkOptions.PERCENTILES.has(cli)) {
        List<Double> percentileList = new ArrayList<>();
        for (String token : Splitter.on(",").split(BenchmarkOptions.PERCENTILES.get(cli))) {
            if (token.trim().isEmpty()) {
                continue;
            }
            Double d = Double.parseDouble(token.trim());
            percentileList.add(d);
        }
        percentiles = (Double[]) percentileList.toArray();
    }
    PrintWriter out = new PrintWriter(System.out);
    if (output.isPresent()) {
        out = new PrintWriter(output.get());
    }
    for (String statement : lines) {
        if (statement.trim().startsWith("#") || statement.trim().isEmpty()) {
            continue;
        }
        Microbenchmark.StellarStatement s = new Microbenchmark.StellarStatement();
        s.context = Context.EMPTY_CONTEXT();
        s.expression = statement;
        s.functionResolver = StellarFunctions.FUNCTION_RESOLVER();
        s.variableResolver = new MapVariableResolver(variables);
        DescriptiveStatistics stats = Microbenchmark.run(s, warmup, numTimes);
        out.println("Expression: " + statement);
        out.println(Microbenchmark.describe(stats, percentiles));
    }
    if (argv.length > 2) {
        out.close();
    }
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) HashMap(java.util.HashMap) PosixParser(org.apache.commons.cli.PosixParser) ArrayList(java.util.ArrayList) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) FileInputStream(java.io.FileInputStream) CommandLine(org.apache.commons.cli.CommandLine) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

MapVariableResolver (org.apache.metron.stellar.dsl.MapVariableResolver)10 VariableResolver (org.apache.metron.stellar.dsl.VariableResolver)7 StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Context (org.apache.metron.stellar.dsl.Context)2 JSONObject (org.json.simple.JSONObject)2 Function (com.google.common.base.Function)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 CommandLine (org.apache.commons.cli.CommandLine)1 PosixParser (org.apache.commons.cli.PosixParser)1 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)1 Aggregators (org.apache.metron.common.aggregator.Aggregators)1