elasticsearch, kibana, 6.8.18 版本下的创建索引,指定timestamp,java CRUD,maven版本等
在Elasticsearch 6.8.18版本下,使用Java客户端创建索引并指定时间戳字段,可以通过以下步骤实现:
- 在Maven项目的
pom.xml
中添加Elasticsearch依赖:
<dependencies>
<!-- Elasticsearch客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.8.18</version>
</dependency>
<!-- Elasticsearch核心 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.18</version>
</dependency>
</dependencies>
- 创建索引并指定时间戳字段的Java代码:
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import java.io.IOException;
public class ElasticsearchExample {
public static void main(String[] args) throws IOException {
// 初始化Elasticsearch客户端
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
// 创建CreateIndexRequest
CreateIndexRequest request = new CreateIndexRequest("my_index");
// 设置索引的mappings,包括时间戳字段
String jsonMappings = "{\n" +
" \"mappings\": {\n" +
" \"_doc\": {\n" +
" \"_source\": {\n" +
" \"enabled\": false\n" +
" },\n" +
" \"properties\": {\n" +
" \"@timestamp\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"strict_date_optional_time||epoch_millis\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
request.mapping("_doc", jsonMappings, XContentType.JSON);
// 创建索引
try {
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("I
评论已关闭