Search in sources :

Example 1 with ProfilePeriod

use of org.apache.metron.profiler.ProfilePeriod in project metron by apache.

the class HBaseProfilerClient method fetch.

/**
 * Fetch the values stored in a profile based on a set of timestamps.
 *
 * @param clazz      The type of values stored by the profile.
 * @param profile    The name of the profile.
 * @param entity     The name of the entity.
 * @param groups     The groups used to sort the profile data.
 * @param periods    The set of profile measurement periods
 * @param defaultValue The default value to specify.  If empty, the result will be sparse.
 * @return A list of values.
 */
@Override
public <T> List<T> fetch(Class<T> clazz, String profile, String entity, List<Object> groups, Iterable<ProfilePeriod> periods, Optional<T> defaultValue) {
    byte[] columnFamily = Bytes.toBytes(columnBuilder.getColumnFamily());
    byte[] columnQualifier = columnBuilder.getColumnQualifier("value");
    // find all the row keys that satisfy this fetch
    List<byte[]> keysToFetch = rowKeyBuilder.rowKeys(profile, entity, groups, periods);
    // create a Get for each of the row keys
    List<Get> gets = keysToFetch.stream().map(k -> new Get(k).addColumn(columnFamily, columnQualifier)).collect(Collectors.toList());
    // get the 'gets'
    return get(gets, columnQualifier, columnFamily, clazz, defaultValue);
}
Also used : Arrays(java.util.Arrays) Result(org.apache.hadoop.hbase.client.Result) SerDeUtils(org.apache.metron.common.utils.SerDeUtils) Get(org.apache.hadoop.hbase.client.Get) ColumnBuilder(org.apache.metron.profiler.hbase.ColumnBuilder) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) Optional(java.util.Optional) RowKeyBuilder(org.apache.metron.profiler.hbase.RowKeyBuilder) Bytes(org.apache.hadoop.hbase.util.Bytes) Get(org.apache.hadoop.hbase.client.Get)

Example 2 with ProfilePeriod

use of org.apache.metron.profiler.ProfilePeriod in project metron by apache.

the class VerboseProfile method apply.

@Override
public Object apply(List<Object> args, Context context) throws ParseException {
    // required arguments
    String profile = getArg(0, String.class, args);
    String entity = getArg(1, String.class, args);
    List<ProfilePeriod> periods = getArg(2, List.class, args);
    // optional 'groups' argument
    List<Object> groups = new ArrayList<>();
    if (args.size() >= 4) {
        groups = getArg(3, List.class, args);
    }
    // get globals from the context
    Map<String, Object> globals = (Map<String, Object>) context.getCapability(GLOBAL_CONFIG).orElse(Collections.emptyMap());
    // lazily create the profiler client, if needed
    if (client == null) {
        RowKeyBuilder rowKeyBuilder = getRowKeyBuilder(globals);
        ColumnBuilder columnBuilder = getColumnBuilder(globals);
        TableProvider provider = getTableProvider(globals);
        long periodDuration = getPeriodDurationInMillis(globals);
        client = new HBaseProfilerClient(provider, rowKeyBuilder, columnBuilder, periodDuration, getTableName(globals), HBaseConfiguration.create());
    }
    // is there a default value?
    Optional<Object> defaultValue = Optional.empty();
    if (globals != null) {
        defaultValue = Optional.ofNullable(PROFILER_DEFAULT_VALUE.get(globals));
    }
    List<ProfileMeasurement> measurements = client.fetch(Object.class, profile, entity, groups, periods, defaultValue);
    // render a view of each profile measurement
    List<Object> results = new ArrayList<>();
    for (ProfileMeasurement measurement : measurements) {
        results.add(render(measurement));
    }
    return results;
}
Also used : ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) ArrayList(java.util.ArrayList) SaltyRowKeyBuilder(org.apache.metron.profiler.hbase.SaltyRowKeyBuilder) RowKeyBuilder(org.apache.metron.profiler.hbase.RowKeyBuilder) TableProvider(org.apache.metron.hbase.TableProvider) HTableProvider(org.apache.metron.hbase.HTableProvider) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) HBaseProfilerClient(org.apache.metron.profiler.client.HBaseProfilerClient) ArrayList(java.util.ArrayList) List(java.util.List) ColumnBuilder(org.apache.metron.profiler.hbase.ColumnBuilder) ValueOnlyColumnBuilder(org.apache.metron.profiler.hbase.ValueOnlyColumnBuilder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ProfilePeriod

use of org.apache.metron.profiler.ProfilePeriod 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);
    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.jupiter.api.Test)

Example 4 with ProfilePeriod

use of org.apache.metron.profiler.ProfilePeriod 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) {
        assertTrue(assertion.test(state), assertion.name());
    }
    return state;
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) Range(org.apache.commons.lang3.Range) Assertions(org.junit.jupiter.api.Assertions) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 5 with ProfilePeriod

use of org.apache.metron.profiler.ProfilePeriod in project metron by apache.

the class SaltyRowKeyBuilderTest method testRowKeys.

/**
 * `rowKeys` should return all of the row keys needed to retrieve the profile values over a given time horizon.
 */
@Test
public void testRowKeys() {
    int hoursAgo = 1;
    // setup
    List<Object> groups = Collections.emptyList();
    rowKeyBuilder = new SaltyRowKeyBuilder(saltDivisor, periodDuration, periodUnits);
    // a dummy profile measurement
    long now = System.currentTimeMillis();
    long oldest = now - TimeUnit.HOURS.toMillis(hoursAgo);
    ProfileMeasurement m = new ProfileMeasurement().withProfileName("profile").withEntity("entity").withPeriod(oldest, periodDuration, periodUnits).withProfileValue(22);
    // generate a list of expected keys
    List<byte[]> expectedKeys = new ArrayList<>();
    for (int i = 0; i < (hoursAgo * 4) + 1; i++) {
        // generate the expected key
        byte[] rk = rowKeyBuilder.rowKey(m);
        expectedKeys.add(rk);
        // advance to the next period
        ProfilePeriod next = m.getPeriod().next();
        m = new ProfileMeasurement().withProfileName("profile").withEntity("entity").withPeriod(next.getStartTimeMillis(), periodDuration, periodUnits);
    }
    // execute
    List<byte[]> actualKeys = rowKeyBuilder.rowKeys(measurement.getProfileName(), measurement.getEntity(), groups, oldest, now);
    // validate - expectedKeys == actualKeys
    for (int i = 0; i < actualKeys.size(); i++) {
        byte[] actual = actualKeys.get(i);
        byte[] expected = expectedKeys.get(i);
        assertThat(actual, equalTo(expected));
    }
}
Also used : ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) ArrayList(java.util.ArrayList) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) Test(org.junit.jupiter.api.Test)

Aggregations

ProfilePeriod (org.apache.metron.profiler.ProfilePeriod)8 ArrayList (java.util.ArrayList)4 ProfileMeasurement (org.apache.metron.profiler.ProfileMeasurement)4 List (java.util.List)3 TimeUnit (java.util.concurrent.TimeUnit)3 Test (org.junit.jupiter.api.Test)3 Map (java.util.Map)2 Optional (java.util.Optional)2 ColumnBuilder (org.apache.metron.profiler.hbase.ColumnBuilder)2 RowKeyBuilder (org.apache.metron.profiler.hbase.RowKeyBuilder)2 StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)2 DefaultVariableResolver (org.apache.metron.stellar.dsl.DefaultVariableResolver)2 Cache (com.github.benmanes.caffeine.cache.Cache)1 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 Collectors (java.util.stream.Collectors)1 Range (org.apache.commons.lang3.Range)1