摘要:通过JAVA API操作elasticsearch5.6.1的连接及连接过程中所遇到的问题,问题分析,问题解决。作为一个ES的JAVA操作的新生参考与记录。
JAVA操作ES的文档:
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html
如果安装了xpack一定要查看这个:
https://www.elastic.co/guide/en/x-pack/current/java-clients.html
创建文件名log4j2.properties,放在src的根目录就可以,让java可以找到;
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
rootLogger.level = info
rootLogger.appenderRef.console.ref = console
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>sparkmlgroupId>
<artifactId>esartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>esname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<repositories>
<repository>
<id>elasticsearch-releasesid>
<url>https://artifacts.elastic.co/mavenurl>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
<dependencies>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>transportartifactId>
<version>5.6.1version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>x-pack-transportartifactId>
<version>5.6.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
dependencies>
project>
public class App {
@SuppressWarnings({ "unchecked", "resource" })
public static void main(String[] args) {
// on startup
TransportClient client;
try {
Settings settings = Settings.builder()
.put("cluster.name", "esName")
.put("xpack.security.transport.ssl.enabled", false)
.put("xpack.security.user", "elastic:changeme")
.put("client.transport.sniff", true).build();
client = new PreBuiltXPackTransportClient(settings)
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("192.168.xxx.xxx"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("192.168.xxx.xxx"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("192.168.xxx.xxx"), 9300));
GetResponse response = client.prepareGet("index", "fulltext", "2")
.setOperationThreaded(true).get();
System.out.println(response);
// on shutdown
client.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
loaded plugin [org.elasticsearch.xpack.XPackPlugin]
{"_index":"index","_type":"fulltext","_id":"2","_version":2,"found":true,"_source":{
"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
}
}
failed to get node info for {#transport#-1}{aOvqR1DwTkSlqWAiPocgXw}{192.168.xxx.xxx}{192.168.xxx.xxx:9300}, disconnecting...
org.elasticsearch.transport.RemoteTransportException: [node][192.168.xxx.xxx:9300][cluster:monitor/nodes/liveness]
Caused by: org.elasticsearch.ElasticsearchSecurityException: missing authentication token for action [cluster:monitor/nodes/liveness]
at org.elasticsearch.xpack.security.support.Exceptions.authenticationError(Exceptions.java:39) ~[?:?]
at org.elasticsearch.xpack.security.authc.DefaultAuthenticationFailureHandler.missingToken(DefaultAuthenticationFailureHandler.java:74) ~[?:?]
at org.elasticsearch.xpack.security.authc.AuthenticationService$AuditableTransportRequest.anonymousAccessDenied(AuthenticationService.java:553) ~[?:?]
at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$handleNullToken$16(AuthenticationService.java:363) ~[?:?]
原因
这个因为ES安装x-pack,所以在访问x-pack的时候要加入用户名也密码;
在设置Settings中加入.put(“xpack.security.user”, “elastic:changeme”)。
Exception in thread "main" java.lang.IllegalArgumentException: unknown setting [xpack.security.transport.ssl.enabled] please check that any required plugins are installed, or check the breaking changes documentation for removed settings
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:293)
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:256)
at org.elasticsearch.common.settings.SettingsModule.(SettingsModule.java:139)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:138)
at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:265)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:130)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:116)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:106)
at sparkml.es.App.main(App.java:30)
Suppressed: java.lang.IllegalArgumentException: unknown setting [xpack.security.user] please check that any required plugins are installed, or check the breaking changes documentation for removed settings
... 9 more
原因:
不认识加了xpack开头的所有参数,这个是因为缺少xpack的lib造成的。
解决:
pom.xml加上
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>x-pack-transportartifactId>
<version>5.6.1version>
dependency>
不过一般默认的maven配置的库对这个包是找不到,所以是要对库进行配置一下:
<repositories>
<repository>
<id>elasticsearch-releasesid>
<url>https://artifacts.elastic.co/mavenurl>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
大数据学习[06]:elasticsearch5.6.1初探:http://blog.csdn.net/ld326/article/details/78057145
大数据学习[07]:elasticsearch5.6.1集群与问题:http://blog.csdn.net/ld326/article/details/78066952
【作者:happyprince, http://blog.csdn.net/ld326/article/details/78136182】