JAVA编写ES各类脚本完成索引、模板构建

文章讲述了如何将ES6使用EsDump导出的Mapping文件转换为ES8格式,包括删除第三层的`type`,在ES8Mapping中添加索引名、生命周期配置以及动态别名,同时从ES6Mapping生成对应的生命周期模板。
摘要由CSDN通过智能技术生成

ES6使用esdump导出的mapping结构转为ES8的结构

  • 输入:ES6使用EsDump导出的的Mapping文件
  • 输出:ES8的http建表Mapping文件,源文件会被覆盖!
  • 处理:去除第三层的type
package com.es;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 输入:ES6使用EsDump导出的的Mapping文件
 * 输出:ES8的http建表Mapping文件,源文件会被覆盖!
 * 处理:去除第三层的type
 */
public class Es6ToEs8Mapping {
    public static void main(String[] args) {
        // 获取当前文件夹下所有的 .json 文件
        File folder = new File("C:\\Users\\wyc\\Desktop\\小卫星\\集群维护\\es_mapping");
//        File[] files = folder.listFiles((dir, name) -> name.endsWith(".json")&&name.startsWith("m"));
        File[] files = folder.listFiles((dir, name) -> name.endsWith(".json"));
        // 遍历每个文件
        for (File file : files) {
            try {
                // 读取文件内容
                Path path = Paths.get(file.getAbsolutePath());
                byte[] bytes = Files.readAllBytes(path);
                String content = new String(bytes);
                // 解析 JSON 数据
                Object jsonObject = JSON.parse(content, Feature.OrderedField);
                // 删除第三层的键
                deleteThirdLevelKeys(jsonObject, 1);
                // 将修改后的 JSON 写回原文件
                String jsonString = JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat);
                Files.write(path, jsonString.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void deleteThirdLevelKeys(Object json, int level) {
        if (json instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) json;
            for (String key : jsonObject.keySet()) {
                Object value = jsonObject.get(key);
                if (value instanceof JSONObject) {
                    if (level == 2) {
                        JSONObject jsonObject2 = (JSONObject) value;
                        JSONObject tmp = (JSONObject)jsonObject2.get(jsonObject2.keySet().iterator().next());
                        jsonObject.put(key, tmp);
                    } else {
                        deleteThirdLevelKeys(value, level + 1);
                    }
                } else if (value instanceof JSONArray) {
                    JSONArray jsonArray = (JSONArray) value;
                    for (int i = 0; i < jsonArray.size(); i++) {
                        deleteThirdLevelKeys(jsonArray.get(i), level);
                    }
                }
            }
        } else if (json instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) json;
            for (int i = 0; i < jsonArray.size(); i++) {
                deleteThirdLevelKeys(jsonArray.get(i), level);
            }
        }
    }
}

ES8导出的mapping批量修改索引名

  • 输入:ES8的http建表Mapping文件
  • 输出:ES8的http建表Mapping文件+索引、生命周期配置,源文件会被覆盖!
  • 处理:依据文件名动态增加别名配置
package com.es;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @Author yichuan@iscas.ac.cn
 * @Date 2024/4/26 9:27
 * 输入:ES8的http建表Mapping文件
 * 输出:ES8的http建表Mapping文件+索引、生命周期配置,源文件会被覆盖!
 * 处理:依据文件名动态增加别名配置
 */
public class Es8BatchLifeAlias {
    public static void main(String[] args) {
        // 获取当前文件夹下所有的 .json 文件
        File folder = new File("C:\\Users\\wyc\\Desktop\\小卫星\\集群维护\\es8_mapping_life\\es8_mapping_life");
        File[] files = folder.listFiles((dir, name) -> name.endsWith(".json") && name.startsWith("m"));
//        File[] files = folder.listFiles((dir, name) -> name.endsWith(".json"));
        // 遍历每个文件
        for (File file : files) {
            try {
                // 读取文件内容
                Path path = Paths.get(file.getAbsolutePath());
                byte[] bytes = Files.readAllBytes(path);
                String content = new String(bytes);
                // 解析 JSON 数据
                Object jsonObject = JSON.parse(content, Feature.OrderedField);
                JSONObject newJson = modifyAlias(jsonObject);
                // 将修改后的 JSON 写回原文件
                String jsonString = JSON.toJSONString(newJson, SerializerFeature.PrettyFormat);
                Files.write(path, jsonString.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

        private static JSONObject modifyAlias(Object json) {
            JSONObject jsonObject = (JSONObject) json;
            // 创建新的 JSON 对象
            JSONObject newJson = new JSONObject();
            // 根据原始值动态生成别名
            String originalAlias = jsonObject.keySet().iterator().next(); // 获取原始值
            String dynamicAlias = originalAlias.substring(0,originalAlias.indexOf("-"));
            // 添加别名和设置部分
            JSONObject aliases = new JSONObject();
            JSONObject aliasDetails = new JSONObject();
            aliasDetails.put("is_write_index", true);
            aliases.put(dynamicAlias, aliasDetails); // 使用动态生成的别名
            newJson.put("aliases", aliases);
            JSONObject settings = new JSONObject();
            settings.put("index.lifecycle.name", "iscas-ilm-policy");
            settings.put("index.lifecycle.rollover_alias", dynamicAlias); // 使用动态生成的别名
            newJson.put("settings", settings);
            // 添加映射部分--es8 PUT创建的版本
            JSONObject tmp = (JSONObject) jsonObject.get(originalAlias);
            newJson.put("mappings", tmp.get("mappings"));
            return newJson;
        }
}

ES导出的Mapping转对应含生命周期模板

  • 输入:ES6的http建表Mapping文件
  • 输出:ES8的http建模板Template文件加生命周期配置,源文件会被覆盖!
  • 处理:依据文件名动态增加模板生命周期别名配置,根据文件内容读取mapping到模板
package com.es;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @Author yichuan@iscas.ac.cn
 * @Date 2024/4/27 10:35
 * 输入:ES6的http建表Mapping文件
 * 输出:ES8的http建模板Template文件加生命周期配置,源文件会被覆盖!
 * 处理:依据文件名动态增加模板生命周期别名配置,根据文件内容读取mapping到模板
 */
public class Es8BatchTemplate {
    public static void main(String[] args) {
        // 获取当前文件夹下所有的 .json 文件
        File folder = new File("C:\\Users\\wyc\\Desktop\\小卫星\\集群维护\\es_mapping");
        File[] files = folder.listFiles((dir, name) -> name.endsWith(".json") && name.startsWith("m"));
//        File[] files = folder.listFiles((dir, name) -> name.endsWith(".json"));
        // 遍历每个文件
        for (File file : files) {
            try {
                // 读取文件内容
                Path path = Paths.get(file.getAbsolutePath());
                byte[] bytes = Files.readAllBytes(path);
                String content = new String(bytes);
                String indexName = file.getName().substring(0, file.getName().lastIndexOf("."));
                // 解析 JSON 数据
                Object jsonObject = JSON.parse(content, Feature.OrderedField);
                JSONObject dynamicMappings = (JSONObject) ((JSONObject) ((JSONObject)((JSONObject)jsonObject).get(indexName)).get("mappings")).get(indexName);
                JSONObject newJson = modifyName(jsonObject, indexName, dynamicMappings);
                // 将修改后的 JSON 写回原文件
                String jsonString = JSON.toJSONString(newJson, SerializerFeature.PrettyFormat);
                Files.write(path, jsonString.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static JSONObject modifyName(Object json, String dynamicIndex, JSONObject dynamicMappings) {
        JSONObject newJson = new JSONObject();
        newJson.put("order", 0);
        JSONArray patternsArray = new JSONArray();
        patternsArray.add(dynamicIndex + "-*");
        newJson.put("index_patterns",patternsArray);
        JSONObject settings = new JSONObject();
        JSONObject index = new JSONObject();
        JSONObject lifecycle = new JSONObject();
        lifecycle.put("name", "iscas-ilm-policy");
        lifecycle.put("rollover_alias", dynamicIndex);
        index.put("lifecycle", lifecycle);
        JSONObject routing = new JSONObject();
        JSONObject allocation = new JSONObject();
        JSONObject require = new JSONObject();
        require.put("hotwarm_type", "hot");
        allocation.put("require", require);
        routing.put("allocation", allocation);
        index.put("routing", routing);
        index.put("number_of_shards", "6");
        index.put("number_of_replicas", "1");
        JSONObject translog = new JSONObject();
        translog.put("durability", "async");
        translog.put("sync_interval", "30s");
        translog.put("flush_threshold_size", "1024mb");
        index.put("translog", translog);
        settings.put("index", index);
        JSONObject aliases = new JSONObject();
        JSONObject aliasProperties = new JSONObject();
        aliasProperties.put("is_write_index", true);
        aliases.put(dynamicIndex, aliasProperties);
        // 将构建的 JSON 结构放入新的 JSON 对象中
        newJson.put("settings", settings);
        newJson.put("aliases", aliases);
        newJson.put("mappings", dynamicMappings);
        return newJson;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值