I'm able to connect to elastic search 6.1.0 and index, update, delete and query the documents via cURL and plugin head but I'm unable to connect to the same via the java client, i.e,
/**
* Elasticsearch
*/
// https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch
compile group: 'org.elasticsearch', name: 'elasticsearch', version: '6.1.0'
// https://mvnrepository.com/artifact/org.elasticsearch.client/transport
**compile group: 'org.elasticsearch.client', name: 'transport', version: '6.1.0'**
dependencies. Below is elasticsearch.yml
#cluster.name: my-application
cluster.name: saasworthy-qa
node.name: qa-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
#network.host: 192.168.0.1
network.host: 10.10.10.2
#http.port: 9200
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#discovery.zen.ping.unicast.hosts: ["10.10.10.2"]
#discovery.zen.minimum_master_nodes: 1
The error received via java client is:
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{SLvQfw9FREWVhpwTBtjNWg}{X.X.X.X}{X.X.X.X:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245)
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:360)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
at com.sw.config.elastic.connection.factory.SaaSWorthyElasticsearchClientFactory.main
Also, netstat info,
netstat -antp | grep 9300
tcp6 0 0 10.10.10.2:9300 :::* LISTEN 19768/java
Java Client Code:
public class SampleElasticsearchClientFactory extends BaseElasticsearchClientFactory {
private static final Logger logger = LoggerFactory.getLogger(SampleElasticsearchClientFactory.class);
private String clusterName = "saasworthy-qa" ;
private String addresses = "X.X.X.X";
private String ports = "9300";
private void createConnection() throws UnknownHostException {
logger.info("Initiating Elasticsearch Client creation.");
super.createClient(addresses, ports, clusterName);
logger.info("Elasticsearch Client created.");
}
private void closeConnection() {
if (getClient() != null) {
getClient().close();
}
}
public static void main(String[] args) throws UnknownHostException {
SampleElasticsearchClientFactory f = new SampleElasticsearchClientFactory();
f.createConnection();
SearchResponse response = f.getClient().prepareSearch("temp").setTypes("sample").setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
System.out.println(response);
}
}
public class BaseElasticsearchClientFactory {
private Client client;
public Client getClient() {
return client;
}
public void createClient(String addresses, String ports, String clusterName) throws UnknownHostException {
Settings settings = Settings.builder()
.put("cluster.name", clusterName)
.put("client.transport.sniff", true).build();
if (!StringUtils.isEmpty(addresses) && !StringUtils.isEmpty(ports)) {
TransportClient transportClient = new PreBuiltTransportClient(settings);
String[] address = addresses.split(",");
String[] port = ports.split(",");
for (int i = 0; i < address.length; i++) {
transportClient = transportClient.addTransportAddress(
new TransportAddress(InetAddress.getByName(address[i].trim()), port[i] == null ? 9300 : Integer.valueOf(port[i].trim()))
);
}
this.client = transportClient;
}
}
}
Kindly help me out in resolving the issue. Thanks.