private Map<String, ZonedDateTime> findTimestamps(ElasticsearchMessage message) { final TypeConstraint<ZonedDateTime> timestampConstraint = TypeConstraint.ofClass(ZonedDateTime.class); return message .toMap() .entrySet() .stream() .filter(entry -> timestampConstraint.canCast(entry.getValue())) .collect( Collectors.<Map.Entry<String, Object>, String, ZonedDateTime>toMap( entry -> entry.getKey(), entry -> timestampConstraint.cast(entry.getValue()))); }
/** * Adds an index request to store a single message to a single index. * * @param message The message to index. * @param index The index of the message. * @param metrics The metrics to record the storage in. */ private void storeMessageToIndex(ElasticsearchMessage message, ElasticsearchIndex index) { /* * Implementation note: Elasticsearch is not happy about the ZonedDateTime toString format, so we need to format * the timestamp using a time zone offset instead. */ Map<String, Object> sourceMap = message.toMap(); Map<String, String> formattedTimestamps = formatTimestamps(findTimestamps(message)); if (!formattedTimestamps.containsKey(ElasticsearchMessage.DEFAULT_TIMESTAMP_FIELD)) { formattedTimestamps.put( ElasticsearchMessage.DEFAULT_TIMESTAMP_FIELD, formatTimestamp(ZonedDateTime.now())); } sourceMap.putAll(formattedTimestamps); IndexRequest request = new IndexRequest(index.toString(), documentType).source(sourceMap); processor.add(request); if (LOGGER.isTraceEnabled()) { LOGGER.trace(String.format("Added message to %s with data: %s", index, sourceMap)); } }