forked from opensearch-project/opensearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointInTime.java
81 lines (69 loc) · 3.78 KB
/
PointInTime.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.client.samples;
import java.util.Collections;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.opensearch._types.mapping.IntegerNumberProperty;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch._types.mapping.TypeMapping;
import org.opensearch.client.opensearch.core.pit.CreatePitRequest;
import org.opensearch.client.opensearch.core.pit.CreatePitResponse;
import org.opensearch.client.opensearch.core.pit.DeletePitRequest;
import org.opensearch.client.opensearch.core.pit.DeletePitResponse;
import org.opensearch.client.opensearch.core.pit.ListAllPitResponse;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.DeleteIndexRequest;
import org.opensearch.client.opensearch.indices.IndexSettings;
/**
* Run with: <c>./gradlew :samples:run -Dsamples.mainClass=PointInTime</c>
*/
public class PointInTime {
private static final Logger LOGGER = LogManager.getLogger(PointInTime.class);
public static void main(String[] args) {
try {
var client = SampleClient.create();
var version = client.info().version();
LOGGER.info("Server: {}@{}", version.distribution(), version.number());
final var indexName = "my-index";
if (!client.indices().exists(r -> r.index(indexName)).value()) {
LOGGER.info("Creating index {}", indexName);
IndexSettings settings = new IndexSettings.Builder()
.numberOfShards("2")
.numberOfReplicas("1")
.build();
TypeMapping mapping = new TypeMapping.Builder()
.properties("age", new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build())
.build();
CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder()
.index(indexName)
.settings(settings)
.mappings(mapping)
.build();
client.indices().create(createIndexRequest);
}
CreatePitRequest createPitRequest = new CreatePitRequest.Builder()
.targetIndexes(Collections.singletonList(indexName))
.keepAlive(new Time.Builder().time("100m").build()).build();
CreatePitResponse createPitResponse = client.createPit(createPitRequest);
LOGGER.info("PIT created with id: {}", createPitResponse.pitId());
ListAllPitResponse listAllPitResponse = client.listAllPit();
LOGGER.info("Found {} PITs", listAllPitResponse.pits().size());
DeletePitRequest deletePitRequest = new DeletePitRequest.Builder()
.pitId(Collections.singletonList(createPitResponse.pitId())).build();
DeletePitResponse deletePitResponse = client.deletePit(deletePitRequest);
LOGGER.info("Deleting PIT: {}", deletePitResponse.pits());
LOGGER.info("Deleting index {}", indexName);
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(indexName).build();
client.indices().delete(deleteIndexRequest);
} catch (Exception e) {
LOGGER.error("Unexpected exception", e);
}
}
}