use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class ShellFunctionsTest method testMap2TableNullInput.
@Test
public void testMap2TableNullInput() {
Map<String, Object> variables = new HashMap<String, Object>() {
{
put("map_field", null);
}
};
Context context = Context.EMPTY_CONTEXT();
Object out = run("SHELL_MAP2TABLE(map_field)", variables, context);
Assert.assertEquals(expectedMap2TableNullInput, out);
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class ClasspathFunctionResolverTest method create.
/**
* Create a function resolver to test.
* @param config The configuration for Stellar.
*/
public static ClasspathFunctionResolver create(Properties config) {
ClasspathFunctionResolver resolver = new ClasspathFunctionResolver();
Context context = new Context.Builder().with(Context.Capabilities.STELLAR_CONFIG, () -> config).build();
resolver.initialize(context);
return resolver;
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class ConfigurationsUtils method setupStellarStatically.
public static void setupStellarStatically(CuratorFramework client, Optional<String> globalConfig) {
/*
In order to validate stellar functions, the function resolver must be initialized. Otherwise,
those utilities that require validation cannot validate the stellar expressions necessarily.
*/
Context.Builder builder = new Context.Builder().with(Context.Capabilities.ZOOKEEPER_CLIENT, () -> client);
if (globalConfig.isPresent()) {
builder = builder.with(Context.Capabilities.GLOBAL_CONFIG, () -> GLOBAL.deserialize(globalConfig.get())).with(Context.Capabilities.STELLAR_CONFIG, () -> GLOBAL.deserialize(globalConfig.get()));
} else {
builder = builder.with(Context.Capabilities.STELLAR_CONFIG, () -> new HashMap<>());
}
Context stellarContext = builder.build();
StellarFunctions.FUNCTION_RESOLVER().initialize(stellarContext);
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class GetProfileTest method testWithConfigOverride.
/**
* Values should be retrievable that were written with configuration different than current global config.
*/
@Test
public void testWithConfigOverride() {
final int periodsPerHour = 4;
final int expectedValue = 2302;
final int hours = 2;
final long startTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(hours);
final List<Object> group = Collections.emptyList();
// setup - write some measurements to be read later
final int count = hours * periodsPerHour;
ProfileMeasurement m = new ProfileMeasurement().withProfileName("profile1").withEntity("entity1").withPeriod(startTime, periodDuration, periodUnits);
profileWriter.write(m, count, group, val -> expectedValue);
// now change the executor configuration
Context context2 = setup2();
// validate it is changed in significant way
@SuppressWarnings("unchecked") Map<String, Object> global = (Map<String, Object>) context2.getCapability(Context.Capabilities.GLOBAL_CONFIG).get();
Assert.assertEquals(PROFILER_PERIOD.get(global), periodDuration2);
Assert.assertNotEquals(periodDuration, periodDuration2);
// execute - read the profile values - with (wrong) default global config values.
// No error message at this time, but returns empty results list, because
// row keys are not correctly calculated.
String expr = "PROFILE_GET('profile1', 'entity1', PROFILE_FIXED(4, 'HOURS'))";
@SuppressWarnings("unchecked") List<Integer> result = run(expr, List.class);
// validate - expect to fail to read any values
Assert.assertEquals(0, result.size());
// execute - read the profile values - with config_override.
// first two override values are strings, third is deliberately a number.
String overrides = "{'profiler.client.period.duration' : '" + periodDuration + "', " + "'profiler.client.period.duration.units' : '" + periodUnits.toString() + "', " + "'profiler.client.salt.divisor' : " + saltDivisor + " }";
expr = "PROFILE_GET('profile1', 'entity1', PROFILE_FIXED(4, 'HOURS', " + overrides + "), [], " + overrides + ")";
result = run(expr, List.class);
// validate - expect to read all values from the past 4 hours
Assert.assertEquals(count, result.size());
}
use of org.apache.metron.stellar.dsl.Context in project metron by apache.
the class GetProfileTest method setup2.
/**
* This method is similar to setup(), in that it sets up profiler configuration context,
* but only for the client. Additionally, it uses periodDuration2, periodUnits2
* and saltDivisor2, instead of periodDuration, periodUnits and saltDivisor respectively.
*
* This is used in the unit tests that test the config_overrides feature of PROFILE_GET.
* In these tests, the context from @Before setup() is used to write the data, then the global
* context is changed to context2 (from this method). Each test validates that a default read
* using global context2 then gets no valid results (as expected), and that a read using
* original context values in the PROFILE_GET config_overrides argument gets all expected results.
*
* @return context2 - The profiler client configuration context created by this method.
* The context2 values are also set in the configuration of the StellarStatefulExecutor
* stored in the global variable 'executor'. However, there is no API for querying the
* context values from a StellarStatefulExecutor, so we output the context2 Context object itself,
* for validation purposes (so that its values can be validated as being significantly
* different from the setup() settings).
*/
private Context setup2() {
state = new HashMap<>();
// global properties
Map<String, Object> global = new HashMap<String, Object>() {
{
put(PROFILER_HBASE_TABLE.getKey(), tableName);
put(PROFILER_COLUMN_FAMILY.getKey(), columnFamily);
put(PROFILER_HBASE_TABLE_PROVIDER.getKey(), MockHBaseTableProvider.class.getName());
put(PROFILER_PERIOD.getKey(), Long.toString(periodDuration2));
put(PROFILER_PERIOD_UNITS.getKey(), periodUnits2.toString());
put(PROFILER_SALT_DIVISOR.getKey(), Integer.toString(saltDivisor2));
}
};
// create the modified context
Context context2 = new Context.Builder().with(Context.Capabilities.GLOBAL_CONFIG, () -> global).build();
// create the stellar execution environment
executor = new DefaultStellarStatefulExecutor(new SimpleFunctionResolver().withClass(GetProfile.class).withClass(FixedLookback.class), context2);
// because there is no executor.getContext() method
return context2;
}
Aggregations