Looking for java implementation

can I call this elasticsearch-SQL command from java ? if yes can someone tell me how

curl -X POST http://hadoop1:9200/_xpack/sql?format=txt -H 'Content-Type: application/json' -d '{"query": "SELECT * FROM product WHERE prodid = 758677748 "}'

Hi @aliyesami,
For Java access to ES-SQL features, you can use the JDBC driver. You have some starting point here, in the documentation.

this document I already followed and have installed jdbc. any working example available to any kind of ES query or creating index?

There is a starting point in the page I already referenced about getting the Connection to Elasticsearch and initiating a request (a PreparedStatement). The PreparedStatement can have parameters and they are specified in the query itself as ? (question marks). This is your typical JDBC access to a database, so if you are familiar with that, there is no difference between accessing a MySQL database or accessing Elasticsearch through the SQL plugin.

Please, do remember something: with ES-SQL JDBC (and ES-SQL in general) you cannot create an index, you cannot add, delete or update documents, only reads are permitted.

To get you started with something, here is a very simple example (a bit more than what it's in the documentation):

		String url = "jdbc:es://localhost:9200/";
		Connection conn = DriverManager.getConnection(url);
		
		PreparedStatement statement = conn.prepareStatement("SELECT some_id, some_date, prodid FROM product WHERE prodid = ?");
		statement.setLong(1, 758677748L);
		
		ResultSet results = statement.executeQuery();
		while (results.next()) {
			// do something with the results
			results.getInt("some_id");
			results.getDate("some_date");
			System.out.println(results.getLong("prodid"));
			System.out.println(results.getLong(3));
		}

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