{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 10000, "relation": "gte" }, "max_score": 1, "hits": [{ "_index": "tp_portflux_ip网管_2023_05_10", "_type": "_doc", "_id": "vft6AYgBqWgyteKAl0pq", "_score": 1, "_source": { "kpi1": 0, "kpi2": 0, "kpi3": 0, "kpi4": 0, "kpi5": -3, "kpi6": -3, "kpi7": 20971520000, "kpi8": 0, "kpi9": 0, "kpi10": 0, "kpi11": 0, "kpi12": 0, "kpi13": 0, "mo_id": "307434295fd494ab3b303da202c94517", "object_id": "Eth-Trunk4.3004", "gather_time": 1683650700000 } }] } } springboot代码插入这条数据
时间: 2023-06-19 16:09:13 浏览: 128
C++11 并发指南之std::mutex详解
可以使用 Elasticsearch 客户端来插入这条数据。以下是一个示例 Spring Boot 控制器方法,可以使用它来插入数据:
```java
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.Map;
@RestController
public class ElasticsearchController {
@Autowired
private RestHighLevelClient client;
@PostMapping("/insert_data")
public String insertData(@RequestBody Map<String, Object> data) throws IOException {
IndexRequest request = new IndexRequest("tp_portflux_ip网管_2023_05_10");
request.index("tp_portflux_ip网管_2023_05_10");
request.type("_doc");
request.source(data, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
return response.getId();
}
}
```
注意,这里使用了 `RestHighLevelClient`,需要在 Spring Boot 中进行配置。`data` 参数是一个包含要插入的字段和值的 Map 对象,例如:
```json
{
"kpi1": 0,
"kpi2": 0,
"kpi3": 0,
"kpi4": 0,
"kpi5": -3,
"kpi6": -3,
"kpi7": 20971520000,
"kpi8": 0,
"kpi9": 0,
"kpi10": 0,
"kpi11": 0,
"kpi12": 0,
"kpi13": 0,
"mo_id": "307434295fd494ab3b303da202c94517",
"object_id": "Eth-Trunk4.3004",
"gather_time": 1683650700000
}
```
阅读全文