import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequestBuilder;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by luodaijun on 2015-08-18.
* 说明:ES管理类
*/
public class ESManager {
private static final Logger logger = LoggerFactory.getLogger(ESManager.class);
/**
* ES客户端
*/
private TransportClient client;
public void init(String clusterNodes, Settings settings) throws IOException {
String[] clusterNodesArray = clusterNodes.split(",");
this.client = new PreBuiltTransportClient(settings);
for (String row : clusterNodesArray) {
String[] tmp = row.split(":");
String host = tmp[0].trim();
int port = Integer.valueOf(tmp[1].trim());
this.client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
}
}
public void insert(boolean async, String index, String type, List docs, BulkInsertCallback bulkInsertCallback) throws IOException {
if (docs == null || docs.isEmpty()) {
return;
}
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (Object doc : docs) {
byte[] jsonDoc = JacksonUtils.objectMapper.writeValueAsBytes(doc);
IndexRequestBuilder builder = client.prepareIndex(index, type).setSource(jsonDoc);
if (bulkInsertCallback != null) {
bulkInsertCallback.processRow(builder, doc);
}
bulkRequest.add(builder);
}
if (async) {
bulkRequest.execute(new ActionListener<BulkResponse>() {
@Override
public void onResponse(BulkResponse bulkResponse) {
printResponse(bulkResponse);
}
@Override
public void onFailure(Exception e) {
logger.error("es insert error", e);
}
});
} else {
BulkResponse bulkResponse = bulkRequest.get();
printResponse(bulkResponse);
}
}
private void printResponse(BulkResponse bulkResponse) {
if (bulkResponse.hasFailures()) {
BulkItemResponse[] bulkItemResponses = bulkResponse.getItems();
for (BulkItemResponse bulkItemResponse : bulkItemResponses) {
if (bulkItemResponse.isFailed()) {
logger.error("es insert error,index[{}] type[{}] cause:{}", bulkItemResponse.getIndex(), bulkItemResponse.getType(), bulkItemResponse.getFailureMessage());
}
}
}
}
/**
* 初始化Elastic Search模板
*/
private void initTemplate(String templateName) throws IOException {
try {
deleteTemplate(templateName);
} catch (Exception e) {
logger.error("delete template[" + templateName + "] error", e);
}
createTemplate(templateName);
}
/**
* 获取模板
*
* @param templateName
* @return
*/
private void deleteTemplate(String templateName) {
DeleteIndexTemplateRequestBuilder dtb = client.admin().indices().prepareDeleteTemplate(templateName);
DeleteIndexTemplateResponse resp = dtb.execute().actionGet();
if (resp.isAcknowledged()) {
logger.info("delete template : " + templateName);
} else {
throw new RuntimeException("failed to delete template" + templateName + ", please check the config.");
}
}
/**
* 创建模板
*
* @param templateName
* @throws IOException
*/
private void createTemplate(String templateName) throws IOException {
String json = new String(ClasspathResourceUtils.readByteArrayFromClasspath(templateName + ".json"), "UTF-8");
logger.info("template:" + json);
PutIndexTemplateRequestBuilder pitrb = client.admin().indices().preparePutTemplate(templateName);
pitrb.setSource(json);
PutIndexTemplateResponse resp = pitrb.execute().actionGet();
if (resp.isAcknowledged()) {
logger.info("create template : " + templateName);
} else {
throw new RuntimeException("failed to create template" + templateName + ", please check the config.");
}
}
/**
* 查询数据
*
* @param index
* @param type
* @param queryBuilder
* @return
*/
public List<Map<String, Object>> query(String index, String type, QueryBuilder queryBuilder) {
List<Map<String, Object>> result = new ArrayList<>();
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setQuery(queryBuilder)
.execute()
.actionGet();
SearchHit[] searchHists = response.getHits().getHits();
for (SearchHit hit : searchHists) {
result.add(hit.getSource());
}
return result;
}
public void close() {
client.close();
}
}
插入
esManager.insert(true, indexName, "metrics", dataEvent.getBody(), new BulkInsertCallback<Map>() {
@Override
public void processRow(IndexRequestBuilder builder, Map row) {
String rowKey = (String) row.get("rowKey");
builder.setRouting(rowKey);
}
});