Skip to content

Commit 72e3c11

Browse files
committed
feat: elasticsearch 6.x 示例
1 parent 14b5853 commit 72e3c11

File tree

23 files changed

+1894
-494
lines changed

23 files changed

+1894
-494
lines changed

codes/javadb/elasticsearch/elasticsearch6/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<parent>o
6+
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
99
<version>2.7.7</version>
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>cn.hutool</groupId>
4444
<artifactId>hutool-all</artifactId>
45-
<version>5.8.8</version>
45+
<version>5.8.25</version>
4646
</dependency>
4747

4848
<dependency>

codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/Demo.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/ElasticsearchFactory.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.elasticsearch.client.RestClientBuilder;
1010
import org.elasticsearch.client.RestHighLevelClient;
1111

12+
import java.util.Collection;
1213
import java.util.List;
1314
import java.util.stream.Collectors;
1415

@@ -38,11 +39,13 @@ public static RestClient newRestClient() {
3839
}
3940

4041
public static RestClient newRestClient(String env) {
41-
String hosts = getDefaultEsAddress(env);
42-
return newRestClient(toHttpHostList(hosts));
42+
String hostsConfig = getDefaultEsAddress(env);
43+
List<String> hosts = StrUtil.split(hostsConfig, ",");
44+
return newRestClient(hosts);
4345
}
4446

45-
public static RestClient newRestClient(HttpHost[] httpHosts) {
47+
public static RestClient newRestClient(Collection<String> hosts) {
48+
HttpHost[] httpHosts = toHttpHostList(hosts);
4649
RestClientBuilder builder = getRestClientBuilder(httpHosts);
4750
if (builder == null) {
4851
return null;
@@ -62,11 +65,13 @@ public static RestHighLevelClient newRestHighLevelClient() {
6265
}
6366

6467
public static RestHighLevelClient newRestHighLevelClient(String env) {
65-
String hosts = getDefaultEsAddress(env);
66-
return newRestHighLevelClient(toHttpHostList(hosts));
68+
String hostsConfig = getDefaultEsAddress(env);
69+
List<String> hosts = StrUtil.split(hostsConfig, ",");
70+
return newRestHighLevelClient(hosts);
6771
}
6872

69-
public static RestHighLevelClient newRestHighLevelClient(HttpHost[] httpHosts) {
73+
public static RestHighLevelClient newRestHighLevelClient(Collection<String> hosts) {
74+
HttpHost[] httpHosts = toHttpHostList(hosts);
7075
RestClientBuilder builder = getRestClientBuilder(httpHosts);
7176
if (builder == null) {
7277
return null;
@@ -86,12 +91,13 @@ public static ElasticsearchTemplate newElasticsearchTemplate() {
8691
}
8792

8893
public static ElasticsearchTemplate newElasticsearchTemplate(String env) {
89-
String hosts = getDefaultEsAddress(env);
90-
return newElasticsearchTemplate(toHttpHostList(hosts));
94+
String hostsConfig = getDefaultEsAddress(env);
95+
List<String> hosts = StrUtil.split(hostsConfig, ",");
96+
return newElasticsearchTemplate(hosts);
9197
}
9298

93-
public static ElasticsearchTemplate newElasticsearchTemplate(HttpHost[] httpHosts) {
94-
RestHighLevelClient client = newRestHighLevelClient(httpHosts);
99+
public static ElasticsearchTemplate newElasticsearchTemplate(Collection<String> hosts) {
100+
RestHighLevelClient client = newRestHighLevelClient(hosts);
95101
if (client == null) {
96102
return null;
97103
}
@@ -125,21 +131,22 @@ public static RestClientBuilder getRestClientBuilder(HttpHost[] httpHosts) {
125131
return restClientBuilder;
126132
}
127133

128-
private static HttpHost[] toHttpHostList(String hosts) {
129-
if (StrUtil.isBlank(hosts)) {
130-
return null;
134+
private static HttpHost[] toHttpHostList(Collection<String> hosts) {
135+
if (CollectionUtil.isEmpty(hosts)) {
136+
return new HttpHost[0];
131137
}
132-
List<String> strList = StrUtil.split(hosts, ",");
133-
List<HttpHost> list = strList.stream().map(str -> {
134-
List<String> params = StrUtil.split(str, ":");
135-
return new HttpHost(params.get(0), Integer.parseInt(params.get(1)), "http");
136-
}).collect(Collectors.toList());
138+
List<HttpHost> list = hosts.stream().map(ElasticsearchFactory::toHttpHost).collect(Collectors.toList());
137139
if (CollectionUtil.isEmpty(list)) {
138140
return new HttpHost[0];
139141
}
140142
return list.toArray(new HttpHost[0]);
141143
}
142144

145+
public static HttpHost toHttpHost(String host) {
146+
List<String> params = StrUtil.split(host, ":");
147+
return new HttpHost(params.get(0), Integer.parseInt(params.get(1)), "http");
148+
}
149+
143150
public static String getDefaultEsAddress() {
144151
// 从配置中心读取环境变量
145152
String env = "test";

0 commit comments

Comments
 (0)