@PostMapping("/insert_data23") public String insertData23(@RequestBody List<Map<String, Object>> dataList) throws IOException { BulkRequest bulkRequest = new BulkRequest(); for (Map<String, Object> data : dataList) { IndexRequest indexRequest = new IndexRequest("tp_portflux_ip网管_2023_05_12"); indexRequest.id(data.get("id").toString()); // 指定id字段 indexRequest.source( new XContentFactory.JsonBuilder() .startObject() .field("field1", data.get("field1")) // 指定field1字段 .field("field2", data.get("field2")) // 指定field2字段 .field("field3", data.get("field3")) // 指定field3字段 // ... .endObject() ); bulkRequest.add(indexRequest); } BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT); if (bulkResponse.hasFailures()) { // 处理失败情况 return "fail"; } else { return "success"; } } 导包及maven依赖
时间: 2024-02-13 19:02:03 浏览: 59
这段代码使用了Elasticsearch的Java High Level REST Client,需要导入以下依赖:
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.0</version>
</dependency>
```
同时需要导入以下包:
```java
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.common.xcontent.XContentFactory;
```
另外,需要注意的是,这段代码是使用POST方法来插入数据到Elasticsearch中,因此需要在Controller类上添加`@RestController`和`@RequestMapping`注解,同时在该方法上添加`@PostMapping`注解以映射HTTP POST请求。
阅读全文