Java Sample Code

To use the Drill JDBC driver in an application, you must include all of the JAR files from the ZIP archive in the classpath for the Java project.

  1. You can download JDBC driver or, starting from EEP-9.3.2, you can retrieve a compatible JDBC driver directly from a cluster with the mapr-drill-internal package installed. You can find the driver in the following directory:
    $DRILL_HOME/jars/jdbc-driver/drill-jdbc-all-<drill-version>.jar
    The following Java code shows how to use the JDBC API to:
    • Register the driver for Drill
    • Establish a connection to a Drill server (MAPRSASL secure cluster)
    • Query demo data set from Drill classpath default examples (cp.`employee.json`)
    • Parse a result set
    • Handle exceptions.
    import java.sql.*;
    
    public class DrillJDBCExample {
        private static final String JDBC_DRIVER = "org.apache.drill.jdbc.Driver";
        private static final String CONNECTION_URL = "jdbc:drill:drillbit=127.0.0.1:31010;auth=MAPRSASL";
    
        public static void main(String[] args) {
            try {
                // Load the JDBC driver
                Class.forName(JDBC_DRIVER);
    
                // Establish a connection
                try (Connection con = DriverManager.getConnection(CONNECTION_URL);
                     Statement stmt = con.createStatement();
                     ResultSet rs = stmt.executeQuery("SELECT employee_id, full_name, first_name, last_name, salary FROM cp.`employee.json` LIMIT 20")) {
    
                    System.out.printf("%20s%20s%20s%20s%20s\r\n", "employee_id", "full_name", "first_name", "last_name", "salary");
    
                    while (rs.next()) {
                        System.out.printf("%20s%20s%20s%20s%20s\r\n",
                                rs.getString("employee_id"),
                                rs.getString("full_name"),
                                rs.getString("first_name"),
                                rs.getString("last_name"),
                                rs.getString("salary"));
                    }
                }
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }
    }
    NOTE
    The Class.forName(JDBC_DRIVER) statement is not necessary for Java 6 and later versions, as the JDBC driver is automatically loaded when the DriverManager.getConnection method is called.
  2. Compile the application using command:
    javac DrillJDBCExample.java
  3. Run the application to using the command:
    java -cp /opt/mapr/drill/drill-1.21.2/jars/jdbc-driver/drill-jdbc-all-1.21.2.100-eep-932-SNAPSHOT.jar:. DrillJDBCExample

Example of output

$ java -cp /opt/mapr/drill/drill-1.21.2/jars/jdbc-driver/drill-jdbc-all-1.21.2.100-eep-932-SNAPSHOT.jar:. DrillJDBCExample
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.mapr.fs.ShimLoader (file:/opt/mapr/drill/drill-1.21.2/jars/jdbc-driver/drill-jdbc-all-1.21.2.100-eep-932-SNAPSHOT.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.mapr.fs.ShimLoader
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
         employee_id           full_name          first_name           last_name              salary
                   1        Sheri Nowmer               Sheri              Nowmer             80000.0
                   2     Derrick Whelply             Derrick             Whelply             40000.0
                   4      Michael Spence             Michael              Spence             40000.0
                   5      Maya Gutierrez                Maya           Gutierrez             35000.0
                   6     Roberta Damstra             Roberta             Damstra             25000.0
                   7    Rebecca Kanagaki             Rebecca            Kanagaki             15000.0
                   8         Kim Brunner                 Kim             Brunner             10000.0
                   9     Brenda Blumberg              Brenda            Blumberg             17000.0
                  10        Darren Stanz              Darren               Stanz             50000.0
                  11   Jonathan Murraiin            Jonathan            Murraiin             15000.0
                  12         Jewel Creek               Jewel               Creek              8500.0
                  13        Peggy Medina               Peggy              Medina             15000.0
                  14      Bryan Rutledge               Bryan            Rutledge             17000.0
                  15    Walter Cavestany              Walter           Cavestany             12000.0
                  16        Peggy Planck               Peggy              Planck             17000.0
                  17     Brenda Marshall              Brenda            Marshall             10000.0
                  18       Daniel Wolter              Daniel              Wolter             17000.0
                  19      Dianne Collins              Dianne             Collins             10000.0
                  20       Beverly Baker             Beverly               Baker             30000.0
                  21      Pedro Castillo               Pedro            Castillo             35000.0