java 连接操作Elasticsearch(一)

  • 导入Elasticsearch依赖包
复制代码
    <!-- Elasticsearch6.4.3 依赖 -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>transport-netty4-client</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
复制代码
  • properties配置文件
复制代码
elasticsearch.ip=es库ip
elasticsearch.port=9300(es对外连接端口)
elasticsearch.cluster.name=elasticsearch
elasticsearch.shards=3
elasticsearch.replicas=1
elasticsearch.pool=3
elasticsearch.regenerateIndexEnabled=false
elasticsearch.syncDataEnabled=true
elasticsearch.sniff=true
复制代码

 

  • 获取连接
复制代码
public static Connection getConn(){
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties");
        // 使用properties对象加载输入流
        try {
            properties.load(in);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String url = properties.getProperty("postgresql.url");
        String driver = properties.getProperty("postgresql.driver");
        String user = properties.getProperty("postgresql.user");
        String password = properties.getProperty("postgresql.password");
        
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password); //获取连接
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return conn;
    }    
复制代码
  • 获取单例客户端
复制代码
public static TransportClient getSingleClient() {
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties");
        // 使用properties对象加载输入流
        try {
            properties.load(in);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        String host = properties.getProperty("elasticsearch.ip");
        String cluster_name = properties.getProperty("elasticsearch.cluster.name");
        int port = Integer.parseInt(properties.getProperty("elasticsearch.port"));
        
        /*
         * int shards =
         * Integer.parseInt(properties.getProperty("elasticsearch.shards")); int
         * replicas =
         * Integer.parseInt(properties.getProperty("elasticsearch.replicas"));
         * String sniff = properties.getProperty("elasticsearch.sniff");
         */

        if (client == null) {
            synchronized (TransportClient.class) {
            }
            if (client == null) {
                try {
                    /**
                     * 配置信息 client.transport.sniff 增加嗅探机制,找到ES集群
                     * thread_pool.search.size 增加线程池个数,暂时设为3
                     */
                    Settings esSetting = Settings.builder().put("cluster.name", cluster_name).build();
                    // 配置信息Settings自定义
                    client = new PreBuiltTransportClient(esSetting);
                    TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(host), port);
                    client.addTransportAddress(transportAddress);
                } catch (Exception e) {
                    LOGGER.error("elasticsearch TransportClient create error!!!", e);
                }
            }
        }
        return client;
    }
    
复制代码
  • 获取es库客户端
复制代码
@SuppressWarnings("resource")
    public static ElasticsearchClient getEsClient(){
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties");
        // 使用properties对象加载输入流
        try {
            properties.load(in);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        String host = properties.getProperty("elasticsearch.ip");
        int port = Integer.parseInt(properties.getProperty("elasticsearch.port"));

        try {
            es_client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return es_client;
    }
复制代码

 

posted @   过氧化氢  阅读(1353)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示