Java往ES插入数据
1. 流程概述
为了向ES(Elasticsearch)插入数据,我们需要按照以下步骤进行操作:
- 连接ES服务器
- 创建索引
- 定义映射
- 插入数据
下面我们将逐一介绍每个步骤需要做什么,以及相应的Java代码。
2. 连接ES服务器
在Java中连接ES服务器需要使用Elasticsearch的Java客户端。可以使用Maven或Gradle将以下依赖项添加到项目中:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.15.0</version>
</dependency>
连接ES服务器的代码如下:
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
public class ESConnection {
private RestHighLevelClient client;
public ESConnection() {
client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
}
// 其他代码...
}
在上面的代码中,我们使用默认的本地主机和端口来连接ES服务器。你可以根据具体情况进行修改。
3. 创建索引
在插入数据之前,我们需要先创建一个索引。索引是ES用于存储和索引数据的地方。
创建索引的代码如下:
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
public class ESIndex {
private ESConnection connection;
public ESIndex(ESConnection connection) {
this.connection = connection;
}
public boolean createIndex(String indexName) {
CreateIndexRequest request = new CreateIndexRequest(indexName);
CreateIndexResponse createIndexResponse = null;
try {
createIndexResponse = connection.getClient().indices().create(request);
} catch (IOException e) {
e.printStackTrace();
}
return createIndexResponse != null && createIndexResponse.isAcknowledged();
}
// 其他代码...
}
在上面的代码中,我们使用CreateIndexRequest
对象创建了一个索引请求,并将其发送到ES服务器。如果创建成功,返回的响应中会包含一个acknowledged
字段,其值为true
。
4. 定义映射
映射(Mapping)是用于定义文档类型和字段的结构的。在插入数据之前,我们需要先定义好映射。
定义映射的代码如下:
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.XContentType;
public class ESMapping {
private ESConnection connection;
public ESMapping(ESConnection connection) {
this.connection = connection;
}
public boolean createMapping(String indexName, String typeName, String mappingJson) {
PutMappingRequest request = new PutMappingRequest(indexName);
request.type(typeName);
request.source(mappingJson, XContentType.JSON);
AcknowledgedResponse putMappingResponse = null;
try {
putMappingResponse = connection.getClient().indices().putMapping(request);
} catch (IOException e) {
e.printStackTrace();
}
return putMappingResponse != null && putMappingResponse.isAcknowledged();
}
// 其他代码...
}
在上面的代码中,我们使用PutMappingRequest
对象创建了一个映射请求,并将其发送到ES服务器。映射的结构以JSON格式传递,并使用XContentType.JSON
指定。
5. 插入数据
在创建好索引和定义好映射之后,我们可以开始向ES插入数据了。
插入数据的代码如下:
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
public class ESDataInsertion {
private ESConnection connection;
public ESDataInsertion(ESConnection connection) {
this.connection = connection;
}
public String insertData(String indexName, String typeName, String documentJson) {
IndexRequest request = new IndexRequest(indexName, typeName);
request.source(documentJson, XContentType.JSON);
IndexResponse indexResponse = null;
try {
indexResponse = connection.getClient().index(request);
} catch (IOException e) {
e.printStackTrace();
}
return indexResponse != null ? indexResponse.getId() : null;
}
// 其他代码...
}
在上面