刚使用es的RestHighLevelClient连接了es集群,查了很多资料,请各位大佬指正。。。
maven的配置:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client-sniffer</artifactId>
<version>6.2.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.2.4</version>
</dependency>
第一个是es的嗅探器,第二第三是es的版本以及es的RestHighLevelClient版本依赖。
首先初始化es的restClient,创建restClient的bulider类:
public class ClientBuilders {
private static final String CLUSTER_HOSTNAME_PORT = "192.169.0.49:9200,192.169.0.50:9200";
/**
* 初始化 clientBuilder的详细说明
* @return
*/
public static RestClientBuilder getClientBulider() {
String [] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(",");
String host;
int port;
String[] temp;
RestClientBuilder restClientBuilder = null;
/*restClient 初始化*/
if (0 != hostNamesPort.length) {
for (String hostPort : hostNamesPort) {
temp = hostPort.split(":");
host = temp[0].trim();
port = Integer.parseInt(temp[1].trim());
restClientBuilder = RestClient.builder(new HttpHost(host, port, "http"));
}
//这有问题,不应该用for的,写入多个httpHost可以在builder中用多个new HttpHost(host, port, "http")。
}
/*RestClientBuilder 在构建 RestClient 实例时可以设置以下的可选配置参数*/
/*1.设置请求头,避免每个请求都必须指定*/
Header[] defaultHeaders = new Header[]{
new BasicHeader("header", "value")
};
restClientBuilder.setDefaultHeaders(defaultHeaders);
/*2.设置在同一请求进行多次尝试时应该遵守的超时时间。默认值为30秒,与默认`socket`超时相同。
如果自定义设置了`socket`超时,则应该相应地调整最大重试超时。*/
restClientBuilder.setMaxRetryTimeoutMillis(10000);
/*3.设置每次节点发生故障时收到通知的侦听器。内部嗅探到故障时被启用。*/
restClientBuilder.setFailureListener(new RestClient.FailureListener() {
@Override
public void onFailure(HttpHost host) {
// TODO 这里是当嗅探器测出某个节点有故障时,如何提醒用户,或有问题后做其他操
//作。
}
});
/*4.设置修改默认请求配置的回调(例如:请求超时,认证,或者其他
设置)。
*/
restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.setSocketTimeout(10000);
}
});
/*5.//设置修改 http 客户端配置的回调(例如:ssl 加密通讯,线程IO的配置,或其他任何 设置)*/
// 简单的身份认证
final CredentialsProvider credentialsProvider =
new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("user", "password"));
restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//线程设置
httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(10).build());
return httpClientBuilder;
}
});
return restClientBuilder;
}
}
之后,创建RestHighLevelClient 实例,调用clientBulider:
public class HighLevelClient {
private static RestClientBuilder restClientBuilder = ClientBuilders.getClientBulider();
// 实例化客户端
private static RestHighLevelClient restHighLevelClient;
// 嗅探器实例化
private static Sniffer sniffer;
/**
* 开启client,sniffer
* @return
*/
public RestHighLevelClient getClient() {
restHighLevelClient = new RestHighLevelClient(restClientBuilder);
//十秒刷新并更新一次节点
sniffer = Sniffer.builder(restHighLevelClient.getLowLevelClient())
.setSniffAfterFailureDelayMillis(10000)
.build();
return restHighLevelClient;
}
/**
*
* @throws ESIoException
* 关闭sniffer client
*/
public void closeRestHighLevelClient() throws ESIoException {
if (null != restHighLevelClient) {
try {
sniffer.close();
restHighLevelClient.close();
} catch (IOException e) {
throw new ESIoException("RestHighLevelClient Client close exception", e);
}
}
}
}
最后,创建创建函数类,创建main方法,调用client,就成功了:
public class EsOperate {
public static void main(String[] args) throws IOException {
HighLevelClient highLevelClient = new HighLevelClient();
RestHighLevelClient client = highLevelClient.getClient();
SearchRequest searchRequest = new SearchRequest("lib2");
SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
for (SearchHit hit : hits) {
String index = hit.getIndex(); //获取文档的index
String type = hit.getType(); //获取文档的type
String id = hit.getId(); //获取文档的id
Map<String, Object> sourceMap = hit.getSourceAsMap();
String sourceString = hit.getSourceAsString(); //获取文档内容,转换为json字符串。
System.out.println(id+"$$$$$$$$$$"+index+"$$$$$$$$$$$$$$$$$"+type+"&&&&&&&&&&&&&&&&"+sourceString);
}
highLevelClient.closeRestHighLevelClient();
}
}
es的集群安装以及Kibana的安装有时间再写。
参考文档:
https://segmentfault.com/a/1190000017119757
https://blog.csdn.net/HuoqilinHeiqiji/article/details/87972878
https://github.com/codersfarm/elasticsearch/tree/master/elasticsearch6.x/src/main/java/com/elastic