All Products
Search
Document Center

E-MapReduce:Read from and write to StarRocks

Last Updated:Jun 25, 2026

StarRocks provides an official Spark Connector to enable data transfer between Spark and StarRocks. You can add the necessary configurations in EMR Serverless Spark to connect to a StarRocks instance. This topic describes how to read from and write to StarRocks using EMR Serverless Spark.

Access methods

You can access StarRocks in two ways, depending on your needs:

Method 1: Task/session-level configuration

With this method, you configure StarRocks connection information—such as the JDBC URL, username, and password—in each task or session. This method is suitable for the following scenarios:

  • Temporary or ad hoc data access

  • Different tasks need to access different StarRocks clusters

  • You need fine-grained access control for each task

Method 2: Centralized configuration with Data Catalog

Note
  • Jobs submitted through Livy or Kyuubi cannot access the StarRocks Data Catalog.

  • Only the following engine versions are supported: esr-4.8.0 or later, and esr-5.2.0 or later.

Use the Data Catalog feature of EMR Serverless Spark to add a StarRocks Data Catalog. Once configured, all jobs and sessions in the workspace can access authorized data in the Data Catalog by default, eliminating the need for repeated configuration in each task. For more information, see Manage data catalogs.

This method is suitable for the following scenarios:

  • You need frequent access to StarRocks data

  • Multiple tasks share the same StarRocks access configuration

  • You want to simplify task configuration and improve development efficiency

Note

For workspaces that require long-term, frequent access to StarRocks, we recommend using the Data Catalog (Method 2) to reduce configuration overhead and improve development efficiency.

Prerequisites

Limitations

The Serverless Spark engine version must be esr-2.5.0, esr-3.1.0, esr-4.1.0, or later.

Procedure

Step 1: Get and upload the Spark Connector JAR

  1. Download the Spark Connector JAR for your engine version. For more information, see Read data from StarRocks using Spark connector.

    Note

    For engine versions esr-4.8.0 or later and esr-5.2.0 or later, the Spark Connector JAR is built-in. You can skip this step.

    For this example, we download a pre-compiled JAR from the Maven Central Repository.

    Note

    The Connector JAR file is named in the format starrocks-spark-connector-${spark_version}_${scala_version}-${connector_version}.jar. For example, if you use engine version esr-4.1.0 (Spark 3.5.2, Scala 2.12) and want to use connector version 1.1.2, select starrocks-spark-connector-3.5_2.12-1.1.2.jar.

  2. Upload the downloaded Spark Connector JAR to OSS. For more information, see Simple upload.

Step 2: Add a network connection

  1. Get network information.

    On the EMR Serverless StarRocks page, go to the Instance Details page of your target StarRocks instance to get the Virtual Private Cloud (VPC) and vSwitch information.

  2. Create a Network Connection.

    1. On the EMR Serverless Spark page, navigate to the Network Connection page for your Spark workspace and click Create Network Connection.

    2. In the Create Network Connection dialog box, enter a Name, select the VPC and vSwitch of the StarRocks instance, and then click OK.

      Note

      The Network Connection must match the StarRocks instance. Select a vSwitch that is in the same VPC as the StarRocks instance. If no vSwitch is available in the current availability zone, click vSwitch to go to the VPC console and create one. For more information, see VPCs and vSwitches.

Step 3: Create a database and table

  1. Connect to the StarRocks instance. For more information, see Connect to a StarRocks instance using EMR StarRocks Manager.

  2. On the SQL Editor Queries page, click File or the image icon on the right, and then click OK to create a new file.

  3. In the new file, enter the following SQL statements and click Run.

    CREATE DATABASE `testdb`;
    CREATE TABLE `testdb`.`score_board`
    (
        `id` int(11) NOT NULL COMMENT "",
        `name` varchar(65533) NULL DEFAULT "" COMMENT "",
        `score` int(11) NOT NULL DEFAULT "0" COMMENT ""
    )
    ENGINE=OLAP
    PRIMARY KEY(`id`)
    COMMENT "OLAP"
    DISTRIBUTED BY HASH(`id`);

Step 4: Read from and write to StarRocks

Method 1: SQL and Notebook sessions

For more information about session types, see Session Manager.

SQL session

  1. Write data to StarRocks by using EMR Serverless Spark.

    1. Create an SQL session. For more information, see Manage SQL sessions.

      When creating a session, select the engine version that corresponds to the Spark Connector version, select the network connection created in the previous step in Network Connection, and add the following parameters in Spark Configuration to load the Spark Connector.

      spark.emr.serverless.user.defined.jars  oss://<bucketname>/path/connector.jar

      Replace oss://<bucketname>/path/connector.jar with the OSS path of the Spark Connector that you uploaded in Step 1. For example, oss://emr-oss/spark/starrocks-spark-connector-3.5_2.12-1.1.2.jar.

    2. On the Development page, create a SparkSQL task. Then, select the SQL session you created from the upper-right corner.

      For more information, see Develop a SparkSQL job.

    3. Copy the following code to the new SparkSQL tab, replace the placeholder values as needed, and then click Run.

      CREATE TEMPORARY VIEW score_board
      USING starrocks
      OPTIONS
      (
        "starrocks.table.identifier" = "testdb.score_board",
        "starrocks.fe.http.url" = "<fe_host>:<fe_http_port>",
        "starrocks.fe.jdbc.url" = "jdbc:mysql://<fe_host>:<fe_query_port>",
        "starrocks.user" = "<user>",
        "starrocks.password" = "<password>"
      );
      INSERT INTO `score_board` VALUES (1, "starrocks", 100), (2, "spark", 100);

      Parameter description:

      • <fe_host>: The internal or public endpoint of the FE node in your EMR Serverless StarRocks instance. You can find this on the Instance Details page in the FE Details section.

        • If you use an internal endpoint, ensure that both services are in the same VPC.

        • If you use a public endpoint, ensure that the security group rules allow traffic on the required ports. For more information, see Network access and security settings.

      • <fe_http_port>: The HTTP port of the FE node in the EMR Serverless StarRocks instance. The default is 8030. You can find this on the Instance Details page in the FE Details section.

      • <fe_query_port>: The query port of the FE node in the EMR Serverless StarRocks instance. The default is 9030. You can find this on the Instance Details page in the FE Details section.

      • <user>: The username for the Serverless StarRocks instance. By default, an admin user with administrator privileges is provided. You can also add new users to connect by using the User Management page. For more information about how to add users, see Manage Users and Data Authorization.

      • <password>: The password that corresponds to the <user>.

  2. Query the written data by using EMR Serverless Spark.

    In this example, we create a temporary view named test_view in the Spark SQL task and use it to query data from score_board. Copy the following code into a new Spark SQL tab, select the code, and then click Run the selected.

    CREATE TEMPORARY VIEW test_view
    USING starrocks
    OPTIONS
    (
       "starrocks.table.identifier" = "testdb.score_board",
       "starrocks.fe.http.url" = "<fe_host>:<fe_http_port>",
       "starrocks.fe.jdbc.url" = "jdbc:mysql://<fe_host>:<fe_query_port>",
       "starrocks.user" = "<user>",
       "starrocks.password" = "<password>"
    );
    SELECT * FROM test_view;

    Output

    The query returns two records: (1, "starrocks", 100) and (2, "spark", 100).

Notebook session

  1. Write data to StarRocks by using EMR Serverless Spark.

    1. Create a Notebook session. For more information, see Manage Notebook sessions.

      When creating a session, select the engine version that corresponds to the Spark Connector version, select the network connection created in the previous step in Network Connection, and add the following parameters in Spark Configuration to load the Spark Connector.

      spark.emr.serverless.user.defined.jars  oss://<bucketname>/path/connector.jar

      Replace oss://<bucketname>/path/connector.jar with the OSS path of the Spark Connector that you uploaded in Step 1. For example, oss://emr-oss/spark/starrocks-spark-connector-3.5_2.12-1.1.2.jar.

    2. On the Development page, create an Interactive Development > Notebook task, and then select the created Notebook session in the upper-right corner.

      For more information, see Manage Notebook sessions.

    3. Copy the following code to a Python cell in the Notebook and click Run.

      # Replace the placeholders with your EMR Serverless StarRocks configurations.
      fe_host = "<fe_host>"
      fe_http_port = "<fe_http_port>"
      fe_query_port = "<fe_query_port>"
      user = "<user>"
      password = "<password>"
      # Create a view.
      create_table_sql = f"""
      CREATE TEMPORARY VIEW score_board
      USING starrocks
      OPTIONS (
        "starrocks.table.identifier" = "testdb.score_board",
        "starrocks.fe.http.url" = "{fe_host}:{fe_http_port}",
        "starrocks.fe.jdbc.url" = "jdbc:mysql://{fe_host}:{fe_query_port}",
        "starrocks.user" = "{user}",
        "starrocks.password" = "{password}"
      )
      """
      spark.sql(create_table_sql)
      # Insert data.
      insert_data_sql = """
      INSERT INTO `score_board` VALUES (1, "starrocks", 100), (2, "spark", 100)
      """
      spark.sql(insert_data_sql)
      

      Parameter description:

      • <fe_host>: The internal or public endpoint of the FE node in your EMR Serverless StarRocks instance. You can find this on the Instance Details page in the FE Details section.

        • If you use an internal endpoint, ensure that both services are in the same VPC.

        • If you use a public endpoint, ensure that the security group rules allow traffic on the required ports. For more information, see Network access and security settings.

      • <fe_http_port>: The HTTP port of the FE node in the EMR Serverless StarRocks instance. The default is 8030. You can find this on the Instance Details page in the FE Details section.

      • <fe_query_port>: The query port of the FE node in the EMR Serverless StarRocks instance. The default is 9030. You can find this on the Instance Details page in the FE Details section.

      • <user>: The username for the Serverless StarRocks instance. By default, an admin user with administrator privileges is provided. You can also add new users to connect by using the User Management page. For more information about how to add users, see Manage Users and Data Authorization.

      • <password>: The password that corresponds to the <user>.

  2. Query the written data by using EMR Serverless Spark.

    In a new Python cell, create a temporary view named test_view to query the score_board table. Copy the following code into the cell and click the run icon (image) to execute it.

    # Create a view.
    create_view_sql=f"""
    CREATE TEMPORARY VIEW test_view
    USING starrocks
    OPTIONS (
      "starrocks.table.identifier" = "testdb.score_board",
      "starrocks.fe.http.url" = "{fe_host}:{fe_http_port}",
      "starrocks.fe.jdbc.url" = "jdbc:mysql://{fe_host}:{fe_query_port}",
      "starrocks.user" = "{user}",
      "starrocks.password" = "{password}"
    )
    """
    spark.sql(create_view_sql)
    # Query the data.
    query_sql="SELECT * FROM test_view"
    result_df = spark.sql(query_sql)
    result_df.show()

    Output

    +---+---------+-----+
    | id|     name|score|
    +---+---------+-----+
    |  2|    spark|  100|
    |  1|starrocks|  100|
    +---+---------+-----+

Method 2: Spark batch job

  1. Create a Spark batch job.

    1. On the EMR Serverless Spark page, click Development in the left-side navigation pane.

    2. On the Development tab, click the image icon.

    3. In the Create dialog box, enter a Name, set the type to Application > SQL, and then click OK.

      You can adjust the type based on your needs. This topic uses SQL as an example. For more information about job types, see Develop an application.

  2. Read from and write to StarRocks by using the Spark batch job.

    1. In the upper-right corner of the new job development page, select a queue.

      For more information about how to add a queue, see Manage resource queues.

    2. On the new job development page, configure the following settings, leave the other parameters at their defaults, and then click Run.

      Parameter

      Description

      SQL File

      This example uses the spark_sql_starrocks.sql file, which contains the SQL statements from the SQL session. Before you use the file, download it, modify the configurations as needed, and then upload it on the Artifacts page.

      spark_sql_starrocks.sql parameters

      Parameter description:

      • <fe_host>: The internal or public endpoint of the FE node in your EMR Serverless StarRocks instance. You can find this on the Instance Details page in the FE Details section.

        • If you use an internal endpoint, ensure that both services are in the same VPC.

        • If you use a public endpoint, ensure that the security group rules allow traffic on the required ports. For more information, see Network access and security settings.

      • <fe_http_port>: The HTTP port of the FE node in the EMR Serverless StarRocks instance. The default is 8030. You can find this on the Instance Details page in the FE Details section.

      • <fe_query_port>: The query port of the FE node in the EMR Serverless StarRocks instance. The default is 9030. You can find this on the Instance Details page in the FE Details section.

      • <user>: The username for the Serverless StarRocks instance. By default, an admin user with administrator privileges is provided. You can also add new users to connect by using the User Management page. For more information about how to add users, see Manage Users and Data Authorization.

      • <password>: The password that corresponds to the <user>.

      Engine Version

      Select the engine version that matches the Spark Connector version.

      Normal Network Connection

      Select the Network Connection that you created in the previous step.

      Spark Configuration

      Add the following parameter in the Spark Configuration section to load the Spark Connector.

      spark.emr.serverless.user.defined.jars  oss://<bucketname>/path/connector.jar

      Replace oss://<bucketname>/path/connector.jar with the OSS path of the Spark Connector that you uploaded in Step 1. For example, oss://emr-oss/spark/starrocks-spark-connector-3.5_2.12-1.1.2.jar.

  3. View log information.

    1. In the Execution Records section at the bottom, click Details in the Actions column.

    2. Click the Log Exploration tab to view the log information for the job.

      Select Driver Log > Stdout to view the stdout.log file. If the job is successful, the log output contains starrocks 100 and spark 100. This confirms that data was written to and then read from StarRocks successfully.

References

For more information, see the official StarRocks documentation: