All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lumbermill.elasticsearch.ElasticSearchBulkResponseEvent Maven / Gradle / Ivy

/*
 * Copyright 2016 Sony Mobile Communications, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package lumbermill.elasticsearch;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lumbermill.api.JsonEvent;
import lumbermill.internal.Json;
import lumbermill.internal.elasticsearch.ElasticSearchBulkResponse;
import rx.Observable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 

This result matches an Elasticsearch Bulk API response */ public class ElasticSearchBulkResponseEvent extends JsonEvent { private final Map eventAndResponse = new HashMap<>(); public static ElasticSearchBulkResponseEvent of(ElasticSearchBulkResponse bulkResponse) { return new ElasticSearchBulkResponseEvent(buildJsonResponse(bulkResponse.hasErrors(), bulkResponse.eventWithResponse), bulkResponse.eventWithResponse); } public static ElasticSearchBulkResponseEvent ofPostponed(List events) { return new ElasticSearchBulkResponseEvent(buildPostponedJsonResponse(events), new HashMap<>()); } public static ElasticSearchBulkResponseEvent ofPostponed(ElasticSearchBulkRequestEvent requestEvent) { return new ElasticSearchBulkResponseEvent(buildPostponedJsonResponse(requestEvent), new HashMap<>()); } private ElasticSearchBulkResponseEvent(ObjectNode node, Map eventAndResponse) { super(node); this.eventAndResponse.putAll(eventAndResponse); } public ElasticSearchBulkResponseEvent nextAttempt(ElasticSearchBulkResponse elasticSearchBulkResponse) { eventAndResponse.putAll(elasticSearchBulkResponse.eventWithResponse); return new ElasticSearchBulkResponseEvent(buildJsonResponse(elasticSearchBulkResponse.hasErrors(), eventAndResponse), eventAndResponse); } public Observable arguments() { return Observable.from(eventAndResponse.keySet()); } private static ObjectNode buildJsonResponse(boolean hasErrors, Map eventAndResponse) { ObjectNode node = Json.OBJECT_MAPPER.createObjectNode() .put("errors", hasErrors) .put("took", 1L); node.putArray("items") .addAll( eventAndResponse.values() .stream() .map(jsonEvent -> jsonEvent.copyNode()) .collect(Collectors.toList())); return node; } private static ObjectNode buildPostponedJsonResponse(ElasticSearchBulkRequestEvent requestEvent) { ObjectNode node = Json.OBJECT_MAPPER.createObjectNode() .put("errors", false) .put("took", 1L); node.putArray("items") .addAll( requestEvent.indexRequests().stream() .map(tuple -> tuple.getSecond()) .map(ElasticSearchBulkResponseEvent::toPostponsedEvent) .collect(Collectors.toList())); return node; } private static ObjectNode buildPostponedJsonResponse(List events) { ObjectNode node = Json.OBJECT_MAPPER.createObjectNode() .put("errors", false) .put("took", 1L); node.putArray("items") .addAll( events.stream() .map(ElasticSearchBulkResponseEvent::toPostponsedEvent) .collect(Collectors.toList())); System.out.println("Response built: " + new JsonEvent(node).toString(true)); return node; } private static JsonNode toPostponsedEvent(JsonEvent jsonEvent) { ObjectNode objectNode = Json.OBJECT_MAPPER.createObjectNode(); objectNode.putObject("create").put("_id", "ID_CREATION_POSTPONED").put("status", 202); return objectNode; } /* Below are used for test and debugging */ public List indexNames() { ArrayNode array = (ArrayNode) super.jsonNode.get("items"); List indices = new ArrayList<>(); array.forEach(created -> indices.add(dataNode(created).get("_index").asText())); return indices; } public List types() { ArrayNode array = (ArrayNode) super.jsonNode.get("items"); List indices = new ArrayList<>(); array.forEach(created -> indices.add(created.fieldNames().next())); return indices; } public List versions() { ArrayNode array = (ArrayNode) super.jsonNode.get("items"); List indices = new ArrayList<>(); array.forEach(created -> indices.add(dataNode(created).get("_version").asText())); return indices; } /** * Elasticsearch returns "create" if an entry was created and "index" if it was updated. */ private JsonNode dataNode(JsonNode node) { String action = node.has("index") ? "index" : "create"; return node.get(action); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy