java xpack密码连接elasticsearch 7.1集群
原创 2019-09-08 14:18 阅读(2996)次
java连接elasticsearch7.x版本跟6.x有很大不一样,api都改了不少。废话不多说,直接分享代码,让看到的人少掉坑里。
由于我开启了xpack密码验证,所以连接es时也要加上密码,代码如下:
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
public class EsUtil {
public static RestHighLevelClient getClient() {
/** 用户认证对象 */
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
/** 设置账号密码 */
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "123456"));
/** 创建rest client对象 */
RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200))
.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
/**
* 单条保存
* @param index
* @param id
* @param m
*/
public static void saveData(String index,String id,Map<String, Object> m){
try {
RestHighLevelClient client = getClient();
IndexRequest indexRequest = new IndexRequest(index)
.id( id)
.source(m);
client.index(indexRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Date d = new Date();
String id = d.getTime()+"";
Map<String, Object> m = new HashMap<String, Object>();
m.put("id", id);
m.put("area_id", 1);
m.put("camera_id", 1);
m.put("log_time","2019-08-01 11:11:11");
m.put("age", 1);
EsUtil.saveData("global_house_list",id,m);
}
}
pom.xml: <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.1.0</version>
</dependency>
但是测试时getClient这个方法可以这样写,生产环境中,我们不可能一直new Client,这样效率很慢,所以需要将连接好的client存在内存中,减少不必要的生成client对象,将上面的getClient()方法修改单列如下:
//每次都取client太耗时,大约需要2秒左右,所以只取一次,放在内存中,不关闭,一直用
private static RestHighLevelClient client = null;
public static RestHighLevelClient getClient() {
try {
if(client != null){
return client;
}
synchronized (EsUtil.class) {
if(client != null){
return client;
}
long t = new Date().getTime();
/** 用户认证对象 */
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
/** 设置账号密码 */
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "123456"));
/** 创建rest client对象 */
RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200))
.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
client = new RestHighLevelClient(builder);
long t1 = new Date().getTime();
System.out.println("获得连接,耗时:"+(t1-t)+"s");
return client;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
下一篇:js格式化日期Date