默认情况下,ElasticSearch使用两个端口来监听外部TCP流量。
RestHighLevelClient是ES的Java客户端,它是通过HTTP与ES集群进行通信。
<!--引入es-high-level-client相关依赖 start-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.10.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.0</version>
</dependency>
<!--引入es-high-level-client相关依赖 end-->
# es配置
# es用户名
elasticsearch.userName=elastic
# es密码
elasticsearch.password=elastic
# es host ip 地址(集群),多个以","间隔
elasticsearch.hosts=127.0.0.1:9200
# es 请求方式
elasticsearch.scheme=http
# es 连接超时时间(ms)
elasticsearch.connectTimeOut=1000
# es socket 连接超时时间(ms)
elasticsearch.socketTimeOut=30000
# es 请求超时时间(ms)
elasticsearch.connectionRequestTimeOut=500
# es 最大连接数
elasticsearch.maxConnectNum=100
# es 每个路由的最大连接数
elasticsearch.maxConnectNumPerRoute=100
/**
* restHighLevelClient 客户端配置类
*
*/
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchConfig {
/**
* es host ip 地址(集群)
*/
private String hosts;
/**
* es用户名
*/
private String userName;
/**
* es密码
*/
private String password;
/**
* es 请求方式
*/
private String scheme;
/**
* es 连接超时时间
*/
private int connectTimeOut;
/**
* es socket 连接超时时间
*/
private int socketTimeOut;
/**
* es 请求超时时间
*/
private int connectionRequestTimeOut;
/**
* es 最大连接数
*/
private int maxConnectNum;
/**
* es 每个路由的最大连接数
*/
private int maxConnectNumPerRoute;
/**
* 如果@Bean没有指定bean的名称,那么方法名就是bean的名称
*/
@Bean(name = "restHighLevelClient")
public RestHighLevelClient restHighLevelClient() {
// 构建连接对象
RestClientBuilder builder = RestClient.builder(getEsHost());
// 连接延时配置
builder.setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
});
// 连接数配置
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectNumPerRoute);
httpClientBuilder.setDefaultCredentialsProvider(getCredentialsProvider());
return httpClientBuilder;
});
return new RestHighLevelClient(builder);
}
private HttpHost[] getEsHost() {
// 拆分地址(es为多节点时,不同host以逗号间隔)
List<HttpHost> hostLists = new ArrayList<>();
String[] hostList = hosts.split(",");
for (String addr : hostList) {
String host = addr.split(":")[0];
String port = addr.split(":")[1];
hostLists.add(new HttpHost(host, Integer.parseInt(port), scheme));
}
// 转换成 HttpHost 数组
return hostLists.toArray(new HttpHost[]{});
}
private CredentialsProvider getCredentialsProvider() {
// 设置用户名、密码
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
return credentialsProvider;
}
}
创建es索引的工具类如下所示。
/**
* 操作ES索引
*
*/
@Slf4j
@Service
public class EsIndexOperation {
@Resource
private RestHighLevelClient restHighLevelClient;
private final RequestOptions options = RequestOptions.DEFAULT;
/**
* 判断索引是否存在
*/
public boolean checkIndex(String index) {
try {
return restHighLevelClient.indices().exists(new GetIndexRequest(index), options);
} catch (Exception e) {
log.error("EsIndexOperation checkIndex error.", e);
}
return Boolean.FALSE;
}
/**
* 创建索引
*
* @param indexName es索引名
* @param esSettingFilePath es索引的alias、settings和mapping的配置文件
*/
public boolean createIndex(String indexName, String esSettingFilePath) {
String aliases = null;
String mappings = null;
String settings = null;
if (StringUtils.isNotBlank(esSettingFilePath)) {
try {
String fileContent = FileUtils.readFileContent(esSettingFilePath);
if (StringUtils.isNotBlank(fileContent)) {
JSONObject jsonObject = JSON.parseObject(fileContent);
aliases = jsonObject.getString("aliases");
mappings = jsonObject.getString("mappings");
settings = jsonObject.getString("settings");
}
} catch (Exception e) {
log.error("createIndex error.", e);
return false;
}
}
if (checkIndex(indexName)) {
log.error("createIndex indexName:[{}]已存在", indexName);
return false;
}
CreateIndexRequest request = new CreateIndexRequest(indexName);
if ((StringUtils.isNotBlank(aliases))) {
request.aliases(aliases, XContentType.JSON);
}
if (StringUtils.isNotBlank(mappings)) {
request.mapping(mappings, XContentType.JSON);
}
if (StringUtils.isNotBlank(settings)) {
request.settings(settings, XContentType.JSON);
}
try {
this.restHighLevelClient.indices().create(request, options);
return true;
} catch (IOException e) {
log.error("EsIndexOperation createIndex error.", e);
return false;
}
}
/**
* 删除索引
*/
public boolean deleteIndex(String indexName) {
try {
if (checkIndex(indexName)) {
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, options);
return response.isAcknowledged();
}
} catch (Exception e) {
log.error("EsIndexOperation deleteIndex error.", e);
}
return Boolean.FALSE;
}
}
/**
* 文件操作类
*/
@Slf4j
public class FileUtils {
/**
* 读取项目resources文件夹下的文件
*
* @param filePath 文件路径
* @return 文件内容
*/
public static String readFileContent(String filePath) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
reader.close();
return stringBuilder.toString();
} catch (IOException e) {
log.error("readFileContent error.", e);
}
return null;
}
public static void main(String[] args) {
String filePath = "src/main/resources/es/mappings_test20231216.txt";
String fileContent = readFileContent(filePath);
}
}
(1)在“resources”文件夹下创建es索引的配置文件
配置文件内容如下所示。
{
"aliases": {
"test": {}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
}
}
}
(2)读取es索引的配置文件,创建es索引
@Test
public void createIndex() {
String indexName = "test_1216";
String filePath = "src/main/resources/es/mappings_test20231216.txt";
boolean b = esIndexOperation.createIndex(indexName, filePath);
Assert.assertTrue(b);
}
(3)查看创建结果
通过命令?GET /test 查看es索引创建结果,结果如下所示。
{
"test_1216" : {
"aliases" : {
"test" : { }
},
"mappings" : {
"properties" : {
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "test_1216",
"creation_date" : "1702723364945",
"number_of_replicas" : "1",
"uuid" : "RCAhqjPZSG-n4fse3cot4A",
"version" : {
"created" : "7100099"
}
}
}
}
}
(1)elasticsearch学习(七):es客户端RestHighLevelClient_炎升的博客