Search in sources :

Example 21 with Document

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

the class ElasticsearchDao method getAllLatest.

@Override
public Iterable<Document> getAllLatest(final List<GetRequest> getRequests) throws IOException {
    Collection<String> guids = new HashSet<>();
    Collection<String> sensorTypes = new HashSet<>();
    for (GetRequest getRequest : getRequests) {
        guids.add(getRequest.getGuid());
        sensorTypes.add(getRequest.getSensorType());
    }
    List<Document> documents = searchByGuids(guids, sensorTypes, hit -> {
        Long ts = 0L;
        String doc = hit.getSourceAsString();
        String sourceType = Iterables.getFirst(Splitter.on("_doc").split(hit.getType()), null);
        try {
            return Optional.of(new Document(doc, hit.getId(), sourceType, ts));
        } catch (IOException e) {
            throw new IllegalStateException("Unable to retrieve latest: " + e.getMessage(), e);
        }
    });
    return documents;
}
Also used : GetRequest(org.apache.metron.indexing.dao.search.GetRequest) IOException(java.io.IOException) Document(org.apache.metron.indexing.dao.update.Document) HashSet(java.util.HashSet)

Example 22 with Document

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

the class ElasticsearchMetaAlertDao method buildCreateDocument.

/**
 * Build the Document representing a meta alert to be created.
 * @param alerts The Elasticsearch results for the meta alerts child documents
 * @param groups The groups used to create this meta alert
 * @return A Document representing the new meta alert
 */
protected Document buildCreateDocument(Iterable<Document> alerts, List<String> groups) {
    // Need to create a Document from the multiget. Scores will be calculated later
    Map<String, Object> metaSource = new HashMap<>();
    List<Map<String, Object>> alertList = new ArrayList<>();
    for (Document alert : alerts) {
        alertList.add(alert.getDocument());
    }
    metaSource.put(ALERT_FIELD, alertList);
    // Add any meta fields
    String guid = UUID.randomUUID().toString();
    metaSource.put(GUID, guid);
    metaSource.put(Constants.Fields.TIMESTAMP.getName(), System.currentTimeMillis());
    metaSource.put(GROUPS_FIELD, groups);
    metaSource.put(STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString());
    return new Document(metaSource, guid, METAALERT_TYPE, System.currentTimeMillis());
}
Also used : Document(org.apache.metron.indexing.dao.update.Document)

Example 23 with Document

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

the class ElasticsearchMetaAlertDao method patch.

/**
 * Does not allow patches on the "alerts" or "status" fields.  These fields must be updated with their
 * dedicated methods.
 *
 * @param request The patch request
 * @param timestamp Optionally a timestamp to set. If not specified then current time is used.
 * @throws OriginalNotFoundException
 * @throws IOException
 */
@Override
public void patch(PatchRequest request, Optional<Long> timestamp) throws OriginalNotFoundException, IOException {
    if (isPatchAllowed(request)) {
        Document d = getPatchedDocument(request, timestamp);
        indexDao.update(d, Optional.ofNullable(request.getIndex()));
    } else {
        throw new IllegalArgumentException("Meta alert patches are not allowed for /alert or /status paths.  " + "Please use the add/remove alert or update status functions instead.");
    }
}
Also used : Document(org.apache.metron.indexing.dao.update.Document)

Example 24 with Document

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

the class ElasticsearchMetaAlertDao method createMetaAlert.

@Override
@SuppressWarnings("unchecked")
public MetaAlertCreateResponse createMetaAlert(MetaAlertCreateRequest request) throws InvalidCreateException, IOException {
    List<GetRequest> alertRequests = request.getAlerts();
    if (request.getAlerts().isEmpty()) {
        throw new InvalidCreateException("MetaAlertCreateRequest must contain alerts");
    }
    if (request.getGroups().isEmpty()) {
        throw new InvalidCreateException("MetaAlertCreateRequest must contain UI groups");
    }
    // Retrieve the documents going into the meta alert and build it
    Iterable<Document> alerts = indexDao.getAllLatest(alertRequests);
    Document metaAlert = buildCreateDocument(alerts, request.getGroups());
    calculateMetaScores(metaAlert);
    // Add source type to be consistent with other sources and allow filtering
    metaAlert.getDocument().put(SOURCE_TYPE, MetaAlertDao.METAALERT_TYPE);
    // Start a list of updates / inserts we need to run
    Map<Document, Optional<String>> updates = new HashMap<>();
    updates.put(metaAlert, Optional.of(MetaAlertDao.METAALERTS_INDEX));
    try {
        // We need to update the associated alerts with the new meta alerts, making sure existing
        // links are maintained.
        Map<String, Optional<String>> guidToIndices = alertRequests.stream().collect(Collectors.toMap(GetRequest::getGuid, GetRequest::getIndex));
        Map<String, String> guidToSensorTypes = alertRequests.stream().collect(Collectors.toMap(GetRequest::getGuid, GetRequest::getSensorType));
        for (Document alert : alerts) {
            if (addMetaAlertToAlert(metaAlert.getGuid(), alert)) {
                // Use the index in the request if it exists
                Optional<String> index = guidToIndices.get(alert.getGuid());
                if (!index.isPresent()) {
                    // Look up the index from Elasticsearch if one is not supplied in the request
                    index = elasticsearchDao.getIndexName(alert.getGuid(), guidToSensorTypes.get(alert.getGuid()));
                    if (!index.isPresent()) {
                        throw new IllegalArgumentException("Could not find index for " + alert.getGuid());
                    }
                }
                updates.put(alert, index);
            }
        }
        // Kick off any updates.
        indexDaoUpdate(updates);
        MetaAlertCreateResponse createResponse = new MetaAlertCreateResponse();
        createResponse.setCreated(true);
        createResponse.setGuid(metaAlert.getGuid());
        return createResponse;
    } catch (IOException ioe) {
        throw new InvalidCreateException("Unable to create meta alert", ioe);
    }
}
Also used : MetaAlertCreateResponse(org.apache.metron.indexing.dao.metaalert.MetaAlertCreateResponse) IOException(java.io.IOException) Document(org.apache.metron.indexing.dao.update.Document) GetRequest(org.apache.metron.indexing.dao.search.GetRequest) InvalidCreateException(org.apache.metron.indexing.dao.search.InvalidCreateException)

Example 25 with Document

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

the class ElasticsearchMetaAlertDaoTest method testInvalidInit.

@Test(expected = IllegalArgumentException.class)
public void testInvalidInit() {
    IndexDao dao = new IndexDao() {

        @Override
        public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException {
            return null;
        }

        @Override
        public GroupResponse group(GroupRequest groupRequest) throws InvalidSearchException {
            return null;
        }

        @Override
        public void init(AccessConfig config) {
        }

        @Override
        public Document getLatest(String guid, String sensorType) throws IOException {
            return null;
        }

        @Override
        public Iterable<Document> getAllLatest(List<GetRequest> getRequests) throws IOException {
            return null;
        }

        @Override
        public void update(Document update, Optional<String> index) throws IOException {
        }

        @Override
        public void batchUpdate(Map<Document, Optional<String>> updates) throws IOException {
        }

        @Override
        public Map<String, FieldType> getColumnMetadata(List<String> indices) throws IOException {
            return null;
        }
    };
    ElasticsearchMetaAlertDao metaAlertDao = new ElasticsearchMetaAlertDao();
    metaAlertDao.init(dao);
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) Optional(java.util.Optional) GroupRequest(org.apache.metron.indexing.dao.search.GroupRequest) ArrayList(java.util.ArrayList) List(java.util.List) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) Document(org.apache.metron.indexing.dao.update.Document) HashMap(java.util.HashMap) Map(java.util.Map) IndexDao(org.apache.metron.indexing.dao.IndexDao) FieldType(org.apache.metron.indexing.dao.search.FieldType) Test(org.junit.Test)

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