-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElasticStore.java
More file actions
51 lines (44 loc) · 1.67 KB
/
ElasticStore.java
File metadata and controls
51 lines (44 loc) · 1.67 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
package tech.thedumbdev.data;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import tech.thedumbdev.data.exceptions.ElasticStoreException;
import tech.thedumbdev.pojo.Log;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
public class ElasticStore implements DataStore {
private ElasticsearchClient client;
private long timestamp;
public ElasticStore(String hostUrl, String apiKey) throws ElasticStoreException {
this.client = ElasticsearchClient.of(b -> b.host(hostUrl).apiKey(apiKey));
this.timestamp = java.time.Instant.now().getEpochSecond();
try {
this.client.indices().create(c -> c.index(String.valueOf(this.timestamp)));
} catch (IOException | ElasticsearchException e) {
throw new ElasticStoreException("Unable to create index", e);
}
}
@Override
public void appendLog(Set<Log> logs) {
try {
for(Log log : logs) {
IndexResponse response = this.client.index(i -> i
.index(String.valueOf(this.timestamp))
.document(log)
);
}
} catch(ElasticsearchException | IOException e) {
throw new RuntimeException("Failed to write logs", e);
}
}
@Override
public void close() {
try {
this.client.close();
} catch (IOException e) {
throw new RuntimeException("", e);
}
}
}