@Slf4j
@Component
public class EsClientUtil {
private static EsClientUtil instance;
private static String esAddress;
public static String esAddressNoStatic;
private static RestHighLevelClient client = null;
private static RestClientBuilder restClientBuilder;
private EsClientUtil() {
esAddress = esAddressNoStatic;
}
public synchronized static EsClientUtil getInstance() {
if (instance == null) {
instance = new EsClientUtil();
}
return instance;
}
/**
* 获取es客户端连接
*/
public synchronized static RestHighLevelClient getClient() throws BusinessException {
if(client == null){
if (StringUtils.isEmpty(esAddress)) {
throw new BusinessException(ModelCatalogCenterErrorCode.ERROR_PARAM, "es ip is null");
}
ArrayList<HttpHost> hostList = new ArrayList<>();
Arrays.stream(esAddress.split(",")).forEach(host -> getHostList(host, hostList));
restClientBuilder = RestClient.builder(hostList.toArray(new HttpHost[0]));
restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis());
httpClientBuilder.disableAuthCaching();
return httpClientBuilder;
});
client = new RestHighLevelClient(restClientBuilder);
}
return client;
}
public void createIndex(String index, String type, String mapping,Map settings) {
RestHighLevelClient client = getClient();
CreateIndexRequest request = new CreateIndexRequest(index);
if(null != settings){
request.settings(settings);
}
request.mapping(type,mapping, XContentType.JSON);
try {
client.indices().create(request, RequestOptions.DEFAULT);
} catch (IOException e) {
//log.error(DgdeviceLoggerHelper.message(ErrorCode.ES_OPERATION_FAILD, " es createIndex method error..."), e);
}
}
/**
* @param host
* @param hostList
* @return
*/
private static void getHostList(String host, ArrayList<HttpHost> hostList) {
String[] address = host.split(":");
if (address.length == 2) {
hostList.add(new HttpHost(address[0], Integer.parseInt(address[1]), "http"));
}
}
public void createIndexByDynamicTemplate(String indexName, String dynamicTemplates) {
if(StringUtils.isBlank(esAddress) || StringUtils.isBlank(dynamicTemplates)){
throw new BusinessException("esAddress or dynamicTemplates is null ,createDeviceIndex failure");
}
String url = "http://"+esAddress+"/"+indexName;
try {
HttpClientUtil.doPutJson(url,dynamicTemplates);
} catch (Exception e) {
//log.error(DgdeviceLoggerHelper.message(ErrorCode.ES_OPERATION_FAILD, " es createDeviceIndex method error..."), e);
}
}
/**
* @param index 索引名称 判断es中是否存在该索引
*/
public boolean exsitIndex(String index) {
RestHighLevelClient client = getClient();
boolean result = false;
GetIndexRequest request = new GetIndexRequest();
request.indices(index);
request.local(false);
request.humanReadable(true);
request.includeDefaults(false);
try {
result = client.indices().exists(request, RequestOptions.DEFAULT);
} catch (IOException e) {
//log.error(DgdeviceLoggerHelper.message(ErrorCode.ES_OPERATION_FAILD, " es exsitIndex method error..."), e);
}
return result;
}
}