zoukankan      html  css  js  c++  java
  • es-06-java创建mapping和setting

    说实话, java的方式太繁琐, 不如直接使用DSL进行创建

    1, create

    package com.wenbronk.elasticsearch.usage.index;
    
    import com.wenbronk.elasticsearch.usage.highLevel.RestHighLevelClientParent;
    import org.elasticsearch.ElasticsearchException;
    import org.elasticsearch.action.ActionListener;
    import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
    import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
    import org.elasticsearch.action.support.ActiveShardCount;
    import org.elasticsearch.action.support.IndicesOptions;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.common.xcontent.XContentBuilder;
    import org.elasticsearch.common.xcontent.XContentFactory;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.rest.RestStatus;
    import org.junit.jupiter.api.Test;
    
    import java.io.IOException;
    
    public class Index_1_Create extends RestHighLevelClientParent {
    
        /**
         * index 使用java客户端不好创建, 可以在kibana中进行创建
         * 注意mapping一旦创建, 不可更改
         */
        @Test
        public void createIndices() throws IOException {
    
            CreateIndexRequest request = new CreateIndexRequest("twitter");
    
            // add partition
            request.source("{
    " +
                    "    "settings" : {
    " +
                    "        "number_of_shards" : 3,
    " +
                    "        "number_of_replicas" : 2
    " +
                    "    },
    " +
                    "    "mappings" : {
    " +
                    "        "tweet" : {
    " +
                    "            "properties" : {
    " +
                    "                "message" : { "type" : "text" }
    " +
                    "            }
    " +
                    "        }
    " +
                    "    },
    " +
                    "    "aliases" : {
    " +
                    "        "twitter_alias" : {}
    " +
                    "    }
    " +
                    "}", XContentType.JSON);
    
            // 配置可选参数
            request.timeout(TimeValue.timeValueMinutes(2));
    //        request.timeout("2m");
            request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    //        request.masterNodeTimeout("1m");
    //        request.waitForActiveShards(2);
            request.waitForActiveShards(ActiveShardCount.DEFAULT);
    
            // 使用异步的方式创建
            client.indices().createAsync(request, new ActionListener<CreateIndexResponse>() {
                @Override
                public void onResponse(CreateIndexResponse createIndexResponse) {
                    boolean acknowledged = createIndexResponse.isAcknowledged();
                    boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
                }
    
                @Override
                public void onFailure(Exception e) {
                    System.out.println(e.getCause());
                }
            });
        }
    
    }

    2, delete

    package com.wenbronk.elasticsearch.usage.index;
    
    import com.wenbronk.elasticsearch.usage.highLevel.RestHighLevelClientParent;
    import org.elasticsearch.ElasticsearchException;
    import org.elasticsearch.action.ActionListener;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
    import org.elasticsearch.action.support.IndicesOptions;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.rest.RestStatus;
    import org.junit.jupiter.api.Test;
    
    public class Index_2_Delete extends RestHighLevelClientParent {
    
        @Test
        public void deleteIndices() {
            DeleteIndexRequest request = new DeleteIndexRequest("twitter");
    
            request.timeout(TimeValue.timeValueMinutes(2));
            //        request.timeout("2m");
    //        request.masterNodeTimeout("1m");
            request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
            request.indicesOptions(IndicesOptions.lenientExpandOpen());
    
            client.indices().deleteAsync(request, new ActionListener<DeleteIndexResponse>() {
                @Override
                public void onResponse(DeleteIndexResponse deleteIndexResponse) {
                    boolean acknowledged = deleteIndexResponse.isAcknowledged();
                }
    
                @Override
                public void onFailure(Exception e) {
                    ElasticsearchException ee = (ElasticsearchException) e;
                    if (ee.status() == RestStatus.NOT_FOUND) {
                        System.out.println("indices not found");
                    }
                }
            });
    
        }
    
    }

    3, exists

    package com.wenbronk.elasticsearch.usage.index;
    
    import com.wenbronk.elasticsearch.usage.highLevel.RestHighLevelClientParent;
    import org.elasticsearch.action.ActionListener;
    import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
    
    public class Index_3_exists extends RestHighLevelClientParent {
    
        public void testExists() {
            GetIndexRequest request = new GetIndexRequest();
            request.indices("twitter");
    
            // 可选参数:
            request.local(false);
            request.humanReadable(true);
            request.includeDefaults(false);
    //        request.indicesOptions(indicesOptions);
    
            client.indices().existsAsync(request, new ActionListener<Boolean>() {
                @Override
                public void onResponse(Boolean aBoolean) {
                    System.out.println(aBoolean);
                }
    
                @Override
                public void onFailure(Exception e) {
    
                }
            });
        }
    }

    4, mapping

    package com.wenbronk.elasticsearch.usage.index;
    
    import com.wenbronk.elasticsearch.usage.highLevel.RestHighLevelClientParent;
    import org.elasticsearch.action.ActionListener;
    import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
    import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.common.xcontent.XContentType;
    
    public class Index_4_mapping extends RestHighLevelClientParent {
    
        public void testPutMapping() {
    
            PutMappingRequest request = new PutMappingRequest("twitter");
            request.type("tweet");
    
            request.source(
                    "{
    " +
                            "  "properties": {
    " +
                            "    "message": {
    " +
                            "      "type": "text"
    " +
                            "    }
    " +
                            "  }
    " +
                            "}",
                    XContentType.JSON);
    
            // 可选参数
            request.timeout(TimeValue.timeValueMinutes(2));
    //        request.timeout("2m");
            request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    //        request.masterNodeTimeout("1m");
    
            client.indices().putMappingAsync(request, new ActionListener<PutMappingResponse>() {
                @Override
                public void onResponse(PutMappingResponse putMappingResponse) {
                    boolean acknowledged = putMappingResponse.isAcknowledged();
                    System.out.println(acknowledged);
                }
    
                @Override
                public void onFailure(Exception e) {
    
                }
            });
        }
    
    }

    还是原DSL好用

  • 相关阅读:
    GDB调试汇编堆栈过程分析
    20145335郝昊 《信息安全系统设计基础》第11周学习总结
    20145335郝昊 《信息安全系统设计基础》第10周学习总结
    信息安全系统设计基础》实验五实验报告
    《信息安全系统设计基础》实验四实验报告
    《信息安全系统设计基础》实验二实验报告
    20145335郝昊 《信息安全系统设计基础》第9周学习总结
    20145335《信息安全系统设计基础》实验一实验报告
    20145335郝昊 《信息安全系统设计基础》期中总结
    20145334 《信息安全系统设计基础》第十二周学习总结
  • 原文地址:https://www.cnblogs.com/wenbronk/p/9395893.html
Copyright © 2011-2022 走看看