Avantica Calcite JDBC java connection

Hi Team,

Below is my jdbc code. The problem is i have enabled basic security over druid which is asking for credentials on druid console, but when i am using below code then i am able to query druid without credentials.

public Statement getDataSource() throws SQLException {
log.info(“DruidJDBCConfig.getDataSource() :: inside getDataSource() bean”);
String avaticaURL = “jdbc:avatica:remote:url=http://BROKER:8082/druid/v2/sql/avatica/”;
Properties connectionProperties = new Properties();
try {
Class.forName(“org.apache.calcite.avatica.remote.Driver”);
} catch (ClassNotFoundException e) {
log.error(“druid avatica driver class load fails”);
e.printStackTrace();
}

	try (Connection connection = DriverManager.getConnection(avaticaURL, connectionProperties)) {
		try (final Statement statement = connection.createStatement();) {
			[log.info](http://log.info)("DruidJDBCConfig.getDataSource() :: connection successfull with druid");
			return statement;
		}
	}
}

Can anyone suggest how to authenticate the query through java application ?

Thanks & Regards,

Anshul Sharma.

Hi Ansul,

Please refer below :

import java.sql.*;
import java.util.Properties;
public class JdbcTest
{
 public static void main(String args[]) {
// Connect to /druid/v2/sql/avatica/ on your Broker.
String url = "jdbc:avatica:remote:url=http://<DRUID_BROKER_HOST>:<DRUID_BROKER_PORT>/druid/v2/sql/avatica/";
**Properties connectionProperties = new Properties();
connectionProperties.setProperty("user", "$USER_NAME");
connectionProperties.setProperty("password", "$USER_PASSWORD");**
try (Connection connection = DriverManager.getConnection(url,connectionProperties)) { try (

final Statement statement = connection.createStatement();

final ResultSet rs = statement.executeQuery("select count(*) from wikipedia"); )

{
 while (rs.next()) {

// Do something

System.out.println("The Count Result is=" + rs.getString(1)); }

} }

catch (Exception e) {

System.out.println(e.toString()); }

} }

How to use this program:

To Compile :

javac JdbcTest.java
Eg.
 Vaibhavs-MacBook-Pro$ javac JdbcTest.java

3. Download the Avatica JDBC driver,

4.To Execute:

java -cp .:<Avatica jar Location>/avatica-1.12.0.jar JdbcTest

Thanks and Regards,
Vaibhav 

Hi Vaibhav,

Thanks for the response.

The problem is after enabling basic security also i am able to query druid without credentials.

I think it should not allow if you have enabled the basic auth.

Could you try berlow curl and see it throw the authentication exception:

curl -X ‘POST’ -H ‘Content-Type:application/json’ @ -d @/query.json http://DRUID_BROKER_HOST:PORT/broker/druid/v2/sql

you need to create a query.json file , eg :

{
“query”:"select count(*) from "
}

Thanks and Regards,
Vaibhav