Apache Drill access via Java JDBC API

Here is a quick draft on accessing Apache Drill via the java JDBC.

1. Add the Drill dependency-

<dependency>
      <groupId>org.apache.drill.exec</groupId>
      <artifactId>drill-jdbc</artifactId>
      <version>1.1.0</version>
</dependency>

2. Java Code to access Drill

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

import org.apache.drill.exec.ExecConstants;
import org.apache.drill.exec.client.DrillClient;
import org.apache.drill.exec.proto.UserBitShared.QueryType;
import org.apache.drill.exec.rpc.RpcException;
import org.apache.drill.exec.rpc.user.QueryDataBatch;
import org.apache.drill.jdbc.Driver;

public class DrillJdbcTest {

	public static void main(String[] args) {
		try {
			method();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static void method() throws SQLException {
		String sql = "SELECT * FROM cp.`employee.json` LIMIT 20";
		Connection con = null;
		String jdbcUrl = "jdbc:drill:zk=local";
		//String jdbcUrl = "jdbc:drill:zk=127.0.0.1:2181/drill/drillbits1;schema=dfs.tmp";
		
		try {
			con = new Driver().connect(jdbcUrl, getDefaultProperties());
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while (rs.next()) {
				System.out.println(rs.getString(1));
				System.out.println(rs.getString(2));
				System.out.println(rs.getString(3));
			}
			System.out.println("DONE !!");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			con.close();
		}
	}

	public static Properties getDefaultProperties() {
		final Properties properties = new Properties();
		properties.setProperty("drillJDBCUnitTests", "true");
		properties.setProperty(ExecConstants.HTTP_ENABLE, "false");
		return properties;
	}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *