Elasticsearch - 从 json 文件加载 ILM 策略



我已经通过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 文件(进行加载?

您可以使用低级客户端而不是高级客户端直接请求 ES REST API:

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);

最新更新