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

//判断索引是否已经存在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. 赠票 | 第三届语言与智能高峰论坛
  2. java基础---关于比较器Comparator和Comparable
  3. yum安装mysql5.6
  4. 穿越障碍物JAVA编程_JAVA 基础编程练习题1 【程序 1 不死神兔】
  5. Solve error: Cannot open include file: 'X11/Xlocale.h': No such file or directory
  6. Android TV开发总结(二)构建一个TV Metro界面(仿泰捷视频TV版)
  7. 《Linux操作系统-系统移植》第4章 内核移植(IMX283+Linux3.14)
  8. FireMonkey Control的Paint顺序
  9. Android 自定义CheckBoxPreference的CheckBox复选框
  10. 内温的整体优先效应实验_认知
  11. Verilog 参数化位宽转换设计实例
  12. wp8 android,WP8桌面主题|WP8桌面v1.5.2 for Android版 - 玩客网
  13. Ubutu 12.04LTS 安装搜狗拼音输入法+搜狗皮肤 步骤详解
  14. 【网页制作课作业】用HTML+CSS制作一个简单的学校网页(9页)
  15. echarts中中国地图飞机路线使用
  16. TMS320F280049 I2C IIC 相对于库函数操作CAT24C02 中文
  17. DNSlog注入思路
  18. FileLock——Java文件锁
  19. 学计算机需要自控力,上班族,自控力差,怎样才能静下心好好学习?
  20. [OpenCV]关于opencv不能打开某些视频得问题

热门文章

  1. 班扎古鲁白玛的沉默(见与不见)
  2. sql怎样删除重复值
  3. pl sql mysql 版本_pl sql developer连oracle哪个版本的数据库都可以吗
  4. matlab仿真三相交流电路,三相交流调压电路的MATLAB仿真
  5. 信息学奥赛一本通 1090:含k个3的数 | OpenJudge NOI 1.5 30
  6. 平衡的阵容(洛谷-P2880)
  7. 训练日志 2018.12.16
  8. 43 FI配置-财务会计-固定资产-一般评估-定义折旧范围
  9. 科研诚信与学术规范_大学教授学术造假,违背科研诚信
  10. opencv 扩大区域_python-----opencv图像边界扩充