public class EsAdmin {
private TransportClient client;
public EsAdmin(TransportClient client) {
this.client = client;
}
@Override
public void createIndex(String index, String type, Map<String, String> mappings) throws IOException {
IndicesAdminClient adminClient = client.admin().indices();
boolean indexExists = adminClient.exists(new IndicesExistsRequest(index)).actionGet().isExists();
if (!indexExists) {
String result = adminClient.create(new CreateIndexRequest(index)).actionGet().index();
if (StringUtils.isEmpty(result)) {
throw new ProcessException("createIndex failed: " + index);
}
}
boolean typeExists = adminClient.typesExists(new TypesExistsRequest(new String[]{index}, new String[]{type})).actionGet().isExists();
if (!typeExists) {
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties");
for (Map.Entry<String, String> entry : mappings.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
xContentBuilder.startObject(key)
.field("type", value);
if ("text".equalsIgnoreCase(value)) {
xContentBuilder.startObject("fields")
.startObject("keyword")
.field("type", "keyword")
.endObject()
.endObject();
}
xContentBuilder.endObject();
}
xContentBuilder.endObject()
.endObject();
String json = Strings.toString(xContentBuilder);
PutMappingRequest mapping = Requests.putMappingRequest(index).type(type).source(xContentBuilder);
boolean acknowledged = adminClient.putMapping(mapping).actionGet().isAcknowledged();
if (!acknowledged) {
throw new ProcessException(String.format("createType failed: index=%s, type=%s, mappings=%s", index, type, json));
}
}
}
@Override
public void deleteIndex(String index) {
IndicesAdminClient adminClient = client.admin().indices();
boolean acknowledged = adminClient.delete(new DeleteIndexRequest(index)).actionGet().isAcknowledged();
if (!acknowledged) {
throw new ProcessException(String.format("deleteIndex failed: index=%s", index));
}
}
@Override
public long count(String index, String type) {
SearchResponse searchResponse = client.prepareSearch(index).setTypes(type)
.addAggregation(AggregationBuilders.count("count").field("_id"))
.get();
long count = ((InternalValueCount) (searchResponse.getAggregations().get("count"))).getValue();
return count;
}
@Override
public List<EsData> exportData(String index, String type) {
int from = 0;
int unit = 1000;
List<SearchHit> list = new ArrayList<>();
List<SearchHit> tmpList;
do {
SearchHits hits = client.prepareSearch(index).setTypes(type)
.setQuery(QueryBuilders.matchAllQuery())
.setFrom(from).setSize(unit)
.get().getHits();
tmpList = Arrays.asList(hits.getHits());
list.addAll(tmpList);
from += hits.getTotalHits() + 1;
} while (!tmpList.isEmpty());
List<EsData> export = list.stream().map(e -> {
return new EsData(e.getId(), e.getSourceAsMap());
}).collect(Collectors.toList());
return export;
}
@Override
public void importData(String index, String type, List<EsData> data) {
for (EsData esData : data) {
IndexResponse response = client.prepareIndex(index, type)
.setId(esData.getId())
.setSource(esData.getData())
.get();
if (!(RestStatus.CREATED.equals(response.status()) || RestStatus.OK.equals(response.status()))) {
throw new EsException();
}
}
}
}
public class EsData implements Serializable {
private String id;
private Map<String, Object> data = new HashMap<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Map<String, Object> getData() {
return data;
}
public void setData(Map<String, Object> data) {
this.data = data;
}
public EsData() {
}
public EsData(String id, Map<String, Object> data) {
this.id = id;
this.data = data;
}
}