Facing error while i am trying to connect my JAVA connection class to ElasticSearch

Hello Sir,
I have created a class in my JAVA project so that i can run SQL queries and fetch the result from Elastic Search

Here is my code:

package javaConn;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class SQLConn {

public static void main(String args[]){
	Connection conn;
	try
	{	
		String address = "jdbc:es://" + "10.199.62.140:9200";
		Properties properties = new Properties();
		properties.put("user", "userName");
		properties.put("password", "password");
	     conn = DriverManager.getConnection(address, properties);
		
		Statement stat=(Statement) conn.createStatement();
		boolean ret=stat.execute("Select * from index_name");
		if(ret){
			ResultSet rs = stat.getResultSet();
			while(rs.next())
			{	
			System.out.println("name :"+rs.getString(1));
			}
		}
}
	catch (SQLException e ) {
		System.out.println(e.getMessage());
		System.out.println(e);
	}
}

}

i am facing this error:

when i debugged the code then i find the error at this line i.e {boolean ret=stat.execute("Select * from table_name");}

Exception in thread "main" java.util.ServiceConfigurationError: org.elasticsearch.common.xcontent.XContentBuilderExtension: Provider org.elasticsearch.common.xcontent.XContentElasticsearchExtension could not be instantiated
at java.util.ServiceLoader.fail(Unknown Source)
at java.util.ServiceLoader.access$100(Unknown Source)
at java.util.ServiceLoader$LazyIterator.nextService(Unknown Source)
at java.util.ServiceLoader$LazyIterator.next(Unknown Source)
at java.util.ServiceLoader$1.next(Unknown Source)
at org.elasticsearch.common.xcontent.XContentBuilder.(XContentBuilder.java:118)
at org.elasticsearch.xpack.sql.client.HttpClient.toXContent(HttpClient.java:142)
at org.elasticsearch.xpack.sql.client.HttpClient.post(HttpClient.java:100)
at org.elasticsearch.xpack.sql.client.HttpClient.query(HttpClient.java:80)
at org.elasticsearch.xpack.sql.jdbc.JdbcHttpClient.query(JdbcHttpClient.java:68)
at org.elasticsearch.xpack.sql.jdbc.JdbcStatement.initResultSet(JdbcStatement.java:160)
at org.elasticsearch.xpack.sql.jdbc.JdbcStatement.execute(JdbcStatement.java:151)
at javaConn.SQLConn.main(SQLConn.java:48)
Caused by: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
... 11 more
Caused by: java.lang.ClassNotFoundException: org.joda.time.ReadableInstant
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 15 more

One of the columns is DateTime and the drive tries to build a org.joda.time.ReadableInstant, but you're missing the dependency.
Can you try to add a dependency (and the jar) to joda-time ?

Thank You Sir
It worked and my problem is solved now

1 Like

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