针对java代码连接es线上数据库(本人亲测有效,做完就去上班了!)
前言
用Java代码去连接线上ip带账密的es数据库!亲测有效
一、es是什么?
注释:es全称elasticSearch,是一种大数据搜索工具,在大数据领域很牛批。例如百度等一些大型公司都是用elasticSearch作为搜索工具。还可以作为爬虫工具把数据爬到数据库,功能十分优秀。
二、操作步骤
1.Java代码连接线上es数据库
代码如下(示例):
```java
package com.dd.springboot_ess.utils;
import ch.qos.logback.core.net.server.Client;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
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.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.Arrays;
public class LianJieElasticSearch {
private static final String clusterNodes = "118.89.90.100:9200";//es集群节点
//用户名
private static final String account = "elastic";
//密码
private static final String passWord = "hxes2580xxx";
public static ElasticsearchClient client;
public static RestClientBuilder builder;
public static RestClient restClient;
public static ElasticsearchTransport transport;
//有密码登录
// @Test
// public static void main(String[] args) {
public ElasticsearchClient LianJie () {
// try {
HttpHost[] httpHosts = Arrays.stream(clusterNodes.split(",")).map(x -> {
String[] hostInfo = x.split(":");
return new HttpHost(hostInfo[0], Integer.parseInt(hostInfo[1]));
}).toArray(HttpHost[]::new);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//设置账号密码
credentialsProvider.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials(account, passWord));
builder = RestClient.builder(httpHosts)
.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider));
// Create the low-level client
restClient = builder.build();
// Create the transport with a Jackson mapper
transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
// And create the API client
client = new ElasticsearchClient(transport);
// GetIndexResponse getIndexResponse=client.indices().get(e->e.index("test"));
// System.out.println("getIndexResponse.result()="+getIndexResponse.result());
// System.out.println("getIndexResponse.result().keySet()="+getIndexResponse.result().keySet());
// System.out.println(acknowledged.toString());
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// try {
// client.shutdown();
// transport.close();
// restClient.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
return client;
}
}
2.注意事项
代码如下:(示例)
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// try {
// client.shutdown();
// transport.close();
// restClient.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
该处一定要注释掉,不然连上就关闭了。无法连接到线上,还有一件事。连接线上必须要账号密码。连接本地localhost不需要密码。
3.创建自己的索引名(索引名就是数据库的表名,有兴趣可以看下狂神的es教程)
es8.x以后已经废弃了高级客户端的连接方法,之前我在网上看教程都是7.x,现在已经完全废弃掉了,在这也避免同学们踩坑。希望看到的小伙伴能帮助到你!
狂神说elasticSearch链接进入
总结
这里对文章进行总结:
这是我第一次写博客,我的初心就是想把自己遇到的难点分享出来,避免和我一样的人在一个小问题上浪费太多的时间。我们的目的就是高效开发项目,有些写的不好的地方希望大家指正!
以上就是今天要讲的内容,本文仅仅简单介绍了java连接线上es数据库的使用,而es官网提供了大量能使我们快速便捷地处理数据的函数和方法,可以去官网看下官网文档,以后会持续更新遇到的问题!