Java applications can access databases using the JDBC driver provided by Presto. The procedure is the same as that for general RDBMS databases.

Introduction to Maven

You can add the following configuration to the POM file to introduce the Presto JDBC driver:
<dependency>
    <groupId>com.facebook.presto</groupId>
    <artifactId>presto-jdbc</artifactId>
    <version>0.187</version>
</dependency>

Driver class name

The Presto JDBC driver class is com.facebook.presto.jdbc.PrestoDriver.

Connection string

The following connection string format is supported.
jdbc:presto://<COORDINATOR>:<PORT>/[CATALOG]/[SCHEMA]
For example:
jdbc:presto://emr-header-1:9090               # Connects to data base, using the default Catalog and Schema
jdbc:presto://emr-header-1:9090/hive          # Connects to data base, using Catalog(hive) and the default Schema
jdbc:presto://emr-header-1:9090/hive/default  # Connects to data base, using Catalog(hive) and Schema(default)

Connection parameters

The Presto JDBC driver supports various parameters that may be set as URL parameters or as Properties and passed to DriverManager.

Example of passing parameters to DriverManager as Properties:
String url = "jdbc:presto://emr-header-1:9090/hive/default";
Properties properties = new Properties();
properties.setProperty("user", "hadoop");
Connection connection = DriverManager.getConnection(url, properties);
......
Example of passing parameters to DriverManager as URL parameters:
String url = "jdbc:presto://emr-header-1:9090/hive/default? user=hadoop";
Connection connection = DriverManager.getConnection(url);
......
Parameters are described as follows:
Parameter name Format Description
user STRING User name.
password STRING Password.
Socksproxy \:\ SOCKS proxy server address and port. For example, localhost:1080.
httpProxy \:\ HTTP proxy server address and port. For example, localhost:8888.
SSL true\ Whether or not to use HTTPS for connections. This is false by default.
SSLTrustStorePath STRING Java TrustStore file path.
SSLTrustStorePassword STRING Java TrustStore password.
KerberosRemoteServiceName STRING Kerberos service name.
KerberosPrincipal STRING Kerberos principal.
KerberosUseCanonicalHostname true\ Whether or not to use the canonical hostname. This is false by default.
KerberosConfigPath STRING Kerberos configuration file path.
KerberosKeytabPath STRING Kerberos KeyTab file path.
KerberosCredentialCachePath STRING Kerberos credential cache path

Java example

The following is an example of using the Presto JDBC driver with Java.
.....
// Loads the JDBC Driver class
try {
    Class.forName("com.facebook.presto.jdbc.PrestoDriver");
} catch(ClassNotFoundException e) {
    LOG.ERROR("Failed to load presto jdbc driver.", e);
    System.exit(-1);
}
Connection connection = null;
Statement stmt = null;
try {
    String url = "jdbc:presto://emr-header-1:9090/hive/default";
    Properties properties = new Properties();
    properties.setProperty("user", "hadoop");
    // Creates the connection object
    Connection = drivermanager. getconnection (URL, properties );
    // Creates the Statement object
    statement = connection.createStatement();
    Executes the query
    ResultSet rs = statement.executeQuery("select * from t1");
    Returns results
    int columnNum = rs.getMetaData().getColumnCount();
    int rowIndex = 0;
    while (rs.next()) {
        rowIndex++;
        for(int i = 1; i <= columnNum; i++) {
            System.out.println("Row " + rowIndex + ", Column " + i + ": " + rs.getInt(i));
        }
    }
} catch(SQLException e) {
    LOG.ERROR("Exception thrown.", e);
} finally {
  // Destroys Statement object
  If (statement! = null) {
      try {
        statement.close();
    } catch(Throwable t) {
        // No-ops
    }
  }
  Closes connection
  if (connection ! = null) {
      try {
        connection.close();
    } catch(Throwable t) {
        // No-ops
    }
  }
}

Use reverse proxy

You can use the HAProxy reverse proxy Coodinator to access the Presto service through the proxy service.

  • Non-Security Cluster proxy configuration
    To configure a cluster proxy for a non-Security Cluster, follow these steps:
    1. Install HAProxy on the proxy Node
    2. Modify the HAProxy configuration (/Etc/haproxy. cfg), Add the following content:
      ......
      
      listen prestojdbc :9090
          Mode TCP
          option tcplog
          balance source
          Server presto-coodinator-1 emr-header-1: 9090
    3. Restart the HAProxy Service
    Now, you can use the proxy server to access Presto. You only need to change the IP address of the Connected Server to the IP address of the proxy service.