Search in sources :

Example 6 with StellarProcessor

use of org.apache.metron.stellar.common.StellarProcessor in project metron by apache.

the class WindowLookbackTest method testSpecifyingOnlySelector.

@Test
public void testSpecifyingOnlySelector() {
    String stellarStatement = "PROFILE_WINDOW('1 hour')";
    Map<String, Object> variables = new HashMap<>();
    StellarProcessor stellar = new StellarProcessor();
    List<ProfilePeriod> periods = (List<ProfilePeriod>) stellar.parse(stellarStatement, new DefaultVariableResolver(k -> variables.get(k), k -> variables.containsKey(k)), resolver, context);
    Assert.assertEquals(TimeUnit.HOURS.toMillis(1) / getDurationMs(), periods.size());
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Test(org.junit.Test)

Example 7 with StellarProcessor

use of org.apache.metron.stellar.common.StellarProcessor in project metron by apache.

the class WindowLookbackTest method test.

public State test(String windowSelector, Date now, Optional<Map<String, Object>> config, Assertions... assertions) {
    List<Range<Long>> windowIntervals = WindowProcessor.process(windowSelector).toIntervals(now.getTime());
    String stellarStatement = "PROFILE_WINDOW('" + windowSelector + "', now" + (config.isPresent() ? ", config" : "") + ")";
    Map<String, Object> variables = new HashMap<>();
    variables.put("now", now.getTime());
    if (config.isPresent()) {
        variables.put("config", config.get());
    }
    StellarProcessor stellar = new StellarProcessor();
    List<ProfilePeriod> periods = (List<ProfilePeriod>) stellar.parse(stellarStatement, new DefaultVariableResolver(k -> variables.get(k), k -> variables.containsKey(k)), resolver, context);
    State state = new State(windowIntervals, periods);
    for (Assertions assertion : assertions) {
        Assert.assertTrue(assertion.name(), assertion.test(state));
    }
    return state;
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) Range(org.apache.commons.lang3.Range) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 8 with StellarProcessor

use of org.apache.metron.stellar.common.StellarProcessor 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 9 with StellarProcessor

use of org.apache.metron.stellar.common.StellarProcessor in project metron by apache.

the class HdfsWriter method init.

@Override
public void init(Map stormConfig, TopologyContext topologyContext, WriterConfiguration configurations) {
    this.stormConfig = stormConfig;
    this.stellarProcessor = new StellarProcessor();
    this.fileNameFormat.prepare(stormConfig, topologyContext);
    if (syncPolicy != null) {
        // if the user has specified the sync policy, we don't want to override their wishes.
        syncPolicyCreator = new ClonedSyncPolicyCreator(syncPolicy);
    } else {
        // if the user has not, then we want to have the sync policy depend on the batch size.
        syncPolicyCreator = (source, config) -> new CountSyncPolicy(config == null ? 1 : config.getBatchSize(source));
    }
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) CountSyncPolicy(org.apache.storm.hdfs.bolt.sync.CountSyncPolicy)

Example 10 with StellarProcessor

use of org.apache.metron.stellar.common.StellarProcessor in project metron by apache.

the class KafkaFunctionsIntegrationTest method run.

/**
 * Runs a Stellar expression.
 * @param expr The expression to run.
 */
private Object run(String expr) {
    // make the global properties available to the function
    Context context = new Context.Builder().with(Context.Capabilities.GLOBAL_CONFIG, () -> global).build();
    FunctionResolver functionResolver = new SimpleFunctionResolver().withClass(KafkaFunctions.KafkaGet.class).withClass(KafkaFunctions.KafkaPut.class).withClass(KafkaFunctions.KafkaProps.class).withClass(KafkaFunctions.KafkaTail.class);
    StellarProcessor processor = new StellarProcessor();
    return processor.parse(expr, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), functionResolver, context);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ComponentRunner(org.apache.metron.integration.ComponentRunner) FunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.FunctionResolver) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) BeforeClass(org.junit.BeforeClass) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) Test(org.junit.Test) HashMap(java.util.HashMap) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) KafkaComponent(org.apache.metron.integration.components.KafkaComponent) ArrayList(java.util.ArrayList) List(java.util.List) BaseIntegrationTest(org.apache.metron.integration.BaseIntegrationTest) After(org.junit.After) Map(java.util.Map) ZKServerComponent(org.apache.metron.integration.components.ZKServerComponent) SimpleFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.SimpleFunctionResolver) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Context(org.apache.metron.stellar.dsl.Context) Before(org.junit.Before) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) SimpleFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.SimpleFunctionResolver) FunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.FunctionResolver) SimpleFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.SimpleFunctionResolver)

Aggregations

StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)22 Context (org.apache.metron.stellar.dsl.Context)11 DefaultVariableResolver (org.apache.metron.stellar.dsl.DefaultVariableResolver)11 Map (java.util.Map)10 StellarFunctions (org.apache.metron.stellar.dsl.StellarFunctions)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 Assert (org.junit.Assert)7 ImmutableMap (com.google.common.collect.ImmutableMap)5 List (java.util.List)5 ImmutableList (com.google.common.collect.ImmutableList)4 ArrayList (java.util.ArrayList)4 MapVariableResolver (org.apache.metron.stellar.dsl.MapVariableResolver)4 ParseException (org.apache.metron.stellar.dsl.ParseException)4 Before (org.junit.Before)4 StellarPredicateProcessor (org.apache.metron.stellar.common.StellarPredicateProcessor)3 VariableResolver (org.apache.metron.stellar.dsl.VariableResolver)3 JSONObject (org.json.simple.JSONObject)3 Collectors (java.util.stream.Collectors)2 GaussianRandomGenerator (org.apache.commons.math3.random.GaussianRandomGenerator)2