ILM.java
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.viontech.match.runner;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.indexlifecycle.DeleteAction;
import org.elasticsearch.client.indexlifecycle.LifecycleAction;
import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
import org.elasticsearch.client.indexlifecycle.Phase;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.SetPriorityAction;
import org.elasticsearch.common.unit.TimeValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Author: inggg
* Date: 2022/05/26 16:53
*/
@Component
@Slf4j
public class ILM implements CommandLineRunner {
/**
* 生命周期名字
*/
public final static String LIFECYCLE_NAME = "delete-pool-after-days";
@Value("${delete.after.days:5}")
private Integer deleteAfterDays;
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public void run(String... args) throws Exception {
Map<String, Phase> phases = new HashMap<>();
Map<String, LifecycleAction> setPriorityAction = new HashMap<>();
setPriorityAction.put(SetPriorityAction.NAME, new SetPriorityAction(100));
phases.put("hot", new Phase("hot", TimeValue.ZERO, setPriorityAction));
Map<String, LifecycleAction> deleteActions =
Collections.singletonMap(DeleteAction.NAME, new DeleteAction());
phases.put("delete", new Phase("delete",
new TimeValue(deleteAfterDays, TimeUnit.DAYS), deleteActions));
LifecyclePolicy policy = new LifecyclePolicy(LIFECYCLE_NAME, phases);
PutLifecyclePolicyRequest lifecyclePolicyRequest = new PutLifecyclePolicyRequest(policy);
AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indexLifecycle().putLifecyclePolicy(lifecyclePolicyRequest, RequestOptions.DEFAULT);
if (acknowledgedResponse.isAcknowledged()) {
log.info(LIFECYCLE_NAME + "生命周期创建完成");
}
}
}