ethrbunny
ethrbunny

Reputation: 10469

Writing to elasticsearch via high level client - setting timestamp on documents

Using the high level client I'm able to send KV pairs (as a document) to elastic. I can query these using curl so I know they're in. What they lack is some equivalent to the "@timestamp" value that logstash seems to add. I haven't found what the syntax for this field is (or what the appropriate field would be).

Epoch? TZ?

Upvotes: 1

Views: 1487

Answers (1)

ethrbunny
ethrbunny

Reputation: 10469

FWIW - if you want these values to be available in kibana just use SimpleDateFormat like so:

    Date date = new Date(System.currentTimeMillis());

    // Conversion
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    String stamp = sdf.format(date);

Then add this to your HashMap as '@timestamp':

    // write to elastic
    RestHighLevelClient client = getClient();
    Map<String, Object> mapObject = new HashMap();
    mapObject.put("type", "consumer_test");
    mapObject.put("test.group", group);
    mapObject.put("test.topic", topic);
    mapObject.put("test.sum", sumLag);
    mapObject.put("@timestamp", stamp);

    IndexRequest indexRequest = new IndexRequest(index).source(mapObject);

    try {
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        client.close();
    } catch(Exception e) {
        LOGGER.error("exception from indexing request", e);
        return false;
    }

Upvotes: 3

Related Questions