All Products
Search
Document Center

E-MapReduce:Manage Spark Thrift Server sessions

Last Updated:Oct 10, 2026

Spark Thrift Server is a service provided by Apache Spark. It supports connections and SQL query execution through Java Database Connectivity (JDBC) or Open Database Connectivity (ODBC). This makes it easy to integrate your Spark environment with existing business intelligence (BI) tools, data visualization tools, and other data analytics tools. You can create a Spark Thrift Server session and connect to it from various clients.

Prerequisites

You have created a workspace. For more information, see Manage workspaces.

Create a Spark Thrift Server session

After you create a Spark Thrift Server session, you can select it when creating a Spark SQL node.

  1. Go to the Sessions page.

    1. Log on to the EMR console.

    2. In the left-side navigation pane, choose EMR Serverless > Spark.

    3. On the Spark page, click the name of the target workspace.

    4. On the EMR Serverless Spark page, click Sessions in the left-side navigation pane.

  2. On the Sessions page, click the Spark Thrift Server Session tab.

  3. Click Create Spark Thrift Server Session.

  4. On the Create Spark Thrift Server Session page, configure the parameters and click Create.

    Parameter

    Description

    Name

    The name of the new Spark Thrift Server.

    The name must be 1 to 64 characters in length and can contain letters, digits, hyphens (-), underscores (_), and spaces.

    Resource Queue

    Select a development queue or a queue shared by development and production environments to deploy the session.

    For more information about queues, see Manage resource queues.

    Engine Version

    The engine version for this session. For more information, see Engine versions.

    Use Fusion Acceleration

    Fusion can accelerate Spark workloads and reduce the total cost of jobs. For billing information, see Product Billing. For more information about the Fusion engine, see Fusion engine.

    Automatic Stop

    Enabled by default. The Spark Thrift Server session automatically stops after 45 minutes of inactivity.

    Normal Network Connection

    An existing network connection for accessing data sources in a VPC or external services. For more information, see Network connectivity between EMR Serverless Spark and other VPCs.

    Spark Thrift Server Port

    The port number is 443 for access over the public endpoint and 80 for access over the internal same-region endpoint.

    Authentication Method

    Only the token method is supported.

    spark.driver.cores

    The number of CPU cores for the driver process. Default value: 1.

    spark.driver.memory

    The memory allocated to the driver process. Default value: 3.5 GB.

    spark.executor.cores

    The number of CPU cores for each executor. Default value: 1.

    spark.executor.memory

    The memory allocated to each executor. Default value: 3.5 GB.

    spark.executor.instances

    The number of executors to allocate. Default value: 2.

    Dynamic Resource Allocation

    Disabled by default. When enabled, configure the following parameters:

    • Minimum Number of Executors: The default value is 2.

    • Maximum Number of Executors: If spark.executor.instances is not set, the default value is 10.

    More Memory Configurations

    • spark.driver.memoryOverhead: The non-heap memory available for the driver. If this parameter is not set, Spark automatically allocates a value based on the default, which is max(384 MB, 10% * spark.driver.memory).

    • spark.executor.memoryOverhead: The non-heap memory available for each executor. If this parameter is not set, Spark automatically allocates a value based on the default, which is max(384 MB, 10% * spark.executor.memory).

    • spark.memory.offHeap.size: The amount of off-heap memory available to Spark. The default value is 1 GB.

      This parameter takes effect only when spark.memory.offHeap.enabled is set to true. When the Fusion engine is used, this feature is enabled by default with 1 GB of off-heap memory.

    Spark Configuration

    Enter Spark configuration information. By default, parameters are separated by spaces. For example, spark.sql.catalog.paimon.metastore dlf.

  5. Obtain the endpoint information.

    1. On the Spark Thrift Server Session tab, click the name of the Spark Thrift Server that you created.

    2. On the Overview tab, copy the endpoint information.

      Choose an endpoint type based on your network environment:

      • Public Endpoint: Use this to access EMR Serverless Spark from a local machine, an external network, or a cross-cloud environment. Traffic fees may apply. Ensure that you take appropriate security measures.

      • Internal Endpoint: Use this to access EMR Serverless Spark from an Alibaba Cloud ECS instance in the same region. Internal access is free and more secure, but is limited to the same-region Alibaba Cloud internal network.

Create a token

  1. On the Spark Thrift Server Session tab, click the name of the Spark Thrift Server session that you created.

  2. Click the Token Management tab.

  3. Click Create Token.

  4. In the Create Token dialog box, configure the parameters and click OK.

    Parameter

    Description

    Name

    The name of the new token.

    Expired At

    The expiration time for the token. The value must be at least 1 day. Enabled by default with a 365-day expiration period.

  5. Copy the token information.

    Important

    You must copy the token information immediately after it is created because it cannot be viewed again. If your token expires or is lost, you must create or reset the token.

Connect to Spark Thrift Server

When you connect to Spark Thrift Server, replace the following information as needed:

  • <endpoint>: The Endpoint(Public) or Endpoint(Private) that you obtained on the Overview tab.

    If you use the internal endpoint, access to Spark Thrift Server is limited to resources within the same VPC.

  • <port>: The port number. The port number is 443 for access over the public endpoint and 80 for access over the internal same-region endpoint.

  • <username>: The name of the token that you created on the Token Management tab.

  • <token>: The token information that you copied on the Token Management tab.

Connect to Spark Thrift Server using Python

  1. Run the following command to install the PyHive and Thrift packages.

    pip install pyhive thrift
  2. Write a Python script to connect to Spark Thrift Server.

    The following script connects to Hive and lists all databases. Choose a connection method based on your network environment.

    Connect using a public endpoint

    from pyhive import hive
    
    if __name__ == '__main__':
        # Replace <endpoint>, <username>, and <token> with your actual information.
        cursor = hive.connect('<endpoint>', port=443, scheme='https', username='<username>', password='<token>').cursor()
        cursor.execute('show databases')
        print(cursor.fetchall())
        cursor.close()

    Connect using an internal same-region endpoint

    from pyhive import hive
    
    if __name__ == '__main__':
        # Replace <endpoint>, <username>, and <token> with your actual information.
        cursor = hive.connect('<endpoint>', port=80, scheme='http', username='<username>', password='<token>').cursor()
        cursor.execute('show databases')
        print(cursor.fetchall())
        cursor.close()

Connect to Spark Thrift Server using Java

  1. Add the following Maven dependencies to your pom.xml file.

    <dependencies>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-common</artifactId>
                <version>3.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-jdbc</artifactId>
                <version>2.1.0</version>
            </dependency>
        </dependencies>
    
    Note

    The built-in Hive version for Serverless Spark is 2.x. Therefore, only hive-jdbc 2.x is supported.

  2. Write Java code to connect to Spark Thrift Server.

    The following code connects to Spark Thrift Server and queries the list of databases.

    Connect using a public endpoint

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import org.apache.hive.jdbc.HiveStatement;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            String url = "jdbc:hive2://<endpoint>:443/;transportMode=http;httpPath=cliservice/token/<token>";
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            Connection conn = DriverManager.getConnection(url);
            HiveStatement stmt = (HiveStatement) conn.createStatement();
    
            String sql = "show databases";
            System.out.println("Running " + sql);
            ResultSet res = stmt.executeQuery(sql);
    
            ResultSetMetaData md = res.getMetaData();
            String[] columns = new String[md.getColumnCount()];
            for (int i = 0; i < columns.length; i++) {
                columns[i] = md.getColumnName(i + 1);
            }
            while (res.next()) {
                System.out.print("Row " + res.getRow() + "=[");
                for (int i = 0; i < columns.length; i++) {
                    if (i != 0) {
                        System.out.print(", ");
                    }
                    System.out.print(columns[i] + "='" + res.getObject(i + 1) + "'");
                }
                System.out.println(")]");
            }
            
            conn.close();
        }
    }

    Connect using an internal same-region endpoint

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import org.apache.hive.jdbc.HiveStatement;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            String url = "jdbc:hive2://<endpoint>:80/;transportMode=http;httpPath=cliservice/token/<token>";
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            Connection conn = DriverManager.getConnection(url);
            HiveStatement stmt = (HiveStatement) conn.createStatement();
    
            String sql = "show databases";
            System.out.println("Running " + sql);
            ResultSet res = stmt.executeQuery(sql);
    
            ResultSetMetaData md = res.getMetaData();
            String[] columns = new String[md.getColumnCount()];
            for (int i = 0; i < columns.length; i++) {
                columns[i] = md.getColumnName(i + 1);
            }
            while (res.next()) {
                System.out.print("Row " + res.getRow() + "=[");
                for (int i = 0; i < columns.length; i++) {
                    if (i != 0) {
                        System.out.print(", ");
                    }
                    System.out.print(columns[i] + "='" + res.getObject(i + 1) + "'");
                }
                System.out.println(")]");
            }
            
            conn.close();
        }
    }

Connect to Spark Thrift Server using Spark Beeline

  • If you are using a self-managed cluster, first go to the bin directory of Spark, and then use Beeline to connect to Spark Thrift Server.

    Connect using a public endpoint

    cd /opt/apps/SPARK3/spark-3.4.2-hadoop3.2-1.0.3/bin/
    
    ./beeline -u "jdbc:hive2://<endpoint>:443/;transportMode=http;httpPath=cliservice/token/<token>"

    Connect using an internal same-region endpoint

    cd /opt/apps/SPARK3/spark-3.4.2-hadoop3.2-1.0.3/bin/
    
    ./beeline -u "jdbc:hive2://<endpoint>:80/;transportMode=http;httpPath=cliservice/token/<token>"
    Note

    The path /opt/apps/SPARK3/spark-3.4.2-hadoop3.2-1.0.3 in the code is an example of the Spark installation path on an EMR on ECS cluster. Adjust the path based on the actual Spark installation path on your client. If you are unsure of the Spark installation path, you can find it by running the env | grep SPARK_HOME command.

  • If you are using an EMR on ECS cluster, you can directly use the Spark Beeline client to connect to Spark Thrift Server.

    Connect using a public endpoint

    spark-beeline -u "jdbc:hive2://<endpoint>:443/;transportMode=http;httpPath=cliservice/token/<token>"

    Connect using an internal same-region endpoint

    spark-beeline -u "jdbc:hive2://<endpoint>:80/;transportMode=http;httpPath=cliservice/token/<token>"

If the following error occurs when you use Hive Beeline to connect to Serverless Spark Thrift Server, the cause is usually an incompatibility between the Hive Beeline version and Spark Thrift Server. To resolve this, use a Hive 2.x version of Beeline.

24/08/22 15:09:11 [main]: ERROR jdbc.HiveConnection: Error opening session
org.apache.thrift.transport.TTransportException: HTTP Response code: 404

Configure Apache Superset to connect to Spark Thrift Server

Apache Superset is a data exploration and visualization platform that supports a wide range of chart types. For more information, see the Superset documentation.

  1. Install dependencies.

    Make sure that you have installed version 0.20.0 of the thrift package. If not, you can run the following command to install it.

    pip install thrift==0.20.0
  2. Start Superset and go to the Superset interface.

    For more information about how to start Superset, see the Superset documentation.

  3. In the upper-right corner of the page, click DATABASE to go to the Connect a database page.

  4. On the Connect a database page, select Apache Spark SQL.

    image

  5. Enter the connection string and configure the data source parameters.

    Connect using a public endpoint

    hive+https://<username>:<token>@<endpoint>:443/<db_name>

    Connect using an internal same-region endpoint

    hive+http://<username>:<token>@<endpoint>:80/<db_name>
  6. Click FINISH to confirm the connection and validation.

Configure Hue to connect to Spark Thrift Server

Hue is an open source web interface for interacting with the Hadoop ecosystem. For more information, see the Hue documentation.

  1. Install dependencies.

    Make sure that you have installed version 0.20.0 of the thrift package. If not, you can run the following command to install it.

    pip install thrift==0.20.0
  2. Add the Spark SQL connection string to the Hue configuration file.

    Find the Hue configuration file (usually located at /etc/hue/hue.conf), and add the following content to the file.

    Connect using a public endpoint

    [[[sparksql]]]
         name = Spark Sql
         interface=sqlalchemy
         options='{"url": "hive+https://<username>:<token>@<endpoint>:443/"}'

    Connect using an internal same-region endpoint

    [[[sparksql]]]
         name = Spark Sql
         interface=sqlalchemy
         options='{"url": "hive+http://<username>:<token>@<endpoint>:80/"}'
  3. Restart Hue.

    After modifying the configuration, you must run the following command to restart the Hue service for the changes to take effect.

    sudo service hue restart
  4. Verify the connection.

    After a successful restart, access the Hue interface and find the Spark SQL option. If the configuration is correct, you can connect to Spark Thrift Server and run SQL queries.

    image

Connect to Spark Thrift Server using DataGrip

DataGrip is a database management tool for querying, creating, and managing databases on local, remote, or cloud environments. For more information, see the DataGrip website.

  1. Install DataGrip. For more information, see Install DataGrip.

    The DataGrip version used in this example is 2025.1.2.

  2. Open the DataGrip client. The DataGrip interface is displayed.

  3. Create a project.

    1. Click image and choose New.

      image

    2. In the New Project dialog box, enter a project name, such as Spark, and click OK.

  4. Click the Create Connection icon on the Database Explorer menu bar. Select Data Source > Other > Apache Spark.

    image

  5. In the Data Sources and Drivers dialog box, configure the following parameters.

    image

    Tab

    Parameter

    Description

    General

    Name

    A custom connection name. For example, spark_thrift_server.

    Authentication

    Select an authentication method. In this topic, No auth is selected.

    In a production environment, select User & Password to ensure that only authorized users can submit SQL jobs, which improves system security.

    Driver

    Click Apache Spark, and then click Go to Driver to confirm that the driver version is ver. 1.2.2.

    Note

    Because the current Serverless Spark engine version is 3.x, you must select driver version 1.2.2 to ensure system stability and feature compatibility.

    image

    URL

    The URL for connecting to Spark Thrift Server. Choose a connection method based on your network environment.

    • Connect using a public endpoint

      jdbc:hive2://<endpoint>:443/;transportMode=http;httpPath=cliservice/token/<token>
    • Connect using an internal same-region endpoint

      jdbc:hive2://<endpoint>:80/;transportMode=http;httpPath=cliservice/token/<token>

    Options

    Run keep-alive query

    Optional. Enable this to prevent automatic disconnection due to inactivity timeout.

  6. Click Test Connection to verify the connection.

    image

  7. Click OK to complete the configuration.

  8. You can now manage Spark Thrift Server using DataGrip.

    After DataGrip is connected to Spark Thrift Server, you can perform data development. For more information, see the DataGrip help documentation.

    For example, under the created connection, right-click the target table, choose New, and then write and run an SQL script in the SQL editor that opens to view the table data.

    image

Connect to Spark Thrift Server using Redash

Redash is an open source BI tool for web-based database querying and data visualization. For more information, see the Redash documentation.

  1. Install Redash. For more information, see the official Redash documentation.

  2. Install dependencies.

    Make sure that you have installed version 0.20.0 of the thrift package. If not, you can run the following command to install it.

    pip install thrift==0.20.0
  3. Log on to Redash.

  4. In the navigation pane on the left, click Settings, and on the Data Sources tab, click +New Data Source.

  5. In the dialog box that appears, configure the parameters, and then click Create.

    image

    Parameter

    Description

    Type Selection

    The data source type. In the search box, find and select Hive (HTTP).

    Configuration

    Name

    The data source name. You can customize the name.

    Host

    The endpoint of Spark Thrift Server.

    You can obtain the Endpoint(Public) or Endpoint(Private) on the Overview tab.

    Port

    • If you access over the public endpoint, the port number is 443.

    • If you access over the internal same-region endpoint, the port number is 80.

    HTTP Path

    Set this to /cliservice.

    Username

    The username. You can enter any name, such as root.

    Password

    Enter the token information that you created.

    HTTP Scheme

    • If you access over the public endpoint, set this to https.

    • For an internal same-region endpoint, enter http.

  6. At the top of the page, choose . You can write SQL statements in the editor.

    image

Connect to Spark Thrift Server using dbt

dbt (data build tool) enables data analysts and engineers to write SQL-based transformation logic and manage deployments with software engineering best practices such as versioning and testing. For more information, see the dbt documentation.

  1. Install dbt.

    pip install dbt-spark

    To use the Hive connector, you can also install:

    pip install dbt-spark[HIVE]
  2. Create a dbt project.

    dbt init my_spark_project
    cd my_spark_project
  3. Configure the dbt profile.

    Configure the Spark Thrift Server connection information in the ~/.dbt/profiles.yml file:

    Connect using a public endpoint

    my_spark_project:
      target: dev
      outputs:
        dev:
          type: spark
          method: thrift
          host: <endpoint>
          port: 443
          user: <username>
          password: <token>
          schema: default
          connect_retries: 5
          connect_timeout: 60
          retry_all: true
          use_ssl: true
          server_side_parameters:
            "hive.exec.dynamic.partition": "true"
            "hive.exec.dynamic.partition.mode": "nonstrict"

    Connect using an internal same-region endpoint

    my_spark_project:
      target: dev
      outputs:
        dev:
          type: spark
          method: thrift
          host: <endpoint>
          port: 80
          user: <username>
          password: <token>
          schema: default
          connect_retries: 5
          connect_timeout: 60
          retry_all: true
          use_ssl: false
          server_side_parameters:
            "hive.exec.dynamic.partition": "true"
            "hive.exec.dynamic.partition.mode": "nonstrict"
  4. Test the connection.

    dbt debug

    If the connection is successful, you will see output similar to the following:

    Connection test: [OK connection ok]
  5. Create a dbt model.

    Create an SQL file in the models/ directory, such as models/example_model.sql:

    {{ config(materialized='table') }} select col1, col2, current_timestamp() as created_at from {{ ref('source_table') }} where col1 is not null
  6. Run the dbt project.

    # Run all models
    dbt run
    
    # Run a specific model
    dbt run --models example_model
    
    # Run tests
    dbt test
    
    # Generate documentation
    dbt docs generate
    dbt docs serve
  7. Configure source tables and tests.

    Define source tables and tests in the models/schema.yml file:

    version: 2
    
    sources:
      - name: raw_data
        description: "Raw data from source systems"
        tables:
          - name: source_table
            description: "Source table containing raw data"
            columns:
              - name: col1
                description: "Primary identifier"
                tests:
                  - not_null
                  - unique
              - name: col2
                description: "Data column"
    
    models:
      - name: example_model
        description: "Transformed data model"
        columns:
          - name: col1
            description: "Primary identifier"
            tests:
              - not_null
              - unique
          - name: col2
            description: "Processed data column"
          - name: created_at
            description: "Record creation timestamp"

Notes:

  • Make sure that your dbt version is compatible with Spark Thrift Server. Use dbt-spark 1.3.0 or later.

  • In a production environment, configure sensitive information, such as tokens, using environment variables instead of writing them directly in the configuration file.

  • If a connection timeout occurs, adjust the connect_timeout and connect_retries parameters.

  • For large datasets, use an incremental data model to improve performance:

{{ config(
    materialized='incremental',
    unique_key='id',
    incremental_strategy='merge'
) }} select * from source_table {% if is_incremental() %} where updated_at > (select max(updated_at) from {{ this }}) {% endif %}

After you complete these configurations, you can connect to Spark Thrift Server using dbt and use its powerful features for data transformation and management.

View execution records

After a job completes, you can view its execution history.

  1. On the SQL Sessions page, click the name of the desired session.

  2. Click the Execution Records tab.

    On this tab, you can view details for each execution, such as the run ID, start time, and a link to the Spark UI.

    image