gpt4 book ai didi

java - Elasticsearch - 从 json 文件加载 ILM 策略

转载 作者:行者123 更新时间:2023-12-02 04:22:27 25 4
gpt4 key购买 nike

我已经通过 JAVA api 实现了 ILM 策略。我的代码看起来像这样:

        Map<String, Phase> phases = new HashMap<>();

Map<String, LifecycleAction> hotActions = new HashMap<>();
hotActions.put(RolloverAction.NAME,
new RolloverAction(new ByteSizeValue(30, ByteSizeUnit.GB),
new TimeValue(maxDaysPerIndex, TimeUnit.DAYS),
maxRecordsPerIndex));
phases.put("hot", new Phase("hot", TimeValue.ZERO, hotActions));

Map<String, LifecycleAction> warmActions = new HashMap<>();
warmActions.put(ForceMergeAction.NAME, new ForceMergeAction(1));
phases.put("warm", new Phase("warm", TimeValue.ZERO, warmActions));

Map<String, LifecycleAction> deleteActions = Collections.singletonMap(DeleteAction.NAME, new DeleteAction());
phases.put("delete", new Phase("delete", new TimeValue(retentionPeriodDays, TimeUnit.DAYS), deleteActions));

LifecyclePolicy policy = new LifecyclePolicy("my_policy", phases);

restHighLevelClient.indexLifecycle()
.putLifecyclePolicy(new PutLifecyclePolicyRequest(policy), RequestOptions.DEFAULT);

现在,该策略的 JSON 格式如下所示:

"my_policy" : {
"version" : 3,
"modified_date" : "2019-06-17T08:33:08.356Z",
"policy" : {
"phases" : {
"warm" : {
"min_age" : "0ms",
"actions" : {
"forcemerge" : {
"max_num_segments" : 1
}
}
},
"hot" : {
"min_age" : "0ms",
"actions" : {
"rollover" : {
"max_size" : "30gb",
"max_age" : "1d",
"max_docs" : 5000000
}
}
},
"delete" : {
"min_age" : "29d",
"actions" : {
"delete" : { }
}
}
}
}

}

现在,我不想使用 java api 创建策略,而是想创建一个 json 文件,并将其作为策略加载,这样它看起来像这样:

JsonFILE jsonFile = loadJsonFromFile("fileName");
LifecyclePolicy policy = new LifecyclePolicy("my_policy", jsonFile);
restHighLevelClient.indexLifecycle().putLifecyclePolicy(new
PutLifecyclePolicyRequest(policy), RequestOptions.DEFAULT);

底线 - 有没有办法通过 JAVA 加载一个用于配置 ILM 策略的 json 文件?

最佳答案

您可以使用低级别客户端而不是高级客户端直接request ES REST APIs :

RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http")).build();

String source = new String(Files.readAllBytes(
new File("path/to/file.json", StandardCharsets.UTF_8);

Request request = new Request("PUT", "_ilm/policy/name");
request.setJsonEntity(source);

restClient.performRequest(request);

关于java - Elasticsearch - 从 json 文件加载 ILM 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com