JDBC

MapD Core Database supports JDBC connections.

You can find the JAR file at $MAPD_PATH/bin/mapdjdbc-1.0-SNAPSHOT-jar-with-dependencies.jar

You can find example code in $MAPD_PATH/samples:

The driver is com.mapd.jdbc.MapDDriver

The URL is jdbc:mapd:<machine>:<port>:<dbname>

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SampleJDBC {

// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mapd.jdbc.MapDDriver";
static final String DB_URL = "jdbc:mapd:localhost:9091:mapd";

//  Database credentials
static final String USER = "mapd";
static final String PASS = "HyperInteractive";

public static void main(String[] args) {
  Connection conn = null;
  Statement stmt = null;
  try {
    //STEP 1: Register the JDBC driver
    Class.forName(JDBC_DRIVER);

    //STEP 2: Open a connection
    conn = DriverManager.getConnection(DB_URL, USER, PASS);

    //STEP 3: Execute a query
    stmt = conn.createStatement();

    String sql = "SELECT uniquecarrier from flights_2008_10k"
            + " GROUP BY uniquecarrier limit 5";
    ResultSet rs = stmt.executeQuery(sql);

    //STEP 4: Extract data from the result set
    while (rs.next()) {
      String uniquecarrier = rs.getString("uniquecarrier");
      System.out.println("uniquecarrier: " + uniquecarrier);
    }

    //STEP 5: Clean up the environment
    rs.close();
    stmt.close();
    conn.close();
  } catch (SQLException se) {
    //Handle errors for JDBC
    se.printStackTrace();
  } catch (Exception e) {
    //Handle errors for Class.forName
    e.printStackTrace();
  } finally {
    //finally block used to close resources
    try {
      if (stmt != null) {
        stmt.close();
      }
    } catch (SQLException se2) {
    }// nothing we can do
    try {
      if (conn != null) {
        conn.close();
      }
    } catch (SQLException se) {
      se.printStackTrace();
    }//end finally try
  }//end try
}//end main
}//end SampleJDBC

To compile and execute this example:

javac SampleJDBC.java
java -cp $MAPD_PATH/bin/mapdjdbc-1.0-SNAPSHOT-jar-with-dependencies.jar:./  SampleJDBC

Supported JDBC Methods

MapD Core supports the JDBC methods listed below.

Method Description
MapDConnection
clearWarnings Clears all warnings reported for this MapDConnection object. After a call to this method, the method getWarnings returns null until a new warning is reported for this MapDConnection object.
close Releases this MapDConnection object’s database and JDBC resources immediately instead of waiting for them to be automatically released.
commit Makes all changes made since the previous commit/rollback permanent and releases any database locks currently held by this MapDConnection object.
createStatement Returns a new, empty MapDStatement.
getAutoCommit Retrieves the current auto-commit mode for this MapDConnection object.
getCatalog Retrieves this MapDConnection object’s current catalog name.
getMetaData Retrieves a DatabaseMetaData object that contains metadata about the database to which this MapDConnection object represents a connection. The metadata includes information about the database’s tables, its supported SQL grammar, its stored procedures, the capabilities of this MapDConnection, and so on.
getTransactionIsolation Retrieves this MapDConnection object’s current transaction isolation level.
getWarnings Retrieves the first warning reported by calls on this MapDConnection object.
isClosed Retrieves whether this MapDConnection object has been closed.
isValid Returns true if the connection has not been closed and is still valid.
prepareCall(String sql) Creates a CallableStatement object for calling database stored procedures.
prepareStatement(String sql) Creates a MapDPreparedStatement object for sending parameterized SQL statements to the database.
setAutoCommit Sets this MapDConnection’s auto-commit mode to the given state.
setTransactionIsolation Attempts to change the transaction isolation level for this Connection object to the one given.
MapDDriver
acceptsURL Retrieves whether the driver thinks that it can open a connection to the given URL.
connect Attempts to make a database connection to the given URL.
getMajorVersion Retrieves the driver’s major version number.
getMinorVersion Gets the driver’s minor version number.
getParentLogger Return the parent Logger of all Loggers used by this driver.
getPropertyInfo Gets information about the possible properties for this driver.
jdbcCompliant Reports whether this driver is a genuine JDBC CompliantTM driver.
MapDStatement
cancel Cancels this Statement object if both the DBMS and driver support aborting an SQL statement.
close Close the current session.
createStatement Returns a new, empty MapDStatement.
execute Executes the given MapDStatement, which might return multiple results.
executeQuery Executes the given MapDStatement, which can be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
executeUpdate Executes the given MapDStatement and signals the driver with the given flag about whether the auto-generated keys produced by this MapDStatement object should be made available for retrieval.
getMaxFieldSize Retrieves the maximum number of bytes that can be returned for character and binary column values in a ResultSet object produced by this MapDStatement object.
getMaxRows Retrieves the maximum number of rows that a ResultSet object produced by this MapDStatement object can contain.
getMetaData Returns a new MapDDatabaseMetaData object.
getQueryTimeout Retrieves the number of seconds the driver wait for a MapDStatement object to execute.
preparedStatement Returns a MapDPreparedStatement with the SQL instructions you provide.
setMaxFieldSize Sets the limit for the maximum number of bytes that can be returned for character and binary column values in a ResultSet object produced by this MapDStatement object.
setMaxRows Sets the limit for the maximum number of rows that any ResultSet object generated by this MapDStatement object can contain to the given number. Default is 100,000 rows.
setQueryTimeout Sets the number of seconds the driver waits for a MapDStatement object to execute to the given number of seconds.

You can connect to a JDBC session using the HTTP protocol by appending the http option to a URL. jdbc:mapd:localhost:9092:mapd:http

Unsupported Features

MapD does not support the following JDBC features at this time.

  • Batch statements
  • Multiple result sets
  • Domains
  • Rules
  • Database procedures
  • Indexes
  • Keys
  • Transactions
  • Schemas (any client can see tables and views created by any user without restriction)
  • Rollbacks, checkpoints, or any other type of database recovery