Search in sources :

Example 6 with EnrichmentValue

use of org.apache.metron.enrichment.converter.EnrichmentValue in project metron by apache.

the class ExtractorTest method testExtractionLoading.

@Test
public void testExtractionLoading() throws Exception {
    /**
     *         config:
     *         {
     *            "config" : {}
     *            ,"extractor" : "org.apache.metron.dataloads.extractor.ExtractorTest$DummyExtractor"
     *         }
     */
    String config = "{\n" + "            \"config\" : {}\n" + "            ,\"extractor\" : \"org.apache.metron.dataloads.extractor.ExtractorTest$DummyExtractor\"\n" + "         }";
    ExtractorHandler handler = ExtractorHandler.load(config);
    LookupKV results = Iterables.getFirst(handler.getExtractor().extract(null), null);
    EnrichmentKey key = (EnrichmentKey) results.getKey();
    EnrichmentValue value = (EnrichmentValue) results.getValue();
    Assert.assertEquals("dummy", key.indicator);
    Assert.assertEquals("type", key.type);
    Assert.assertEquals("dummy", value.getMetadata().get("indicator"));
}
Also used : LookupKV(org.apache.metron.enrichment.lookup.LookupKV) EnrichmentKey(org.apache.metron.enrichment.converter.EnrichmentKey) EnrichmentValue(org.apache.metron.enrichment.converter.EnrichmentValue) Test(org.junit.Test)

Example 7 with EnrichmentValue

use of org.apache.metron.enrichment.converter.EnrichmentValue in project metron by apache.

the class TransformFilterExtractorDecoratorTest method transforms_values_and_indicators.

@Test
public void transforms_values_and_indicators() throws IOException {
    final String indicatorVal = "val2";
    EnrichmentKey lookupKey = new EnrichmentKey("testenrichment", indicatorVal);
    EnrichmentValue lookupValue = new EnrichmentValue(new HashMap<String, Object>() {

        {
            put("foo", "val1");
            put("bar", indicatorVal);
            put("baz", "val3");
        }
    });
    LookupKV lkv = new LookupKV<>(lookupKey, lookupValue);
    List<LookupKV> extractedLkvs = new ArrayList<>();
    extractedLkvs.add(lkv);
    Mockito.when(extractor.extract("val1,val2,val3")).thenReturn(extractedLkvs);
    Iterable<LookupKV> extracted = decorator.extract("val1,val2,val3");
    EnrichmentKey expectedLookupKey = new EnrichmentKey("testenrichment", "VAL2");
    EnrichmentValue expectedLookupValue = new EnrichmentValue(new HashMap<String, Object>() {

        {
            put("foo", "VAL1");
            put("bar", "val2");
            put("baz", "val3");
            put("newvar", "VAL1");
            put("lowernewvar", "val1");
        }
    });
    LookupKV expectedLkv = new LookupKV<>(expectedLookupKey, expectedLookupValue);
    List<LookupKV> expectedLkvs = new ArrayList<>();
    expectedLkvs.add(expectedLkv);
    Assert.assertThat(extracted, CoreMatchers.equalTo(expectedLkvs));
}
Also used : LookupKV(org.apache.metron.enrichment.lookup.LookupKV) EnrichmentKey(org.apache.metron.enrichment.converter.EnrichmentKey) EnrichmentValue(org.apache.metron.enrichment.converter.EnrichmentValue) Test(org.junit.Test)

Example 8 with EnrichmentValue

use of org.apache.metron.enrichment.converter.EnrichmentValue in project metron by apache.

the class TransformFilterExtractorDecoratorTest method filters_values.

@Test
public void filters_values() throws Exception {
    final String indicatorVal = "val2";
    EnrichmentKey lookupKey = new EnrichmentKey("testenrichment", indicatorVal);
    EnrichmentValue lookupValue = new EnrichmentValue(new HashMap<String, Object>() {

        {
            put("foo", "val1");
            put("bar", indicatorVal);
            put("baz", "");
        }
    });
    LookupKV lkv = new LookupKV<>(lookupKey, lookupValue);
    List<LookupKV> extractedLkvs = new ArrayList<>();
    extractedLkvs.add(lkv);
    Mockito.when(extractor.extract("val1,val2,")).thenReturn(extractedLkvs);
    Iterable<LookupKV> extracted = decorator.extract("val1,val2,");
    Assert.assertThat(extracted, CoreMatchers.equalTo(new ArrayList<>()));
}
Also used : LookupKV(org.apache.metron.enrichment.lookup.LookupKV) EnrichmentKey(org.apache.metron.enrichment.converter.EnrichmentKey) EnrichmentValue(org.apache.metron.enrichment.converter.EnrichmentValue) Test(org.junit.Test)

Example 9 with EnrichmentValue

use of org.apache.metron.enrichment.converter.EnrichmentValue in project metron by apache.

the class SimpleHBaseAdapter method enrich.

@Override
public JSONObject enrich(CacheKey value) {
    JSONObject enriched = new JSONObject();
    if (!isInitialized()) {
        initializeAdapter(null);
    }
    List<String> enrichmentTypes = value.getConfig().getEnrichment().getFieldToTypeMap().get(EnrichmentUtils.toTopLevelField(value.getField()));
    if (isInitialized() && enrichmentTypes != null && value.getValue() != null) {
        try {
            for (LookupKV<EnrichmentKey, EnrichmentValue> kv : lookup.get(Iterables.transform(enrichmentTypes, new EnrichmentUtils.TypeToKey(value.coerceValue(String.class), lookup.getTable(), value.getConfig().getEnrichment())), false)) {
                if (kv != null && kv.getValue() != null && kv.getValue().getMetadata() != null) {
                    for (Map.Entry<String, Object> values : kv.getValue().getMetadata().entrySet()) {
                        enriched.put(kv.getKey().type + "." + values.getKey(), values.getValue());
                    }
                    LOG.trace("Enriched type {} => {}", kv.getKey().type, enriched);
                }
            }
        } catch (IOException e) {
            LOG.error("Unable to retrieve value: {}", e.getMessage(), e);
            initializeAdapter(null);
            throw new RuntimeException("Unable to retrieve value: " + e.getMessage(), e);
        }
    }
    LOG.trace("SimpleHBaseAdapter succeeded: {}", enriched);
    return enriched;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) IOException(java.io.IOException) Map(java.util.Map) EnrichmentKey(org.apache.metron.enrichment.converter.EnrichmentKey) EnrichmentValue(org.apache.metron.enrichment.converter.EnrichmentValue)

Example 10 with EnrichmentValue

use of org.apache.metron.enrichment.converter.EnrichmentValue in project metron by apache.

the class SimpleHbaseEnrichmentWriter method write.

@Override
public BulkWriterResponse write(String sensorType, WriterConfiguration configurations, Iterable<Tuple> tuples, List<JSONObject> messages) throws Exception {
    Map<String, Object> sensorConfig = configurations.getSensorConfig(sensorType);
    HTableInterface table = getTable(sensorConfig);
    KeyTransformer transformer = getTransformer(sensorConfig);
    Object enrichmentTypeObj = Configurations.ENRICHMENT_TYPE.get(sensorConfig);
    String enrichmentType = enrichmentTypeObj == null ? null : enrichmentTypeObj.toString();
    Set<String> valueColumns = new HashSet<>(getColumns(Configurations.VALUE_COLUMNS.get(sensorConfig), true));
    List<Put> puts = new ArrayList<>();
    for (JSONObject message : messages) {
        EnrichmentKey key = getKey(message, transformer, enrichmentType);
        EnrichmentValue value = getValue(message, transformer.keySet, valueColumns);
        if (key == null || value == null) {
            continue;
        }
        Put put = converter.toPut(this.cf, key, value);
        if (put != null) {
            LOG.debug("Put: {Column Family: '{}', Key: '{}', Value: '{}'}", this.cf, key, value);
            puts.add(put);
        }
    }
    BulkWriterResponse response = new BulkWriterResponse();
    try {
        table.put(puts);
    } catch (Exception e) {
        response.addAllErrors(e, tuples);
        return response;
    }
    // Can return no errors, because put will throw Exception on error.
    response.addAllSuccesses(tuples);
    return response;
}
Also used : HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) Put(org.apache.hadoop.hbase.client.Put) EnrichmentKey(org.apache.metron.enrichment.converter.EnrichmentKey) IOException(java.io.IOException) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) EnrichmentValue(org.apache.metron.enrichment.converter.EnrichmentValue) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse)

Aggregations

EnrichmentKey (org.apache.metron.enrichment.converter.EnrichmentKey)22 EnrichmentValue (org.apache.metron.enrichment.converter.EnrichmentValue)22 LookupKV (org.apache.metron.enrichment.lookup.LookupKV)14 Test (org.junit.Test)10 EnrichmentConverter (org.apache.metron.enrichment.converter.EnrichmentConverter)6 HashMap (java.util.HashMap)5 MockHTable (org.apache.metron.hbase.mock.MockHTable)5 IOException (java.io.IOException)4 Put (org.apache.hadoop.hbase.client.Put)4 ArrayList (java.util.ArrayList)3 EnrichmentLookup (org.apache.metron.enrichment.lookup.EnrichmentLookup)3 BloomAccessTracker (org.apache.metron.enrichment.lookup.accesstracker.BloomAccessTracker)3 PersistentAccessTracker (org.apache.metron.enrichment.lookup.accesstracker.PersistentAccessTracker)3 Before (org.junit.Before)3 StringObjectPropertyType (org.mitre.cybox.common_2.StringObjectPropertyType)3 Map (java.util.Map)2 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)2 Result (org.apache.hadoop.hbase.client.Result)2 ConfigUploadComponent (org.apache.metron.enrichment.integration.components.ConfigUploadComponent)2 KafkaComponent (org.apache.metron.integration.components.KafkaComponent)2