Search in sources :

Example 16 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class HBaseDaoIntegrationTest method shouldGetLatest.

@Test
public void shouldGetLatest() throws Exception {
    // Load alerts
    List<Document> alerts = buildAlerts(3);
    Map<Document, Optional<String>> updates = alerts.stream().collect(Collectors.toMap(document -> document, document -> Optional.empty()));
    hbaseDao.batchUpdate(updates);
    Document actualDocument = hbaseDao.getLatest("message_1", SENSOR_TYPE);
    Document expectedDocument = alerts.get(1);
    Assert.assertEquals(expectedDocument, actualDocument);
}
Also used : Document(org.apache.metron.indexing.dao.update.Document) HBASE_CF(org.apache.metron.indexing.dao.HBaseDao.HBASE_CF) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) Iterator(java.util.Iterator) BeforeClass(org.junit.BeforeClass) IndexDao(org.apache.metron.indexing.dao.IndexDao) IOException(java.io.IOException) HashMap(java.util.HashMap) Test(org.junit.Test) Hex(org.apache.commons.codec.binary.Hex) GetRequest(org.apache.metron.indexing.dao.search.GetRequest) HBaseDao(org.apache.metron.indexing.dao.HBaseDao) Collectors(java.util.stream.Collectors) MockHBaseTableProvider(org.apache.metron.hbase.mock.MockHBaseTableProvider) ArrayList(java.util.ArrayList) List(java.util.List) HBASE_TABLE(org.apache.metron.indexing.dao.HBaseDao.HBASE_TABLE) Map(java.util.Map) After(org.junit.After) Optional(java.util.Optional) Assert(org.junit.Assert) Optional(java.util.Optional) Document(org.apache.metron.indexing.dao.update.Document) Test(org.junit.Test)

Example 17 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class HBaseDaoIntegrationTest method shouldGetAllLatest.

@Test
public void shouldGetAllLatest() throws Exception {
    // Load alerts
    List<Document> alerts = buildAlerts(15);
    alerts.stream().collect(Collectors.toMap(Document::getGuid, document -> Optional.empty()));
    Map<Document, Optional<String>> updates = alerts.stream().collect(Collectors.toMap(document -> document, document -> Optional.empty()));
    hbaseDao.batchUpdate(updates);
    int expectedCount = 12;
    List<GetRequest> getRequests = new ArrayList<>();
    for (int i = 1; i < expectedCount + 1; i++) {
        getRequests.add(new GetRequest("message_" + i, SENSOR_TYPE));
    }
    Iterator<Document> results = hbaseDao.getAllLatest(getRequests).iterator();
    for (int i = 0; i < expectedCount; i++) {
        Document expectedDocument = alerts.get(i + 1);
        Document actualDocument = results.next();
        Assert.assertEquals(expectedDocument, actualDocument);
    }
    Assert.assertFalse("Result size should be 12 but was greater", results.hasNext());
}
Also used : Document(org.apache.metron.indexing.dao.update.Document) HBASE_CF(org.apache.metron.indexing.dao.HBaseDao.HBASE_CF) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) Iterator(java.util.Iterator) BeforeClass(org.junit.BeforeClass) IndexDao(org.apache.metron.indexing.dao.IndexDao) IOException(java.io.IOException) HashMap(java.util.HashMap) Test(org.junit.Test) Hex(org.apache.commons.codec.binary.Hex) GetRequest(org.apache.metron.indexing.dao.search.GetRequest) HBaseDao(org.apache.metron.indexing.dao.HBaseDao) Collectors(java.util.stream.Collectors) MockHBaseTableProvider(org.apache.metron.hbase.mock.MockHBaseTableProvider) ArrayList(java.util.ArrayList) List(java.util.List) HBASE_TABLE(org.apache.metron.indexing.dao.HBaseDao.HBASE_TABLE) Map(java.util.Map) After(org.junit.After) Optional(java.util.Optional) Assert(org.junit.Assert) Optional(java.util.Optional) GetRequest(org.apache.metron.indexing.dao.search.GetRequest) ArrayList(java.util.ArrayList) Document(org.apache.metron.indexing.dao.update.Document) Test(org.junit.Test)

Example 18 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class HBaseDaoIntegrationTest method buildAlerts.

protected List<Document> buildAlerts(int count) throws IOException {
    List<Document> alerts = new ArrayList<>();
    for (int i = 0; i < count; ++i) {
        String guid = "message_" + i;
        String json = "{\"guid\":\"message_" + i + "\", \"source:type\":\"test\"}";
        Document alert = new Document(json, guid, SENSOR_TYPE, System.currentTimeMillis());
        alerts.add(alert);
    }
    return alerts;
}
Also used : ArrayList(java.util.ArrayList) Document(org.apache.metron.indexing.dao.update.Document)

Example 19 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class ElasticsearchDao method batchUpdate.

@Override
public void batchUpdate(Map<Document, Optional<String>> updates) throws IOException {
    String indexPostfix = ElasticsearchUtils.getIndexFormat(accessConfig.getGlobalConfigSupplier().get()).format(new Date());
    BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    // Get the indices we'll actually be using for each Document.
    for (Map.Entry<Document, Optional<String>> updateEntry : updates.entrySet()) {
        Document update = updateEntry.getKey();
        String sensorType = update.getSensorType();
        String indexName = getIndexName(update, updateEntry.getValue(), indexPostfix);
        IndexRequest indexRequest = buildIndexRequest(update, sensorType, indexName);
        bulkRequestBuilder.add(indexRequest);
    }
    BulkResponse bulkResponse = bulkRequestBuilder.get();
    if (bulkResponse.hasFailures()) {
        LOG.error("Bulk Request has failures: {}", bulkResponse.buildFailureMessage());
        throw new IOException("ElasticsearchDao upsert failed: " + bulkResponse.buildFailureMessage());
    }
}
Also used : Optional(java.util.Optional) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) IOException(java.io.IOException) Document(org.apache.metron.indexing.dao.update.Document) IndexRequest(org.elasticsearch.action.index.IndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) Date(java.util.Date)

Example 20 with Document

use of org.apache.metron.indexing.dao.update.Document in project metron by apache.

the class ElasticsearchDao method toDocument.

private Optional<Document> toDocument(final String guid, SearchHit hit) {
    Long ts = 0L;
    String doc = hit.getSourceAsString();
    String sourceType = toSourceType(hit.getType());
    try {
        return Optional.of(new Document(doc, guid, sourceType, ts));
    } catch (IOException e) {
        throw new IllegalStateException("Unable to retrieve latest: " + e.getMessage(), e);
    }
}
Also used : IOException(java.io.IOException) Document(org.apache.metron.indexing.dao.update.Document)

Aggregations

Document (org.apache.metron.indexing.dao.update.Document)31 ArrayList (java.util.ArrayList)13 GetRequest (org.apache.metron.indexing.dao.search.GetRequest)12 Map (java.util.Map)11 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 IOException (java.io.IOException)9 List (java.util.List)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)5 AccessConfig (org.apache.metron.indexing.dao.AccessConfig)5 IndexDao (org.apache.metron.indexing.dao.IndexDao)5 MetaAlertCreateResponse (org.apache.metron.indexing.dao.metaalert.MetaAlertCreateResponse)4 FieldType (org.apache.metron.indexing.dao.search.FieldType)4 GroupRequest (org.apache.metron.indexing.dao.search.GroupRequest)4 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)4 java.util (java.util)3 Constants (org.apache.metron.common.Constants)3 MockHBaseTableProvider (org.apache.metron.hbase.mock.MockHBaseTableProvider)3 MetaAlertCreateRequest (org.apache.metron.indexing.dao.metaalert.MetaAlertCreateRequest)3