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

//判断索引是否已经存在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. 谷歌又发钱了!给全员发1600美元,包括外包和实习生!还宣布将无限期居家办公!...
  2. [js] 说说防止重复发送ajax请求的方法有哪些?各自有什么优缺点?
  3. 以孩子兄弟链表为存储结构,请设计递归算法求树的高度
  4. C# 设置Word文档背景(纯色/渐变/图片背景)
  5. 2019胡润全球富豪榜发布:最有钱的华人还是他!
  6. 搭建Hadoop2.5.2+Eclipse开发调试环境
  7. web网页设计实例作业 ——校园文化(7页) html大学生网站开发实践作业
  8. JQuery视频总结
  9. 文本框、密码框、文本域
  10. iOS-Property follows Cocoa naming convention for returning ‘owned‘ objects
  11. 深澜校园网web认证自动登录脚
  12. 一文教你用 Neo4j 快速构建明星关系图谱
  13. CPU微指令相关概念
  14. cacheable注解原理_@Cacheable的实现原理
  15. tecplot常用笔记
  16. Android - 手机实现振动
  17. 浅说北仑有趣的地名之霞浦篇
  18. 雷军:人因梦想而伟大 金山骨子里重视技术尊重程序员
  19. dell 服务器面板显示屏,Dell服务器面板错误码详解
  20. Haystack:Facebook 的照片存储系统

热门文章

  1. VC中对象的序列化与文件I/O
  2. Ext3.2 beta版已发布
  3. 培训课程第三期签到和意见发表
  4. ZZULIOJ 1077: 空心菱形
  5. OJ1160: 矩阵的最大值(指针专题)(C语言)
  6. spring datasource oracle,spring中2种oracle数据源的配置
  7. 信息学奥赛一本通 1145:字符串p型编码 | OpenJudge NOI 1.7 31:字符串p型编码
  8. 信息学奥赛一本通(1055:判断闰年)
  9. C++语言基础 —— 顺序结构
  10. すぬけ君の地下鉄旅行 / Snuke's Subway Trip(AtCoder-2069)