欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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>

        <!-- 不引入會導(dǎo)致可能 使用 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)文章

  • MDC在多線程中的使用方式

    MDC在多線程中的使用方式

    這篇文章主要介紹了MDC在多線程中的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java MAVEN 工程pom配置報錯解決方案

    Java MAVEN 工程pom配置報錯解決方案

    這篇文章主要介紹了Java MAVEN 工程pom配置報錯解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2019-10-10
  • System.getProperty(user.dir)定位問題解析

    System.getProperty(user.dir)定位問題解析

    System.getProperty(user.dir) 獲取的是啟動項目的容器位置,用IDEA是項目的根目錄,部署在tomcat上是tomcat的啟動路徑,即tomcat/bin的位置,這篇文章主要介紹了System.getProperty(user.dir)定位問題,需要的朋友可以參考下
    2023-05-05
  • java 根據(jù)身份證號碼判斷出生日期、性別、年齡的示例

    java 根據(jù)身份證號碼判斷出生日期、性別、年齡的示例

    這篇文章主要介紹了java 根據(jù)身份證號碼判斷出生日期、性別、年齡的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-10-10
  • IDEA代碼熱部署和熱加載的三種實現(xiàn)方案

    IDEA代碼熱部署和熱加載的三種實現(xiàn)方案

    在日常開發(fā)中,我們需要經(jīng)常修改 Java 代碼,手動重啟項目,查看修改后的效果,如果在項目小時,重啟速度比較快,等待的時間是較短的,我們可以使用代碼熱加載和熱部署解決該問題,本文給大家介紹了三種實現(xiàn)方案,需要的朋友可以參考下
    2023-11-11
  • Java_int、double型數(shù)組常用操作工具類(分享)

    Java_int、double型數(shù)組常用操作工具類(分享)

    下面小編就為大家?guī)硪黄狫ava_int、double型數(shù)組常用操作工具類(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 如何解決IDEA git拉取代碼后本地代碼莫名消失的問題

    如何解決IDEA git拉取代碼后本地代碼莫名消失的問題

    這篇文章主要介紹了如何解決IDEA git拉取代碼后本地代碼莫名消失的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java Swing GroupLayout分組布局的實現(xiàn)代碼

    Java Swing GroupLayout分組布局的實現(xiàn)代碼

    這篇文章主要介紹了Java Swing GroupLayout分組布局的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2019-12-12
  • SpringBoot如何注冊Servlet、Filter、Listener的幾種方式

    SpringBoot如何注冊Servlet、Filter、Listener的幾種方式

    在Servlet 3.0之前都是使用web.xml文件進行配置,這篇文章主要介紹了SpringBoot如何注冊Servlet、Filter、Listener的幾種方式,在Servlet 3.0之前都是使用web.xml文件進行配置,
    2018-10-10
  • Java如何實現(xiàn)圖片的疊加與拼接操作

    Java如何實現(xiàn)圖片的疊加與拼接操作

    這篇文章主要介紹了Java如何實現(xiàn)圖片的疊加與拼接操作,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2019-11-11

最新評論