Use Presto with Dataproc

Presto is a distributed SQL query engine designed to query large data sets distributed over one or more heterogeneous data sources. Presto can query Hive, MySQL, Kafka and other data sources through connectors. This tutorial shows you how to:

  • Install the Presto service on a Dataproc cluster
  • Query public data from a Presto client installed on your local machine that communicates with a Presto service on your cluster
  • Run queries from a Java application that communicates with the Presto service on your cluster through the Presto Java JDBC driver.

Objectives

  1. Create a Dataproc cluster with Presto installed

  2. Prepare data. This tutorial uses the Chicago Taxi Trips public dataset, available in BigQuery.

    1. Extract the data from BigQuery
    2. Load the data into Cloud Storage as CSV files
    3. Transform data:
      1. Expose the data as a Hive external table to make the data queryable by Presto
      2. Convert the data from CSV format into Parquet format to make querying faster
  3. Send Presto CLI or application code queries using an SSH tunnel or Presto JDBC driver, respectively, to the Presto coordinator running on the cluster

  4. Check logs and monitor the Presto service through the Presto Web UI

Costs

This tutorial uses billable components of Google Cloud, including:

Use the Pricing Calculator to generate a cost estimate based on your projected usage. New Google Cloud users may be eligible for a free trial.

Before you begin

If you haven't already done so, create a Google Cloud project and a Cloud Storage bucket to hold the data used in this tutorial.

  1. Setting up your project

    1. Sign in to your Google Account.

      If you don't already have one, sign up for a new account.

    2. In the Cloud Console, on the project selector page, select or create a Cloud project.

      Go to the project selector page

    3. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.

    4. Enable the Dataproc, Compute Engine, Cloud Storage, and BigQuery APIs.

      Enable the APIs

    5. Install and initialize the Cloud SDK.

  2. Creating a Cloud Storage bucket in your project to hold the data used in this tutorial.

    1. In the Cloud Console, go to the Cloud Storage Browser page.

      Go to the Cloud Storage Browser page

    2. Click Create bucket.
    3. In the Create bucket dialog, specify the following attributes:
    4. Click Create.

Create a Dataproc cluster

Create a Dataproc cluster using the optional-components flag (available on image cersion 1.3 and later) to install the Presto optional component on the cluster and the enable-component-gateway flag to enable the Component Gateway to allow you to access the Presto Web UI from the Cloud Console.

  1. Set environment variables:
    • PROJECT: your project ID
    • BUCKET_NAME: the name of the Cloud Storage bucket you created in Before you begin
    • REGION: region where the cluster used in this tutorial will be created, for example, "us-west1"
    • WORKERS: 3 - 5 workers are recommended for this tutorial
    export PROJECT=project-id
    export WORKERS=number
    export REGION=region
    export BUCKET_NAME=bucket-name
    
  2. Run the gcloud command-line tool on your local machine to create the cluster.
    gcloud beta dataproc clusters create presto-cluster \
        --project=${PROJECT} \
        --region=${REGION} \
        --num-workers=${WORKERS} \
        --scopes=cloud-platform \
        --optional-components=PRESTO \
        --image-version=1.3  \
        --enable-component-gateway
    

Prepare data

Export the bigquery-public-data chicago_taxi_trips dataset to Cloud Storage as CSV files, then create a Hive external table to reference the data.

  1. On your local machine, run the following command to import the taxi data from BigQuery as CSV files without headers into the Cloud Storage bucket you created in Before you begin.
    bq --location=us extract --destination_format=CSV \
         --field_delimiter=',' --print_header=false \
           "bigquery-public-data:chicago_taxi_trips.taxi_trips" \
           gs://${BUCKET_NAME}/chicago_taxi_trips/csv/shard-*.csv
    
  2. Create Hive external tables that are backed by the CSV and Parquet files in your Cloud Storage bucket.
    1. Create the Hive external table chicago_taxi_trips_csv.
      gcloud dataproc jobs submit hive \
          --cluster presto-cluster \
          --region=${REGION} \
          --execute "
              CREATE EXTERNAL TABLE chicago_taxi_trips_csv(
                unique_key   STRING,
                taxi_id  STRING,
                trip_start_timestamp  TIMESTAMP,
                trip_end_timestamp  TIMESTAMP,
                trip_seconds  INT,
                trip_miles   FLOAT,
                pickup_census_tract  INT,
                dropoff_census_tract  INT,
                pickup_community_area  INT,
                dropoff_community_area  INT,
                fare  FLOAT,
                tips  FLOAT,
                tolls  FLOAT,
                extras  FLOAT,
                trip_total  FLOAT,
                payment_type  STRING,
                company  STRING,
                pickup_latitude  FLOAT,
                pickup_longitude  FLOAT,
                pickup_location  STRING,
                dropoff_latitude  FLOAT,
                dropoff_longitude  FLOAT,
                dropoff_location  STRING)
              ROW FORMAT DELIMITED
              FIELDS TERMINATED BY ','
              STORED AS TEXTFILE
              location 'gs://${BUCKET_NAME}/chicago_taxi_trips/csv/';"
      
    2. Verify the creation of the Hive external table.
      gcloud dataproc jobs submit hive \
          --cluster presto-cluster \
          --region=${REGION} \
          --execute "SELECT COUNT(*) FROM chicago_taxi_trips_csv;"
      
    3. Create another Hive external table chicago_taxi_trips_parquet with the same columns, but with data stored in Parquetformat for better query performance.
      gcloud dataproc jobs submit hive \
          --cluster presto-cluster \
          --region=${REGION} \
          --execute "
              CREATE EXTERNAL TABLE chicago_taxi_trips_parquet(
                unique_key   STRING,
                taxi_id  STRING,
                trip_start_timestamp  TIMESTAMP,
                trip_end_timestamp  TIMESTAMP,
                trip_seconds  INT,
                trip_miles   FLOAT,
                pickup_census_tract  INT,
                dropoff_census_tract  INT,
                pickup_community_area  INT,
                dropoff_community_area  INT,
                fare  FLOAT,
                tips  FLOAT,
                tolls  FLOAT,
                extras  FLOAT,
                trip_total  FLOAT,
                payment_type  STRING,
                company  STRING,
                pickup_latitude  FLOAT,
                pickup_longitude  FLOAT,
                pickup_location  STRING,
                dropoff_latitude  FLOAT,
                dropoff_longitude  FLOAT,
                dropoff_location  STRING)
              STORED AS PARQUET
              location 'gs://${BUCKET_NAME}/chicago_taxi_trips/parquet/';"
      
    4. Load the data from the Hive CSV table into the Hive Parquet table.
      gcloud dataproc jobs submit hive \
          --cluster presto-cluster \
          --region=${REGION} \
          --execute "
              INSERT OVERWRITE TABLE chicago_taxi_trips_parquet
              SELECT * FROM chicago_taxi_trips_csv;"
      
    5. Verify that the data loaded correctly.
      gcloud dataproc jobs submit hive \
          --cluster presto-cluster \
          --region=${REGION} \
          --execute "SELECT COUNT(*) FROM chicago_taxi_trips_parquet;"
      

Run queries

You can run queries locally from the Presto CLI or from an application.

Presto CLI queries

This section demonstrates how to query the Hive Parquet taxi dataset using the Presto CLI.

  1. Run the following command on your local machine to SSH into your cluster's master node. The local terminal will stop responding during the execution of the command.
    gcloud compute ssh presto-cluster-m
    
  2. In SSH terminal window on your cluster's master node, run the Presto CLI, which connects to the Presto server running on the master node.
    presto --catalog hive --schema default
    
  3. At the presto:default prompt, verify that Presto can find the Hive tables.
    show tables;
    
    Table
    ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
     chicago_taxi_trips_csv
     chicago_taxi_trips_parquet
    (2 rows)
    
  4. Run queries from the presto:default prompt, and compare the performance of querying Parquet versus CSV data.
    • Parquet data query
      select count(*) from chicago_taxi_trips_parquet where trip_miles > 50;
      
       _col0
      ‐‐‐‐‐‐‐‐
       117957
      (1 row)
      Query 20180928_171735_00006_2sz8c, FINISHED, 3 nodes Splits: 308 total, 308 done (100.00%) 0:16 [113M rows, 297MB] [6.91M rows/s, 18.2MB/s]
    • CSV data query
      select count(*) from chicago_taxi_trips_csv where trip_miles > 50;
      
      _col0
      ‐‐‐‐‐‐‐‐
       117957
      (1 row)
      Query 20180928_171936_00009_2sz8c, FINISHED, 3 nodes Splits: 881 total, 881 done (100.00%) 0:47 [113M rows, 41.5GB] [2.42M rows/s, 911MB/s]

Java application queries

To run queries from a Java application through the Presto Java JDBC driver:

  1. Download the Presto Java JDBC driver.

  2. Add a presto-jdbc dependency in Maven pom.xml.

    <dependency>
    <groupId>com.facebook.presto</groupId>
    <artifactId>presto-jdbc</artifactId>
    <version>0.206</version>
    </dependency>
    

Sample Java code

package dataproc.codelab.presto;

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

public class PrestoQuery {
  private static final String URL = "jdbc:presto://presto-cluster-m:8080/hive/default";
  private static final String SOCKS_PROXY = "localhost:1080";
  private static final String USER = "user";
  private static final String QUERY =
      "select count(*) as count from chicago_taxi_trips_parquet where trip_miles > 50";

  public static void main(String[] args) {
    try {
      Properties properties = new Properties();
      properties.setProperty("user", USER);
      properties.setProperty("socksProxy", SOCKS_PROXY);
      Connection connection = DriverManager.getConnection(URL, properties);
      try (Statement stmt = connection.createStatement()) {
        ResultSet rs = stmt.executeQuery(QUERY);
        while (rs.next()) {
          int count = rs.getInt("count");
          System.out.println("The number of long trips: " + count);
        }
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}

Logging and monitoring

Logging

The Presto logs are located at /var/log/presto/ on the cluster's master and worker nodes.

Web UI

See Viewing and Accessing Component Gateway URLs to open the Presto Web UI running on the cluster's master node in your local browser.

Monitoring

Presto exposes cluster runtime information through runtime tables. In a Presto session (from the presto:default) prompt, run the following query to view runtime table data:

select * FROM system.runtime.nodes;

Cleaning up

After you've finished the Use Presto with Cloud Dataproc tutorial, you can clean up the resources that you created on Google Cloud so they won't take up quota and you won't be billed for them in the future. The following sections describe how to delete or turn off these resources.

Deleting the project

The easiest way to eliminate billing is to delete the project that you created for the tutorial.

To delete the project:

  1. In the Cloud Console, go to the Manage resources page.

    Go to the Manage resources page

  2. In the project list, select the project that you want to delete and then click Delete .
  3. In the dialog, type the project ID and then click Shut down to delete the project.

Deleting the cluster

  • To delete your cluster:
    gcloud dataproc clusters delete --project=${PROJECT} presto-cluster \
        --region=${REGION}
    

Deleting the bucket

  • To delete the Cloud Storage bucket you created in Before you begin, including the data files stored in the bucket:
    gsutil -m rm -r gs://${BUCKET_NAME}