Skip to main content
The database provides a self-developed connector named StarRocks Connector for Apache Spark™ (Spark connector for short) to help you read data from a table by using Spark. You can use Spark for complex processing and machine learning on the data you have read from the database. The Spark connector supports three reading methods: Spark SQL, Spark DataFrame, and Spark RDD. You can use Spark SQL to create a temporary view on the table, and then directly read data from the table by using that temporary view. You can also map the table to a Spark DataFrame or a Spark RDD, and then read data from the Spark DataFrame or Spark RDD. We recommend the use of a Spark DataFrame.
NOTICE Only users with the SELECT privilege on a table can read data from this table. You can follow the instructions provided in GRANT to grant the privilege to a user.

Usage notes

  • You can filter data on the database before you read the data, thereby reducing the amount of data transferred.
  • If the overhead of reading data is substantial, you can employ appropriate table design and filter conditions to prevent Spark from reading an excessive amount of data at a time. As such, you can reduce I/O pressure on your disk and network connection, thereby ensuring routine queries can be run properly.

Version requirements

NOTICE
  • Please see Upgrade Spark connector for behavior changes among different connector versions.
  • The connector does not provide MySQL JDBC driver since version 1.1.1, and you need import the driver to the Spark classpath manually. You can find the driver on Maven Central.

Obtain Spark connector

Use one of the following methods to obtain the Spark connector .jar package that suits your business needs:
  • Download a compiled package.
  • Use Maven to add the dependencies required by the Spark connector. (This method is supported only for Spark connector 1.1.0 and later.)
  • Manually compile a package.

Spark connector 1.1.0 and later

Spark connector .jar packages are named in the following format: starrocks-spark-connector-${spark_version}_${scala_version}-${connector_version}.jar For example, if you want to use Spark connector 1.1.0 with Spark 3.2 and Scala 2.12, you can choose starrocks-spark-connector-3.2_2.12-1.1.0.jar.
NOTICE In normal cases, the latest Spark connector version can be used with the most recent three Spark versions.

Download a compiled package

You can obtain Spark connector .jar packages of various versions at Maven Central Repository.

Add Maven dependencies

Configure the dependencies required by the Spark connector as follows:
NOTICE You must replace spark_version, scala_version, and connector_version with the Spark version, Scala version, and Spark connector version you use.
For example, if you want to use Spark connector 1.1.0 with Spark 3.2 and Scala 2.12, configure the dependencies as follows:

Manually compile a package

  1. Download the Spark connector code.
  2. Use the following command to compile the Spark connector:
    NOTICE You must replace spark_version with the Spark version you use.
    For example, if you want to use the Spark connector with Spark 3.2, compile the Spark connector as follows:
  3. Go to the target/ path, in which a Spark connector .jar package like starrocks-spark-connector-3.2_2.12-1.1.0-SNAPSHOT.jar is generated upon compilation.
    NOTICE If you are using a Spark connector version that is not officially released, the name of the generated Spark connector .jar package contains SNAPSHOT as a suffix.

Spark connector 1.0.0

Download a compiled package

Manually compile a package

  1. Download the Spark connector code.
    NOTICE You must switch to spark-1.0.
  2. Take one of the following actions to compile the Spark connector:
    • If you are using Spark 2.x, run the following command, which compiles the Spark connector to suit Spark 2.3.4 by default:
    • If you are using Spark 3.x, run the following command, which compiles the Spark connector to suit Spark 3.1.2 by default:
  3. Go to the output/ path, in which the starrocks-spark2_2.11-1.0.0.jar file is generated upon compilation. Then, copy the file to the classpath of Spark:
    • If your Spark cluster runs in Local mode, place the file into the jars/ path.
    • If your Spark cluster runs in Yarn mode, place the file into the pre-deployment package.
You can use the Spark connector to read data from the database only after you place the file into the specified location.

Parameters

This section describes the parameters you need to configure when you use the Spark connector to read data from the database.

Common parameters

The following parameters apply to all three reading methods: Spark SQL, Spark DataFrame, and Spark RDD.

Parameters for Spark SQL and Spark DataFrame

The following parameters apply only to the Spark SQL and Spark DataFrame reading methods.

Parameters for Spark RDD

The following parameters apply only to the Spark RDD reading method.

Data type mapping between the database and Spark

Spark connector 1.1.0 and later

Spark connector 1.0.0

The processing logic of the underlying storage engine used by the database cannot cover an expected time range when DATE and DATETIME data types are directly used. Therefore, the Spark connector maps the DATE and DATETIME data types from the database to the STRING data type from Spark, and generates readable string texts matching the date and time data read from the database.

Upgrade Spark connector

Upgrade from version 1.0.0 to version 1.1.0

  • Since 1.1.1, the Spark connector does not provide mysql-connector-java which is the official JDBC driver for MySQL, because of the limitations of the GPL license used by mysql-connector-java. However, the Spark connector still needs the mysql-connector-java to connect to the database for the table metadata, so you need to add the driver to the Spark classpath manually. You can find the driver on MySQL site or Maven Central.
  • In version 1.1.0, the Spark connector uses JDBC to access the database to obtain more detailed table information. Therefore, you must configure starrocks.fe.jdbc.url.
  • In version 1.1.0, some parameters are renamed. Both the old and new parameters are retained for now. For each pair of equivalent parameters, you only need to configure one of them, but we recommend that you use the new one because the old one may be deprecated.
    • starrocks.fenodes is renamed as starrocks.fe.http.url.
    • user is renamed as starrocks.user.
    • password is renamed as starrocks.password.
  • In version 1.1.0, the mappings of some data types are adjusted based on Spark 3.x:
    • DATE in the database is mapped to DataTypes.DateType (originally DataTypes.StringType) in Spark.
    • DATETIME in the database is mapped to DataTypes.TimestampType (originally DataTypes.StringType) in Spark.

Examples

The following examples assume you have created a database named test in your cluster and you have the permissions of user root. The parameter settings in the examples are based on Spark Connector 1.1.0.

Network configuration

Ensure that the machine where Spark is located can access the FE nodes of the cluster via the http_port (default: 8030) and query_port (default: 9030), and the BE nodes via the be_port (default: 9060).

Data example

Do as follows to prepare a sample table:
  1. Go to the test database and create a table named score_board.
  2. Insert data into the score_board table.
  3. Query the score_board table.

Read data using Spark SQL

  1. Run the following command in the Spark directory to start Spark SQL:
  2. Run the following command to create a temporary view named spark_starrocks on the score_board table which belongs to the test database:
  3. Run the following command to read data from the temporary view:
    Spark returns the following data:

Read data using Spark DataFrame

  1. Run the following command in the Spark directory to start Spark Shell:
  2. Run the following command to create a DataFrame named starrocksSparkDF on the score_board table which belongs to the test database:
  3. Read data from the DataFrame. For example, if you want to read the first 10 rows, run the following command:
    Spark returns the following data:
    NOTE By default, if you do not specify the number of rows you want to read, Spark returns the first 20 rows.

Read data using Spark RDD

  1. Run the following command in the Spark directory to start Spark Shell:
  2. Run the following command to create an RDD named starrocksSparkRDD on the score_board table which belongs to the test database.
  3. Read data from the RDD. For example, if you want to read the first 10 elements, run the following command:
    Spark returns the following data:
    To read the entire RDD, run the following command:
    Spark returns the following data:

Read data from a specific warehouse

On a shared-data cluster with more than one warehouse, a Spark read runs on default_warehouse unless you set starrocks.request.warehouse (supported since 1.1.5). Set it to keep large reads away from the warehouse that serves your loads or queries. The FE then plans the read on the warehouse you name, and the Spark executors read the data from the compute nodes of that warehouse, so they must be able to reach those nodes on the BE Thrift port (9060 by default).
  • Spark SQL: add "starrocks.request.warehouse" = "<warehouse_name>" to OPTIONS.
  • Spark DataFrame:
  • Spark RDD: add "starrocks.request.warehouse" -> "<warehouse_name>" to cfg.

Best practices

When you read data from the database using the Spark connector, you can use the starrocks.filter.query parameter to specify filter conditions based on which Spark prunes partitions, buckets, and prefix indexes to reduce the cost of data pulling. This section uses Spark DataFrame as an example to show how this is achieved.

Environment setup

Data example

Do as follows to prepare a sample table:
  1. Go to the test database and create a table named mytable.
  2. Insert data into mytable.
  3. Query the mytable table.

Full table scan

  1. Run the following command in the Spark directory to create a DataFrame named df on mytable table which belongs to the test database:
  2. View the FE log file fe.log of your cluster, and find the SQL statement executed to read data. Example:
  3. In the test database, use EXPLAIN to obtain the execution plan of the SELECT k,b,dt,v from test.mytable statement:
In this example, no pruning is performed. Therefore, Spark scans all of the three partitions (as suggested by partitions=3/3) that hold data, and scans all of the 9 tablets (as suggested by tabletRatio=9/9) in those three partitions.

Partition pruning

  1. Run the following command, in which you use the starrocks.filter.query parameter to specify a filter condition dt='2022-01-02 08:00:00 for partition pruning, in the Spark directory to create a DataFrame named df on the mytable table which belongs to the test database:
  2. View the FE log file fe.log of your cluster, and find the SQL statement executed to read data. Example:
  3. In the test database, use EXPLAIN to obtain the execution plan of the SELECT k,b,dt,v from test.mytable where dt=‘2022-01-02 08:00:00’ statement:
In this example, only partition pruning is performed, whereas bucket pruning is not. Therefore, Spark scans one of the three partitions (as suggested by partitions=1/3) and all of the tablets (as suggested by tabletRatio=3/3) in that partition.

Bucket pruning

  1. Run the following command, in which you use the starrocks.filter.query parameter to specify a filter condition k=1 for bucket pruning, in the Spark directory to create a DataFrame named df on the mytable table which belongs to the test database:
  2. View the FE log file fe.log of your cluster, and find the SQL statement executed to read data. Example:
  3. In the test database, use EXPLAIN to obtain the execution plan of the SELECT k,b,dt,v from test.mytable where k=1 statement:
In this example, only bucket pruning is performed, whereas partition pruning is not. Therefore, Spark scans all of the three partitions (as suggested by partitions=3/3) that hold data, and scans all of the three tablets (as suggested by tabletRatio=3/9) to retrieve Hash values that meet the k = 1 filter condition within those three partitions.

Partition pruning and bucket pruning

  1. Run the following command, in which you use the starrocks.filter.query parameter to specify two filter conditions k=7 and dt='2022-01-02 08:00:00' for bucket pruning and partition pruning, in the Spark directory to create a DataFrame named df on the mytable table on the test database:
  2. View the FE log file fe.log of your cluster, and find the SQL statement executed to read data. Example:
  3. In the test database, use EXPLAIN to obtain the execution plan of the SELECT k,b,dt,v from test.mytable where k=7 and dt=‘2022-01-02 08:00:00’ statement:
In this example, both partition pruning and bucket pruning are performed. Therefore, Spark scans only one of the three partitions (as suggested by partitions=1/3) and only one tablet (as suggested by tabletRatio=1/3) in that partition.

Prefix index filtering

  1. Insert more data records into a partition of the mytable table which belongs to the test database:
  2. Query the mytable table:
  3. Run the following command, in which you use the starrocks.filter.query parameter to specify a filter condition k=1 for prefix index filtering, in the Spark directory to create a DataFrame named df on the mytable table which belongs to the test database:
  4. In the test database, set is_report_success to true to enable profile reporting:
  5. Use a browser to open the http://<fe_host>:<http_http_port>/query page, and view the profile of the SELECT * FROM mytable where k=1 statement. Example:
In this example, the filter condition k = 1 can hit the prefix index. Therefore, Spark can filter out three rows (as suggested by ShortKeyFilterRows: 3).

Read nested columns (STRUCT, ARRAY, and MAP)

Reading nested columns is supported since version 1.1.3. The Spark connector supports reading the database columns of type STRUCT, ARRAY, and MAP. Because the connector cannot automatically infer the full nested type from the database schema, you must declare the column types explicitly using the starrocks.column.types option for every nested column.

Nested data type mapping

Nested types can be composed arbitrarily (for example, STRUCT<a ARRAY<INT>, b MAP<STRING, BIGINT>>).

Type-mapping caveats inside nested types

Logical the database types that share an Arrow wire representation (notably DATE, DATETIME, and some DECIMAL encodings) are decoded correctly only when the declared type is provided via starrocks.column.types. Without it, those fields fall back to plain STRING.

Example

Given the following table:
Read the table with Spark:

Current limitations

  • Nested type inference is not automatic. You must supply starrocks.column.types for every nested column.
  • DATE and DATETIME fields inside nested types are returned as Spark DateType / TimestampType only when the declared type is provided. Without the declaration they fall back to StringType.