Elasticsearch-从json文件加载ILM策略

时间:2019-06-17 09:13:39

标签: java elasticsearch

我已经通过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策略?

1 个答案:

答案 0 :(得分:0)

您可以使用低级客户端(而不是高级客户端)来直接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);