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;
public class Es6ToEs8Mapping {
public static void main(String[] args) {
File folder = new File("C:\\Users\\wyc\\Desktop\\小卫星\\集群维护\\es_mapping");
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);
Object jsonObject = JSON.parse(content, Feature.OrderedField);
deleteThirdLevelKeys(jsonObject, 1);
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;
public class Es8BatchLifeAlias {
public static void main(String[] args) {
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"));
for (File file : files) {
try {
Path path = Paths.get(file.getAbsolutePath());
byte[] bytes = Files.readAllBytes(path);
String content = new String(bytes);
Object jsonObject = JSON.parse(content, Feature.OrderedField);
JSONObject newJson = modifyAlias(jsonObject);
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;
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);
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;
public class Es8BatchTemplate {
public static void main(String[] args) {
File folder = new File("C:\\Users\\wyc\\Desktop\\小卫星\\集群维护\\es_mapping");
File[] files = folder.listFiles((dir, name) -> name.endsWith(".json") && name.startsWith("m"));
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("."));
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);
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);
newJson.put("settings", settings);
newJson.put("aliases", aliases);
newJson.put("mappings", dynamicMappings);
return newJson;
}
}