功能
ES8导出的mapping批量修改索引名,便于冷热生命周期的配置
package com.test;
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;
/**
* 将ES8的索引批量新增后缀名
* 去除第三层的type
*/
public class Es8ModifyName {
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);
JSONObject newJson = modifyName(jsonObject);
// 将修改后的 JSON 写回原文件
String jsonString = JSON.toJSONString(newJson, SerializerFeature.PrettyFormat);
Files.write(path, jsonString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static JSONObject modifyName(Object json) {
JSONObject newJson = new JSONObject();
JSONObject jsonObject = (JSONObject) json;
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
newJson.put(key + "-002024", value);
}
return newJson;
}
}