700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > kibana创建es索引_java操作es动态创建索引(按月生成) 索引类型 索引别名

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

时间:2024-03-02 21:33:12

相关推荐

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

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。