最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java操作Elasticsearch?rest-high-level-client?的基本使用

 更新時間:2022年10月25日 16:49:23   作者:AskaJohnny  
這篇文章主要介紹了Java操作Elasticsearch?rest-high-level-client?的基本使用,本篇主要講解一下?rest-high-level-client?去操作?Elasticsearch的方法,結(jié)合實例代碼給大家詳細講解,需要的朋友可以參考下

Elasticsearch rest-high-level-client 基本操作

本篇主要講解一下 rest-high-level-client 去操作 Elasticsearch , 雖然這個客戶端在后續(xù)版本中會慢慢淘汰,但是目前大部分公司中使用Elasticsearch 版本都是6.x 所以這個客戶端還是有一定的了解

前置準備

  • 準備一個SpringBoot環(huán)境 2.2.11 版本
  • 準備一個Elasticsearch 環(huán)境 我這里是8.x版本
  • 引入依賴 elasticsearch-rest-high-level-client 7.4.2

1.配置依賴

注意: 我使用的是 springboot 2.2.11 版本 , 它內(nèi)部的 elasticsearch 和 elasticsearch-rest-client 都是 6.8.13 需要注意

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
       <!-- 引入 elasticsearch 7.4.2  -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.4.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

      <!-- 排除 elasticsearch-rest-client , 也可不排除 為了把maven沖突解決   -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.4.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-client</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>elasticsearch</artifactId>
                    <groupId>org.elasticsearch</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 不引入會導致可能 使用 springboot的 elasticsearch-rest-client 6.8.13 -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.4.2</version>
        </dependency>

        <!-- elasticsearch 依賴 2.x 的 log4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
            <!--  排除掉 log4j-api 因為springbootstarter 中引入了loging模塊 -->
            <exclusions>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- junit 單元測試 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

2.構(gòu)建 RestHighLevelClient

highlevelclient 是 高級客戶端 需要通過它去操作 Elasticsearch , 它底層也是要依賴 rest-client 低級客戶端

@Slf4j
public class TestEsClient {

    private RestHighLevelClient client = null;
    private ObjectMapper objectMapper = new ObjectMapper();
		
    //構(gòu)建 RestHighLevelClient
    @Before
    public void prepare() {
        // 創(chuàng)建Client連接對象
        String[] ips = {"172.16.225.111:9200"};
        HttpHost[] httpHosts = new HttpHost[ips.length];
        for (int i = 0; i < ips.length; i++) {
            httpHosts[i] = HttpHost.create(ips[i]);
        }
        RestClientBuilder builder = RestClient.builder(httpHosts);
        client = new RestHighLevelClient(builder);
    }
}

3.創(chuàng)建索引 client.indices().create

創(chuàng)建索引 需要使用 CreateIndexRequest 對象 , 操作 索引基本上是 client.indices().xxx

構(gòu)建 CreateIndexRequest 對象

@Test
public void test1() {
    CreateIndexRequest request = new CreateIndexRequest("blog1");
    try {
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);
        boolean acknowledged = createIndexResponse.isAcknowledged();
        log.info("[create index blog :{}]", acknowledged);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

4.刪除索引 client.indices().delete

構(gòu)建 DeleteIndexRequest 對象

@Test
public void testDeleteIndex(){
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog1");
    try {
        AcknowledgedResponse response = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        log.info("[delete index response: {}", response.isAcknowledged());
    } catch (IOException e) {
        e.printStackTrace();
    }

}

5.查詢索引 client.indices().get

構(gòu)建 GetIndexRequest 對象

@Test
public void testSearchIndex() {

    GetIndexRequest request = new GetIndexRequest("blog1");
    try {
        GetIndexResponse getIndexResponse =
                client.indices().get(request, RequestOptions.DEFAULT);
        Map<String, List<AliasMetaData>> aliases = getIndexResponse.getAliases();
        Map<String, MappingMetaData> mappings = getIndexResponse.getMappings();
        Map<String, Settings> settings = getIndexResponse.getSettings();
        log.info("[aliases: {}]", aliases);
        log.info("[mappings: {}]", mappings);
        log.info("[settings: {}", settings);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

可以根據(jù) response 獲取 aliases , mappings , settings 等等 和 Kibana 中返回的一樣

6.插入文檔 client.index

插入文檔 需要使用 IndexRequest 對象 , 注意 不是 InsertRequest , 不知道為什么不這樣定義 感覺會更加好理解

request.source(blogInfoJsonStr, XContentType.JSON);

@Test
public void insertDoc() {
    IndexRequest request = new IndexRequest();
    request.index("blog1").id("1");
    BlogInfo blogInfo =
            new BlogInfo()
                    .setBlogName("Elasticsearch 入門第一章")
                    .setBlogType("Elasticsearch")
                    .setBlogDesc("本篇主要介紹了Elasticsearch 的基本client操作");
    try {
         //提供java 對象的 json str
        String blogInfoJsonStr = objectMapper.writeValueAsString(blogInfo);
        
        request.source(blogInfoJsonStr, XContentType.JSON);
        // 這里會拋錯 原因是 我的 Elasticsearch 版本8.x 而 使用的 restHighLevel 已經(jīng)解析不了,因為新的es已經(jīng)不推薦使用
        // restHighLevel,而使用 Elasticsearch Java API Client
        IndexResponse index = client.index(request, RequestOptions.DEFAULT);
        log.info("[Result insert doc :{} ]", index);
    } catch (IOException e) {
    }

7.查詢文檔 client.get

注意 getResponse.getSourceAsString() 返回文檔數(shù)據(jù)

@Test
public void testSelectDoc() {
    GetRequest getRequest = new GetRequest();
    getRequest.index("blog1").id("1");
    try {
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        BlogInfo blogInfo =
                objectMapper.readValue(getResponse.getSourceAsString(), BlogInfo.class);
        log.info("[get doc :{}] ", blogInfo);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

8.刪除文檔 client.delete

注意 刪除文檔 的 response 也解析不了 Elasticsearch 8.x 版本

@Test
public void testDeleteDoc() {
    DeleteRequest deleteRequest = new DeleteRequest();
    deleteRequest.index("blog1").id("1");
    try {
        // 這里也會拋錯 和上面的一樣
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        log.info("[delete response:{} ]", deleteResponse);
    } catch (IOException e) {
    }
}

總結(jié)

本篇主要介紹了 java 操作Elasticsearch 的客戶端 rest-high-level-client 的基本使用 , 如果你是使用springboot 需要注意jar 沖突問題, 后續(xù)操作 Elasticsearch 客戶端 逐漸變成 Elasticsearch Java API Client , 不過目前大部分還是使用 rest-high-level-client

到此這篇關(guān)于Java操作Elasticsearch rest-high-level-client 的基本使用的文章就介紹到這了,更多相關(guān)Elasticsearch rest-high-level-client內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • GSON實現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程

    GSON實現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程

    GSON是Google編寫并在在GitHub上開源的Java序列化與反序列化JSON的類庫,今天我們就來總結(jié)一下使用GSON實現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程
    2016-06-06
  • 劍指Offer之Java算法習題精講二叉樹與鏈表

    劍指Offer之Java算法習題精講二叉樹與鏈表

    跟著思路走,之后從簡單題入手,反復去看,做過之后可能會忘記,之后再做一次,記不住就反復做,反復尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • MyBatis框架簡介及入門案例詳解

    MyBatis框架簡介及入門案例詳解

    MyBatis是一個優(yōu)秀的持久層框架,它對jdbc的操作數(shù)據(jù)庫的過程進行封裝,使開發(fā)者只需要關(guān)注SQL本身,而不需要花費精力去處理例如注冊驅(qū)動、創(chuàng)建connection、創(chuàng)建statement、手動設(shè)置參數(shù)、結(jié)果集檢索等jdbc繁雜的過程代碼,本文將作為最終篇為大家介紹MyBatis的使用
    2022-08-08
  • SpringBoot如何配置數(shù)據(jù)庫主從shardingsphere

    SpringBoot如何配置數(shù)據(jù)庫主從shardingsphere

    這篇文章主要介紹了SpringBoot如何配置數(shù)據(jù)庫主從shardingsphere問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 基于Java事件監(jiān)聽編寫一個中秋猜燈謎小游戲

    基于Java事件監(jiān)聽編寫一個中秋猜燈謎小游戲

    眾所周知,JavaSwing是Java中關(guān)于窗口開發(fā)的一個工具包,可以開發(fā)一些窗口程序,然后由于工具包的一些限制,導致Java在窗口開發(fā)商并沒有太多優(yōu)勢,不過,在JavaSwing中關(guān)于事件的監(jiān)聽機制是我們需要重點掌握的內(nèi)容,本文將基于Java事件監(jiān)聽編寫一個中秋猜燈謎小游戲
    2023-09-09
  • 排查Java應用內(nèi)存泄漏問題的步驟

    排查Java應用內(nèi)存泄漏問題的步驟

    這篇文章主要介紹了排查Java應用內(nèi)存泄漏問題的步驟,幫助大家更好的理解和學習Java,感興趣的朋友可以了解下
    2020-11-11
  • Java常用正則表達式驗證類完整實例【郵箱、URL、IP、電話、身份證等】

    Java常用正則表達式驗證類完整實例【郵箱、URL、IP、電話、身份證等】

    這篇文章主要介紹了Java常用正則表達式驗證類,結(jié)合完整實例形式分析了Java針對郵箱、網(wǎng)址URL、IP地址、電話、身份證等正則驗證相關(guān)操作技巧,需要的朋友可以參考下
    2018-12-12
  • SpringBoot中使用MyBatis-Plus詳細步驟

    SpringBoot中使用MyBatis-Plus詳細步驟

    MyBatis-Plus是MyBatis的增強工具,簡化了MyBatis的使用,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • Java使用Geodesy進行地理計算的技術(shù)指南

    Java使用Geodesy進行地理計算的技術(shù)指南

    在地理信息系統(tǒng) (GIS) 和導航應用中,精確的地理計算是基礎(chǔ),Geodesy 是一個流行的 Java 庫,用于處理地理位置、距離、方向等相關(guān)計算,本博客將介紹 Geodesy 的核心功能,并提供詳細的實踐樣例,幫助開發(fā)者快速上手,需要的朋友可以參考下
    2025-02-02
  • 如何使用Java redis實現(xiàn)發(fā)送手機驗證碼功能

    如何使用Java redis實現(xiàn)發(fā)送手機驗證碼功能

    這篇文章主要介紹了如何使用Java redis實現(xiàn)發(fā)送手機驗證碼功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05

最新評論

遂宁市| 阜新| 莎车县| 安西县| 二连浩特市| 桓仁| 乐安县| 顺平县| 西丰县| 望江县| 科尔| 科技| 九江县| 高台县| 华阴市| 防城港市| 康乐县| 东乌珠穆沁旗| 翁牛特旗| 浮山县| 苗栗县| 晋宁县| 台南县| 古蔺县| 拜泉县| 霍林郭勒市| 卢湾区| 岢岚县| 剑河县| 吉林市| 永靖县| 瑞金市| 寿宁县| 行唐县| 松滋市| 南京市| 县级市| 隆昌县| 新宾| 甘南县| 渝中区|