Java 同步JSON字符串至ES(Elasticsearch) 添加时间戳(@timestamp)、版本(@version) 字段

解决方法:仿照logstash同步原理,对于同步json字符串,首先将其解析,然后添加时间戳和版本字段,或其他字段,打入es。

 public void insertEs(String jsonStr){
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        jsonObject.put("@timestamp", new Date());//添加时间戳
        jsonObject.put("@version", "1");//添加版本
        IndexRequest indexRequest = new IndexRequest(ESIndexName,"doc").source(jsonObject);

        try {
            highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("入es异常,{}", e);
        }
    }

 

你可能感兴趣的:(elk)