use of io.searchbox.indices.IndicesExists in project jmxtrans by jmxtrans.
the class ElasticWriter method createMappingIfNeeded.
private static void createMappingIfNeeded(JestClient jestClient, String indexName, String typeName) throws ElasticWriterException, IOException {
synchronized (CREATE_MAPPING_LOCK) {
IndicesExists indicesExists = new IndicesExists.Builder(indexName).build();
boolean indexExists = jestClient.execute(indicesExists).isSucceeded();
if (!indexExists) {
CreateIndex createIndex = new CreateIndex.Builder(indexName).build();
jestClient.execute(createIndex);
URL url = ElasticWriter.class.getResource("/elastic-mapping.json");
String mapping = Resources.toString(url, Charsets.UTF_8);
PutMapping putMapping = new PutMapping.Builder(indexName, typeName, mapping).build();
JestResult result = jestClient.execute(putMapping);
if (!result.isSucceeded()) {
throw new ElasticWriterException(String.format("Failed to create mapping: %s", result.getErrorMessage()));
} else {
log.info("Created mapping for index {}", indexName);
}
}
}
}
Aggregations