Skip to content

Commit

Permalink
feat: 更新 elasticsearch 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
dunwu committed Feb 21, 2024
1 parent a1d04b3 commit 14b5853
Show file tree
Hide file tree
Showing 18 changed files with 1,012 additions and 261 deletions.
50 changes: 33 additions & 17 deletions codes/javadb/elasticsearch/elasticsearch6/pom.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>o
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
</parent>

<groupId>io.github.dunwu</groupId>
<artifactId>javadb-elasticsearch6</artifactId>
<version>1.0.0</version>
Expand All @@ -17,37 +23,47 @@
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.4.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.20</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
<version>5.8.8</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.17.1</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.4.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.3</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@

import io.github.dunwu.javadb.elasticsearch.entity.User;
import io.github.dunwu.javadb.elasticsearch.mapper.UserEsMapper;
import io.github.dunwu.javadb.elasticsearch.util.ElasticsearchUtil;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Demo {

private static final String HOSTS = "127.0.0.1:9200";
private static final RestHighLevelClient restHighLevelClient = ElasticsearchUtil.newRestHighLevelClient(HOSTS);
private static final String env = "test";
private static final ElasticsearchTemplate elasticsearchTemplate
= ElasticsearchFactory.newElasticsearchTemplate(env);

public static void main(String[] args) throws IOException, InterruptedException {

UserEsMapper mapper = new UserEsMapper(restHighLevelClient);
UserEsMapper mapper = new UserEsMapper(elasticsearchTemplate);

System.out.println("索引是否存在:" + mapper.isIndexExists());

User jack = User.builder().id(1L).username("jack").age(18).build();
User tom = User.builder().id(2L).username("tom").age(20).build();
List<User> users = Arrays.asList(jack, tom);

System.out.println("批量插入:" + mapper.batchInsert(users));
System.out.println("批量插入:" + mapper.batchSave(users));
System.out.println("根据ID查询:" + mapper.getById("1").toString());
System.out.println("根据ID查询:" + mapper.pojoById("2").toString());
System.out.println("根据ID批量查询:" + mapper.pojoListByIds(Arrays.asList("1", "2")).toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package io.github.dunwu.javadb.elasticsearch;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.util.List;
import java.util.stream.Collectors;

/**
* Elasticsearch 客户端实例工厂
*
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @date 2024-02-07
*/
@Slf4j
public class ElasticsearchFactory {

public static int CONNECT_TIMEOUT_MILLIS = 1000;

public static int SOCKET_TIMEOUT_MILLIS = 30000;

public static int CONNECTION_REQUEST_TIMEOUT_MILLIS = 500;

public static int MAX_CONN_TOTAL = 30;

public static int MAX_CONN_PER_ROUTE = 10;

public static RestClient newRestClient() {
// 从配置中心读取环境变量
String env = "test";
return newRestClient(env);
}

public static RestClient newRestClient(String env) {
String hosts = getDefaultEsAddress(env);
return newRestClient(toHttpHostList(hosts));
}

public static RestClient newRestClient(HttpHost[] httpHosts) {
RestClientBuilder builder = getRestClientBuilder(httpHosts);
if (builder == null) {
return null;
}
try {
return builder.build();
} catch (Exception e) {
log.error("【ES】connect failed.", e);
return null;
}
}

public static RestHighLevelClient newRestHighLevelClient() {
// 从配置中心读取环境变量
String env = "test";
return newRestHighLevelClient(env);
}

public static RestHighLevelClient newRestHighLevelClient(String env) {
String hosts = getDefaultEsAddress(env);
return newRestHighLevelClient(toHttpHostList(hosts));
}

public static RestHighLevelClient newRestHighLevelClient(HttpHost[] httpHosts) {
RestClientBuilder builder = getRestClientBuilder(httpHosts);
if (builder == null) {
return null;
}
try {
return new RestHighLevelClient(builder);
} catch (Exception e) {
log.error("【ES】connect failed.", e);
return null;
}
}

public static ElasticsearchTemplate newElasticsearchTemplate() {
// 从配置中心读取环境变量
String env = "test";
return newElasticsearchTemplate(env);
}

public static ElasticsearchTemplate newElasticsearchTemplate(String env) {
String hosts = getDefaultEsAddress(env);
return newElasticsearchTemplate(toHttpHostList(hosts));
}

public static ElasticsearchTemplate newElasticsearchTemplate(HttpHost[] httpHosts) {
RestHighLevelClient client = newRestHighLevelClient(httpHosts);
if (client == null) {
return null;
}
return new ElasticsearchTemplate(client);
}

public static ElasticsearchTemplate newElasticsearchTemplate(RestHighLevelClient client) {
if (client == null) {
return null;
}
return new ElasticsearchTemplate(client);
}

public static RestClientBuilder getRestClientBuilder(HttpHost[] httpHosts) {
if (ArrayUtil.isEmpty(httpHosts)) {
log.error("【ES】connect failed. hosts are empty.");
return null;
}
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
restClientBuilder.setRequestConfigCallback(builder -> {
builder.setConnectTimeout(CONNECT_TIMEOUT_MILLIS);
builder.setSocketTimeout(SOCKET_TIMEOUT_MILLIS);
builder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT_MILLIS);
return builder;
});
restClientBuilder.setHttpClientConfigCallback(builder -> {
builder.setMaxConnTotal(MAX_CONN_TOTAL);
builder.setMaxConnPerRoute(MAX_CONN_PER_ROUTE);
return builder;
});
return restClientBuilder;
}

private static HttpHost[] toHttpHostList(String hosts) {
if (StrUtil.isBlank(hosts)) {
return null;
}
List<String> strList = StrUtil.split(hosts, ",");
List<HttpHost> list = strList.stream().map(str -> {
List<String> params = StrUtil.split(str, ":");
return new HttpHost(params.get(0), Integer.parseInt(params.get(1)), "http");
}).collect(Collectors.toList());
if (CollectionUtil.isEmpty(list)) {
return new HttpHost[0];
}
return list.toArray(new HttpHost[0]);
}

public static String getDefaultEsAddress() {
// 从配置中心读取环境变量
String env = "test";
return getDefaultEsAddress(env);
}

private static String getDefaultEsAddress(String env) {
String defaultAddress;
switch (env) {
case "prd":
defaultAddress = "127.0.0.1:9200,127.0.0.2:9200,127.0.0.3:9200";
break;
case "pre":
defaultAddress = "127.0.0.1:9200";
break;
case "test":
default:
defaultAddress = "127.0.0.1:9200";
break;
}
return defaultAddress;
}

}
Loading

0 comments on commit 14b5853

Please sign in to comment.