Elasticsearch 7.6.2 基本搭建配置 及 JavaAPI 增刪改查

基本搭建配置

=================================基本環境配置==================================

配置linux系統環境(參考:http://blog.csdn.net/satiling/article/details/59697916)

(1)編輯limits.conf 添加類似如下內容
[itstar@bigdata111 elasticsearch-5.6.1]$ sudo vi /etc/security/limits.conf
添加如下內容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

(2)修改配置sysctl.conf
[itstar@bigdata111 elasticsearch-5.6.1]$ sudo vi /etc/sysctl.conf 
添加下面配置:
vm.max_map_count=655360
並執行命令:
[itstar@bigdata111 elasticsearch-5.6.1]$ sudo sysctl -p

=================================elasticsearch.yml==================================

#集羣名稱
cluster.name: my-application

#節點名稱,其他節點分別依次遞增
node.name: node-1

# 表示該節點會不會作爲主節點,true表示會;false表示不會
node.master: true

# 當前節點是否用於存儲數據,是:true、否:false
node.data: true

#數據地址
path.data: /opt/module/elasticsearch-7.6.2/data

#日誌地址
path.logs: /opt/module/elasticsearch-7.6.2/logs

#內存超出後是否鎖定
bootstrap.memory_lock: false

#是否檢測支持SecComp,Centos6爲不支持
bootstrap.system_call_filter: false

#網卡設置,其他階段寫各自的主機名
network.host: bigdata111

#是否發現該節點,寫自己的ip地址,我之前寫主機名好像不行.此段自測
discovery.seed_hosts: ["192.168.220.112", "192.168.220.113", "192.168.220.114"]

#是否發現成爲主節點,道理同上
cluster.initial_master_nodes: ["192.168.220.112", "192.168.220.113", "192.168.220.114"]

Pom提供

   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <!-- 使用自定義ES 版本 -->
        <elasticsearch.version>7.6.2</elasticsearch.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.11.1</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.24</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

JavaAPI代碼

下邊是ES的增刪改查基本JavaAPI代碼

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.security.user.User;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ElasticSearchConfig {
    static RestHighLevelClient client;

    @Before
    public void client() throws IOException {
        client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("bigdata111", 9200, "http")
        ));
    }

    //創建索引
    @Test
    public void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("index");
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);

        System.out.println(createIndexResponse.index());
    }

    //刪除索引
    @Test
    public void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("index");
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        boolean isSuccessful = delete.isAcknowledged();

        System.out.println(isSuccessful);
    }

    //返回索引是否存在
    @Test
    public void existIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("index");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

        System.out.println(exists);
    }

    //刪除文檔
    @Test
    public void deleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("index", "2");
        request.timeout("1s");
        DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);

        System.out.println(deleteResponse.status());
    }



    //查詢
    @Test
    public void search() throws IOException {
        SearchRequest request = new SearchRequest("index");

        //構建搜索條件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        // SearchRequest 搜索請求
        // SearchSourceBuilder 條件構造
        // HighlightBuilder 構建高亮
        // TermQueryBuilder 精確查詢
        // MatchAllQueryBuilder .....
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        sourceBuilder.query(matchAllQueryBuilder)
                .timeout(new TimeValue(60, TimeUnit.SECONDS));

        request.source(sourceBuilder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits(), true));
        System.out.println("===================================");
        for (SearchHit documentFields : searchResponse.getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }

    }
    //批量插入數據
    @Test
    public void BulkRequest() throws IOException {

        BulkRequest bulkRequest = new BulkRequest()
                .timeout("5s");

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");

        List<User> users = Arrays.asList(new User("dai1", list), new User("dai2", list), new User("dai3", list));

        for (User user : users) {
            bulkRequest.add(new IndexRequest("index")
                    //.id("xxx")
                    .source(JSON.toJSONString(user), XContentType.JSON));
        }

        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        //是否失敗,false表示成功
        System.out.println(bulkResponse.hasFailures());
        System.out.println(bulkResponse.status());
    }


    //更新文檔
    @Test
    public void updateDocument() throws IOException {
        UpdateRequest request = new UpdateRequest("index", "1");
        request.timeout("1s");

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");

        User user = new User("dai22", list);
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);

        System.out.println(updateResponse.status());
    }




}

常見問題:

1. 最好是非root用戶 

useradd es

chown -R es.es /opt/module

2.es 7.6.2的header google插件用之前的還不能顯示,暫時沒時間解決.有時間更.

3. es 7.6 的 jdk顯示用jdk11的版本,不用管,我依舊用的 jdk8

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章