针对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官网提供了大量能使我们快速便捷地处理数据的函数和方法,可以去官网看下官网文档,以后会持续更新遇到的问题!

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值