Connecting To Presto via JDBC

Hi Guys,

In this blog we’ll be discussing about how to make a connection to presto server using JDBC, but before we get started let’s discuss what Presto is.

What is Presto ?

So, Presto is an open source distributed SQL query engine for running interactive analytic queries against different data sources. The sizes may ranges from gigabytes to petabytes. It runs on a cluster of machines and its installation includes a coordinator and multiple workers. It allows querying data where it lives, including Hive, Cassandra, relational databases or even proprietary data stores. A single Presto query can combine data from multiple sources, allowing for analytics across your entire organization.

For deploying presto on your machine you can go through the following link : Presto Installation

Setting up a JDBC connection

Prerequisite : The presto cluster must be running before establishing the connection.

Below is your JDBC driver url for presto driver.

JDBC_DRIVER = "com.facebook.presto.jdbc.PrestoDriver";

And your Database url should look like :

DB_URL = "jdbc:presto://localhost:8080/catalog/schema";

The following JDBC URL formats are supported:


Here host:port is the host name and port number that you have specified in the discovery.uri parameter in file present inside the presto directory. Catalog is the catalog name to which you want to connect as a datasource and schema is the name of the database present in the datasource.

Catalog files are present inside the /etc/catalog folder in your presto directory. You can have multiple properties file for different datasources in the catalog directory.

So, here is a sample JDBC program:

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

public class PrestoJdbc {

  public static void main(String args[]) {
    //Connect to Presto server using Presto JDBC

    final String JDBC_DRIVER = "com.facebook.presto.jdbc.PrestoDriver";
    final String DB_URL = "jdbc:presto://localhost:9000/catalogName/schemaName";
    //  Database credentials
    final String USER = "username";
    final String PASS = "password";
    Connection conn = null;
    Statement stmt = null;
    try {
      //Register JDBC driver
      //Open a connection
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      //Execute a query
      stmt = conn.createStatement();
      String sql = "select * from tableName";
      ResultSet res = stmt.executeQuery(sql);
      //Extract data from result set
      while ( {
        //Retrieve by column name
        String name = res.getString("name");
        //Display values
        System.out.println("name : " + name);
      //Clean-up environment
    } catch (SQLException se) {
      //Handle errors for JDBC
    } catch (Exception e) {
      //Handle errors for Class.forName
    } finally {
      //finally block used to close resources
      try {
        if (stmt != null) stmt.close();
      } catch (SQLException sqlException) {
      try {
        if (conn != null) conn.close();
      } catch (SQLException se) {

Happy learning.  🙂


This entry was posted in big data, database, Java, Scala, sql and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s