栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch(7)springbooot整合ES

ElasticSearch(7)springbooot整合ES

使用springBoot整合ES 1 搭建SpringBoot工程 2 引入Elastiesearch相关坐标

引用

        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.4.0
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
            7.4.0
        
        
            org.elasticsearch
            elasticsearch
            7.4.0
        
3 创建编辑配置类

application.yml

elasticsearch:
  host: 192.168.156.131
  port: 9200

ElasticSearchConfig

package com.yy.esdemo.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
    private String host;
    private int port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Bean
    public RestHighLevelClient client(){
        return new RestHighLevelClient(RestClient.builder(
                new HttpHost(
                        host,
                        port,
                        "http"
                )
        ));
    }
}

4 测试

EsDemoApplicationTests

package com.yy.esdemo;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class EsDemoApplicationTests {
    @Autowired
    private RestHighLevelClient client;

    @Test
    void contextLoads() {
        //创建es客户端对象
//        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
//                new HttpHost(
//                        "192.168.156.131",
//                        9200,
//                        "http"
//                )
//        ));
        System.out.println(client);

    }

}

运行结果截图如下,证明整合成功

项目结构:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/680599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号