zoukankan      html  css  js  c++  java
  • HighLevelRestApi管理ES

    https://blog.csdn.net/u010177412/article/details/82835230

    https://www.cnblogs.com/kevingrace/p/10682264.html

    https://www.cnblogs.com/slowcity/p/11727579.html

    https://www.cnblogs.com/swordfall/p/9981883.html

    es框架: https://gitee.com/wzlee/ESClientRHL/

    es代码示例: https://www.cnblogs.com/gmhappy/p/9472383.html

    https://www.jianshu.com/p/3cb80dcf514f

    https://blog.csdn.net/u010177412/article/details/82835230

    示例代码

    package com.example.conf;
    
    import org.apache.http.Header;
    import org.apache.http.HttpHost;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
    import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
    import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
    import org.elasticsearch.client.*;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.xcontent.XContentType;
    
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    public class ESTest {
    
        public static void main(String[] args) throws IOException {
            String username = "admin";
            String password = "admin123";
            String hostname = "192.168.49.131";
            int port = 9200;
    
            final CredentialsProvider provider = new BasicCredentialsProvider();
    
            provider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(username, password));
    
            RestClientBuilder clientBuilder = RestClient.builder(new HttpHost(hostname, port));
    
            clientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    httpClientBuilder.disableAuthCaching();
                    return httpClientBuilder.setDefaultCredentialsProvider(provider);
                }
            });
            RestClient restClient = clientBuilder.build();
    
            RestHighLevelClient highLevelClient = new RestHighLevelClient(clientBuilder);
    
            Request request = new Request(
                    "GET",
                    "_cat/indices?v");
            Response response = restClient.performRequest(request);
            HttpHost host = response.getHost();
            int statusCode = response.getStatusLine().getStatusCode();
            Header[] headers = response.getHeaders();
            String responseBody = EntityUtils.toString(response.getEntity());
    
            String index = "es-demo-" + todayDate();
    
            try {
                indexCreate1(highLevelClient, index);
            } catch (Exception e){
                e.printStackTrace();
            }
    
    
            restClient.close();
            highLevelClient.close();
        }
    
        /**
         * 验证索引是否存在
         *
         * @param index
         *            索引名称
         * @return
         * @throws Exception
         */
        public static boolean indexExists(RestHighLevelClient client, String index) throws Exception {
            GetIndexRequest request = new GetIndexRequest();
            request.indices(index);
            request.local(false);
            request.humanReadable(true);
    
            boolean exists = client.indices().exists(request);
            return exists;
        }
    
        /**
         *
         * @param index
         * @param indexType
         * @param properties
         *            结构: {name:{type:text}} {age:{type:integer}}
         * @return
         * @throws Exception
         */
        public static boolean indexCreate(RestHighLevelClient client, String index, String indexType,
                                   Map<String, Object> properties) throws Exception {
    
            if (indexExists(client, index)) {
                return true;
            }
            CreateIndexRequest request = new CreateIndexRequest(index);
            request.settings(Settings.builder().put("index.number_of_shards", 3)
                    .put("index.number_of_replicas", 1));
    
            Map<String, Object> jsonMap = new HashMap<>();
            Map<String, Object> mapping = new HashMap<>();
            mapping.put("properties", properties);
            jsonMap.put(indexType, mapping);
            request.mapping(indexType, jsonMap);
    
            CreateIndexResponse createIndexResponse = client.indices().create(
                    request, RequestOptions.DEFAULT);
            boolean acknowledged = createIndexResponse.isAcknowledged();
            return acknowledged;
        }
    
        /**
         *
         * @param client
         * @param index
         * @return
         * @throws Exception
         */
        public static boolean indexCreate1(RestHighLevelClient client, String index)
                                    throws Exception {
            if (indexExists(client, index)) {
                return true;
            }
    
            CreateIndexRequest request = new CreateIndexRequest(index);
            request.settings(Settings.builder()
                    .put("index.number_of_shards", 3)
                    .put("index.number_of_replicas", 1)
            );
            request.mapping("_doc",
                    "{
    " +
                            "  "_doc": {
    " +
                            "    "properties": {
    " +
                            "      "message": {
    " +
                            "        "type": "text"
    " +
                            "      }
    " +
                            "    }
    " +
                            "  }
    " +
                            "}",
                    XContentType.JSON);
            CreateIndexResponse createIndexResponse = client.indices().create(
                    request, RequestOptions.DEFAULT);
            boolean acknowledged = createIndexResponse.isAcknowledged();
            return acknowledged;
        }
    
        public static String todayDate() {
            Date date=new Date();
            System.out.println(date);
            SimpleDateFormat dateFormat=new SimpleDateFormat("YYYY.MM.dd");//设置当前时间的格式,为年-月-日
            System.out.println(dateFormat.format(date));
            return dateFormat.format(date);
        }
    
    }
    

      

    pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo-es</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo-es</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>6.4.3</version>
            </dependency>
    
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.4.3</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

      

    示例代码2

    https://github.com/xiaof-github/spring-es-demo

  • 相关阅读:
    Html禁止粘贴 复制 剪切
    表单标签
    自构BeanHandler(用BeansUtils)
    spring配置中引入properties
    How Subcontracting Cockpit ME2ON creates SD delivery?
    cascadia code一款很好看的微软字体
    How condition value calculated in sap
    Code in SAP query
    SO Pricing not updated for partial billing items
    Javascript learning
  • 原文地址:https://www.cnblogs.com/beilong/p/12248814.html
Copyright © 2011-2022 走看看