Elasticsearch java API[5.0] using problem

Hi ,
I am very new to elasticsearch , I want to connect with elasticsearch engine using java API[5.0].
Can anyone give the sample code for this.I tried googling but didn't find any full example which can help me regarding this .

Did you read https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html?

Hi David i read that but didn't get enough information to connect to elasticsearch engine . i am using elasticsearch verion 5.0.2 and java api 5.0 . Please can i have one full sample program with proper imports statements .

What did not work so far? I mean I can definitely copy and paste the guide here but it does not really make sense IMO.

Hi David I am trying to connect with elastic search engine but not able to connect getting Null Pointer Exception.
Below is my code sample for connecting may i know please where I am doing wrong i am very new to this doing POC for this and have to start implementation.

    public static TransportClient createTransportClient(){
    		TransportClient client=null;
    		try {
    			client=new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getLocalHost(), 9200));
    		}catch (Exception e){

    		}finally {
    			client.close();
    		}
    		return client;

    	}
 Exception in thread "main" java.lang.NullPointerException

Please format your code using </> icon as explained in this guide. It will make your post more readable.

What is the stack trace?

David waiting for your reply how can i initialize the TransportClinet as i mentioned in above post i am getting Null Pointer Exception and even i referred below links also but in these links they have given examples using old API version.
http://www.programcreek.com/java-api-examples/index.php?class=org.elasticsearch.client.transport.TransportClient&method=addTransportAddress

Ok. Let me copy and paste the documentation:

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

David thanks for immediate reply i tried that but unluckily didn't work for me .

I can't help sorry. I don't have details I asked you for.
Like a stack trace, an error message.

David i have attached the code base in above post which i was writing for to create TransportClient .I am calling createTransportClient()method from main()and the stack trace is as below:

Exception in thread "main" java.lang.NullPointerException
	at com.SampleApplication.createTransportClient(SampleApplication.java:31)
	at com.SampleApplication.main(SampleApplication.java:18)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

What do you have exactly at line 31 of SampleApplication.java?

client.close(); at line no 31 inside finally block David

so the client is null and has not been initialized.
Probably because you are swallowing the Exception.

May be write something:

try {
  client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
 } catch (Exception e) {
  // Print your exception here. Important things are in it.
 } finally {
  if (client != null) {
    client.close();
  }
}

Thanks David now I am getting below exception when i print it in catch block.

Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/plugins/ActionPlugin
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.<clinit>(PreBuiltTransportClient.java:54)
	at com.tpgsi.cdr.SampleApplication.createTransportClient(SampleApplication.java:27)
	at com.tpgsi.cdr.SampleApplication.main(SampleApplication.java:18)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.plugins.ActionPlugin
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 20 more

David my gradle dependency is as below:

dependencies {
	compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')
	compile('org.springframework.boot:spring-boot-starter-data-mongodb')
	compile('org.springframework.boot:spring-boot-starter-web')
}

So you did not import the following dependency?

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.0.1</version>
</dependency>

BTW you are using spring-boot. Is it compatible with elasticsearch 5.0?

Hi David you are right spring-boot is not compatible with 5.0 .
Thanks a lot for helping now I am able to create TransportClient after adding org.elasticsearch.client dependency .

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.