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

//判断索引是否已经存在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);

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

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

  1. python创建类的实例方法-Python中动态创建类实例的方法

    简介 在Java中我们可以通过反射来根据类名创建类实例,那么在Python我们怎么实现类似功能呢? 其实在Python有一个builtin函数import,我们可以使用这个函数来在运行时动态加载一些模 ...

  2. python动态创建类_Python中通过参数动态创建扩展类(class)

    class Bar: def super_cool_function(self): print("Cool") 1.利用Python闭包动态扩展类 通过在内部创建并从函数返回它来动 ...

  3. es java_JAVA API操作ES详解

    一:运行环境 JDK:1.8 ES:5.6.4 二:JAVA依赖环境 elasticsearch.jar  5.6.4版本: xsi:schemaLocation="http://maven ...

  4. 【ES】CURL 操作 ES命令集合

    1. 概述 主要是有时候es没有界面,我就想测试,那么只能用curl去请求. 1.1 _cat系列 cat系列提供了一系列查询elasticsearch集群状态的接口.你可以通过执行 curl -XG ...

  5. phpexcel_cell 获取表格样式_Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行...

    精品推荐 国内稀缺优秀Java全栈课程-Vue+SpringBoot通讯录系统全新发布! Docker快速手上视频教程(无废话版)[免费] 作者:E-iceblue https://www.cnblo ...

  6. python更新es数据_python操作es增删改查

    1.查询(search) # 获取案例库信息 @app.route('/get_dcn_cases', methods=['GET', 'POST']) def get_dcn_cases(): # ...

  7. java创建读取文件_Java实现文件的创建、读取、写入操作-Fun言

    在日常的开发中,对文件的操作经常会有,所以今天教大家其中一种使用Java实现文件的创建.读取.写入操作 创建文件String filenameTemp = "D:\demo.txt" ...

  8. 使用jstree创建无限分级的树(ajax动态创建子节点)

    首先来看一下效果 页面加载之初 节点全部展开后 首先数据库的表结构如下 其中Id为主键,PId为关联到自身的外键 两个字段均为GUID形式 层级关系主要靠这两个字段维护 其次需要有一个类型 publi ...

  9. java 调用存储过程 无效的列索引_JAVA 调用存储过程报错 java.sql.SQLException: 无效的列索引...

    报错信息java.sql.SQLException:无效的列索引atoracle.jdbc.driver.OracleCallableStatement.registerOutParameterInt ...

最新文章

  1. 自己服务器上部署APP应用(安卓和IOS版),下载页面的代码,以及IOS的xxx.plist文件的建立
  2. Eclipse实现hibernate反向工程:从数据库逆向生成实体类和hbm文件
  3. Linux共享文件夹中毒,Linux find命名快速查找中毒文件操作实例
  4. 粉丝给我发色情app,我反手对色情app渗透,我居然发现了 ....
  5. SAP Spartacus CMS Component的lazy loading懒加载方式
  6. PYTHON__ ITERTOOLS模块
  7. MySQL 5.7.18的安装与主从复制
  8. mplab 语法错误不报错_Python怎么解决报错?
  9. 蓝桥杯 ADV-201 算法提高 我们的征途是星辰大海
  10. php mysql 1040_php – 如何修复消息:SQLSTATE [08004] [1040]连接太多
  11. [原创]Ladon7.5大型内网渗透扫描器Cobalt Strike
  12. android切图规范,APP切图详细规范终极指南
  13. Vo Mic|旧手机不要扔,一招秒变无线麦克风
  14. Python数据类型函数
  15. luogu4061 大吉大利,晚上吃鸡!
  16. java发送电子邮件
  17. Bluetooth 蓝牙介绍(五):低功耗蓝牙BLE Security
  18. python里的jh是啥意思_JH是什么意思啊
  19. 人所共有的19个不良习惯
  20. java-php-net-python-校园OTO超市系统ssm-视频计算机毕业设计程序

热门文章

  1. 日文邮件变成乱码解决方案
  2. SharePoint Enterprise Search基础知识点拾遗系列之二
  3. Kingsoft AntiVirus(金山毒霸) and av-comparatives organization
  4. ZZULIOJ 1071:分解质因子
  5. php 输出数组函数调用函数,php – 无法在数组中调用函数
  6. 信息学奥赛一本通 1099:第n小的质数 | OpenJudge NOI 1.5 44
  7. 信息学奥赛一本通 1075:药房管理 | OpenJudge NOI 1.5 23:药房管理
  8. OpenJudge NOI 1.5 16:买房子
  9. 51 MM配置-库存管理和实际库存-设置“交货完成”标识
  10. python os库使用