kibana创建es索引_java操作es动态创建索引(按月生成),索引类型,索引别名

这篇博客介绍了如何使用Java客户端检查Elasticsearch索引是否存在,若不存在则动态创建,并按月份生成索引。同时,文章还涉及到设置索引别名的操作,为数据插入提供指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:判断索引是否存在:

//判断索引是否已经存在String indexName = Constans.ES_INDEX_TIME+"_"+DateUtils.getDateStrSub(new Date());Client client = elasticsearchTemplate.getClient();//返回值是布尔类型,判断方法是client对象下indices()方法的exists方法,在这个方法里有索引的名字;boolean exists = client        .admin()        .indices()        .exists(new IndicesExistsRequest()        .indices(new String[]{indexName}))        .actionGet().isExists();

第二步:如果不存在则创建索引:

if(!exists){    initEsIndex(indexName,client);}
/** * @author 程序员思思 * @describe 动态创建索引,类型,别名 * @date 2021/1/22 * @param * @return **/private void initEsIndex(String indexName,Client client){    try {         //副本、分片         Settings.Builder settings = Settings.builder()         .put("number_of_replicas","0")         .put("number_of_shards","5");         //mapping        String mappingStr = "{" +                "    "taxTaskTimeLog": {" +                "      "properties": {" +                "        "areaCode": {" +                "          "type": "keyword"" +                "        }," +                "        "areaCodeName": {" +                "          "type": "text"," +                "          "fields": {" +                "            "keyword": {" +                "              "type": "keyword"," +                "              "ignore_above": 256" +                "            }" +                "          }" +                "        }," +                "        "clientGuid": {" +                "          "type": "text"" +                "        }," +                "        "clientIp": {" +                "          "type": "text"" +                "        }," +                "        "createTime": {" +                "          "type": "date"," +                "          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm||yyyy-MM-dd||epoch_millis"" +                "        }," +                "        "deviceId": {" +                "          "type": "text"" +                "        }," +                "        "errorMsg": {" +                "          "type": "text"," +                "          "fields": {" +                "            "keyword": {" +                "              "type": "keyword"," +                "              "ignore_above": 256" +                "            }" +                "          }" +                "        }," +                "        "isCloud": {" +                "          "type": "boolean"" +                "        }," +                "        "isSuccess": {" +                "          "type": "boolean"" +                "        }," +                "        "loginType": {" +                "          "type": "keyword"" +                "        }," +                "        "messageId": {" +                "          "type": "text"" +                "        }," +                "        "runTime": {" +                "          "type": "long"" +                "        }," +                "        "serialNo": {" +                "          "type": "keyword"" +                "        }," +                "        "source": {" +                "          "type": "text"," +                "          "fields": {" +                "            "keyword": {" +                "              "type": "keyword"," +                "              "ignore_above": 256" +                "            }" +                "          }" +                "        }," +                "        "taskId": {" +                "          "type": "keyword"" +                "        }," +                "        "taskNode": {" +                "          "type": "keyword"" +                "        }," +                "        "taskNodeEndTime": {" +                "          "type": "date"," +                "          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm||yyyy-MM-dd||epoch_millis"" +                "        }," +                "        "taskNodeName": {" +                "          "type": "text"," +                "          "fields": {" +                "            "keyword": {" +                "              "type": "keyword"," +                "              "ignore_above": 256" +                "            }" +                "          }" +                "        }," +                "        "taskNodeStartTime": {" +                "          "type": "date"," +                "          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm||yyyy-MM-dd||epoch_millis"" +                "        }," +                "        "taskType": {" +                "          "type": "keyword"" +                "        }," +                "        "taskTypeName": {" +                "          "type": "text"," +                "          "fields": {" +                "            "keyword": {" +                "              "type": "keyword"," +                "              "ignore_above": 256" +                "            }" +                "          }" +                "        }," +                "        "taxType": {" +                "          "type": "keyword"" +                "        }," +                "        "taxTypeName": {" +                "          "type": "text"," +                "          "fields": {" +                "            "keyword": {" +                "              "type": "keyword"," +                "              "ignore_above": 256" +                "            }" +                "          }" +                "        }" +                "      }" +                "    }" +                "  }";        //转成map        HashMap hashMap = ToolUtils.convertJson(mappingStr);        //创建索引,type,别名        client.admin().indices().prepareCreate(indexName).setSettings(settings).execute().actionGet();        //创建type        client.admin().indices().preparePutMapping(indexName).setType(Constans.ES_TYPE_TIME).setSource(hashMap).execute().actionGet();        //创建别名        client.admin().indices().prepareAliases().addAlias(indexName,Constans.ES_INDEX_TIME).execute().actionGet();    } catch (Exception e) {        logger.error(e.getMessage());    }}
/** * @author 程序员思思 * @describe 字符串转map * @date 2021/1/22  * @param str * @return HashMap **/public static HashMap convertJson(String str) {    String result = StringEscapeUtils.unescapeJava(str);    result = result.replace(""{", "{").replace("}"", "}").replace("", "");    return JSON.parseObject(result, HashMap.class);}

第三步:插入数据:

IndexRequest indexRequest = new IndexRequest()        .index(indexName)        .type(Constans.ES_TYPE_TIME)        .versionType(VersionType.EXTERNAL)        .version(1)        .id(rabbitMessage.getMessageId())        .source(source);client.index(indexRequest);
592b564b0d4f5dd792c2b9dd0f6b0a0f.png
a9a0530521224b042dbd18854d5c6959.png
c08fa699d9e9ea4e4dfdefc2c908b2f5.png

结束,欢迎大家指导学习!共同进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值