之前用的7.6.1版本的elasticsearch放在项目中没问题,一切换成8.10.2版本就频频报错,百度了一下是版本不兼容问题报错信息如下:
jar:file:/C:/Users/Administrator/.m2/repository/org/elasticsearch/client/elasticsearch-rest-client/7.12.1/elasticsearch-rest-client-7.12.1.jar!/org/elasticsearch/client/RequestOptions$Builder.class
The class hierarchy was loaded from the following locations:
org.elasticsearch.client.RequestOptions.Builder: file:/C:/Users/Administrator/.m2/repository/org/elasticsearch/client/elasticsearch-rest-client/7.12.1/elasticsearch-rest-client-7.12.1.jar
我的maven环境是这样的
<elasticsearch.version>8.10.2</elasticsearch.version>
<elasticsearch-clients.version>8.1.0</elasticsearch-clients.version>
<jackson.version>2.12.3</jackson.version>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>x-pack-sql-jdbc</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>${elasticsearch-clients.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
一开始我把elasticsearch-clients.version
版本设置成和elasticsearch版本一样了,后来设置成8.1.0就好了.
把8.10.2集成到Java中直接看ElasticSearch 8.10.2 最新版 集成springboot,包括安全认证,使用Elasticsearch Java API Client,地理位置查询geoDistance_springboot es8-CSDN博客这个链接就行
设置yml文件
spring:
elasticsearch:
rest:
# 是否启用es
enable: true
uris: localhost:9200
host: localhost
port: 9200
username:
password:
index: indexName
crtName: http_ca.crt
其中es的证书在 config/certs目录下
然后创建ElasticSearchConfig类
package com.ruoyi.core.config;
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import javax.annotation.PostConstruct;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
/**
* es8的Java客户端配置
* author:Geng
*/
@Configuration
//@Slf4j
public class ElasticSearchConfig {
@Value("${spring.elasticsearch.rest.host}")
private String host;
@Value("${spring.elasticsearch.rest.enable}")
private boolean enable;
@Value("${spring.elasticsearch.rest.port}")
private int port;
@Value("${spring.elasticsearch.rest.username}")
private String userName;
@Value("${spring.elasticsearch.rest.password}")
private String passWord;
@Value("${spring.elasticsearch.rest.crtName}")
private String tempCrtName;
private static String crtName;
@PostConstruct
private void init() {
crtName = tempCrtName;
}
/**
* 解析配置的字符串,转为HttpHost对象数组
*
* @return
*/
private HttpHost toHttpHost() {
HttpHost httpHost = new HttpHost(host, port, "http");
return httpHost;
}
/**
* 同步客户端
* @return
* @throws Exception
*/
@Bean
public ElasticsearchClient clientBySync() throws Exception {
ElasticsearchTransport transport = getElasticsearchTransport(userName, passWord, toHttpHost());
return new ElasticsearchClient(transport);
}
/**
* 异步客户端
* @return
* @throws Exception
*/
@Bean
public ElasticsearchAsyncClient clientByAsync() throws Exception {
ElasticsearchTransport transport = getElasticsearchTransport(userName, passWord, toHttpHost());
return new ElasticsearchAsyncClient(transport);
}
/**
* 传输对象
* @return
* @throws Exception
*/
@Bean
public ElasticsearchTransport getTransport() throws Exception {
return getElasticsearchTransport(userName, passWord, toHttpHost());
}
private static SSLContext buildSSLContext() {
ClassPathResource resource = new ClassPathResource(crtName);
SSLContext sslContext = null;
try {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Certificate trustedCa;
try (InputStream is = resource.getInputStream()) {
trustedCa = factory.generateCertificate(is);
}
KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, null);
trustStore.setCertificateEntry("ca", trustedCa);
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStore, null);
sslContext = sslContextBuilder.build();
} catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException |
KeyManagementException e) {
// log.error("ES连接认证失败", e);
}
return sslContext;
}
private static ElasticsearchTransport getElasticsearchTransport(String username, String passwd, HttpHost... hosts) {
// 账号密码的配置
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, passwd));
// 自签证书的设置,并且还包含了账号密码
RestClientBuilder.HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder
.setSSLContext(buildSSLContext())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setDefaultCredentialsProvider(credentialsProvider);
// 用builder创建RestClient对象
RestClient client = RestClient
.builder(hosts)
.setHttpClientConfigCallback(callback)
.build();
return new RestClientTransport(client, new JacksonJsonpMapper());
}
}
启动测试类
package com.ruoyi.controller.system.web.cw;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.transport.ElasticsearchTransport;
import com.ruoyi.RuoYiApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class CwAnchorExpensesControllerTest {
@Autowired
private ElasticsearchClient esClient;
@Autowired
private ElasticsearchTransport transport;
@Test
public void createTest() throws Exception {
//获取索引客户端对象
ElasticsearchIndicesClient indices = esClient.indices();
//创建索引 采用构建器的方式构建(在创建之前需要先判断该索引是否存在)
boolean exists = indices.exists(u -> u.index("user")).value();
if (exists) {
System.out.println("该索引已存在!!");
} else {
CreateIndexResponse createIndexResponse = indices.create(c -> c.index("user"));
}
//查询索引
GetIndexResponse getResponse = indices.get(g -> g.index("user"));
System.out.println("查询索引:"+getResponse);
//删除索引
DeleteIndexResponse deleteResponse = indices.delete(d -> d.index("user"));
System.out.println("删除索引:"+deleteResponse.acknowledged());
}
@Test
public void deleteIndexTest() throws Exception {
System.out.println(esClient.indices().delete(d -> d.index("test_doc")).acknowledged());
System.out.println(esClient.indices().delete(d -> d.index("test_query")).acknowledged());
System.out.println(esClient.indices().delete(d -> d.index("test_index")).acknowledged());
}
}
测试的时候多添加了几条索引,这里直接删除,完事儿!
最后推荐一下使用客户端连接es
# 启用白金使用
POST /_license/start_trial?acknowledge=true&pretty
使用sql查询es文档