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

//判断索引是否已经存在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. idel 智能提示_intellij idea设置代码自动提示快捷键的详细方法.
  2. 快速搭建RHEL5.9本地yum源
  3. xcode新版本single view_动态数组函数系列1|概况-跟以往Excel版本完全不一样玩法的函数...
  4. 今天的快乐从何而来的飞鸽传书
  5. C# 动态语言扩展(学习笔记)
  6. 【流媒體】live555—VS2010 下live555编译、使用及测试
  7. python从入门到放弃百度云-Python从入门到放弃:概论
  8. 18年12月英语六级第一套听力单词
  9. Ubuntu 12.04 安装离线词典
  10. 两向量点乘坐标运算_向量点乘(内积)和叉乘(外积、向量积)概念及几何意义解读...
  11. H3C设备通过oid获取光衰
  12. 学硕上几年学计算机,研究生一般要读几年毕业
  13. 网页图片上传到服务器
  14. js取小数点后两位 方法总结
  15. yum一次性下载安装包及其依赖包
  16. oracle通过UTL_SMTP包发送邮件
  17. Android之SeekBar(0在中间)
  18. DOCKER笔记(1)
  19. ic启动器我的世界_hmcl启动器下载
  20. c++ Socket 通过域名进行网络连接

热门文章

  1. SQLSERVER压缩数据文件的用处有多大
  2. 行动 习惯 性格 命运
  3. 连接pgsql_Laravel 数据库连接配置和读写分离
  4. 信息学奥赛一本通 1146:判断字符串是否为回文 | OpenJudge NOI 1.7 33:判断字符串是否为回文
  5. 信息学奥赛C++语言:质数
  6. 信息学奥赛C++语言:输出判断
  7. 18 SD配置-主数据-定义公用分销渠道
  8. 20 FI配置-财务会计-定义税务科目
  9. 视图与表之间的异同点_视图和表的区别和联系
  10. 学习笔记4 :opencv 、PIL、matplotlib.image打开、保存图片