截至7.15前版本的Java api官网已废弃。
1、添加依赖
<dependency> <groupId>co.elastic.clients</groupId> <artifactId>elasticsearch-java</artifactId> <version>7.16.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency>
2、账号密码连接集群
String nodes = "192.168.1.96:9200,192.168.1.97:9200,192.168.1.98:9200";//集群节点 HttpHost[] httpHosts = Arrays.stream(nodes.split(",")).map(x -> { String[] hostInfo = x.split(":"); return new HttpHost(hostInfo[0], Integer.parseInt(hostInfo[1])); }).toArray(HttpHost[]::new); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials("elastic", ""));//账号密码 RestClientBuilder builder = RestClient.builder(httpHosts) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); RestClient restClient = builder.build(); ElasticsearchTransport transport = new RestClientTransport( restClient, new JacksonJsonpMapper()); ElasticsearchClient client = new ElasticsearchClient(transport);
今天的文章
ES 7.16 Java API Clien 新特性(ES开启x-pack安全认证,RestClientBuilder设置账号密码连接集群)分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/80571.html