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 replacespark_version,scala_version, andconnector_versionwith the Spark version, Scala version, and Spark connector version you use.
Manually compile a package
- Download the Spark connector code.
-
Use the following command to compile the Spark connector:
NOTICE You must replace
spark_versionwith 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: -
Go to the
target/path, in which a Spark connector .jar package likestarrocks-spark-connector-3.2_2.12-1.1.0-SNAPSHOT.jaris 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
SNAPSHOTas a suffix.
Spark connector 1.0.0
Download a compiled package
Manually compile a package
-
Download the Spark connector code.
NOTICE You must switch to
spark-1.0. -
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:
-
If you are using Spark 2.x, run the following command, which compiles the Spark connector to suit Spark 2.3.4 by default:
-
Go to the
output/path, in which thestarrocks-spark2_2.11-1.0.0.jarfile is generated upon compilation. Then, copy the file to the classpath of Spark:- If your Spark cluster runs in
Localmode, place the file into thejars/path. - If your Spark cluster runs in
Yarnmode, place the file into the pre-deployment package.
- If your Spark cluster runs in
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-javawhich is the official JDBC driver for MySQL, because of the limitations of the GPL license used bymysql-connector-java. However, the Spark connector still needs themysql-connector-javato 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.fenodesis renamed asstarrocks.fe.http.url.useris renamed asstarrocks.user.passwordis renamed asstarrocks.password.
-
In version 1.1.0, the mappings of some data types are adjusted based on Spark 3.x:
DATEin the database is mapped toDataTypes.DateType(originallyDataTypes.StringType) in Spark.DATETIMEin the database is mapped toDataTypes.TimestampType(originallyDataTypes.StringType) in Spark.
Examples
The following examples assume you have created a database namedtest 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 thehttp_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:-
Go to the
testdatabase and create a table namedscore_board. -
Insert data into the
score_boardtable. -
Query the
score_boardtable.
Read data using Spark SQL
-
Run the following command in the Spark directory to start Spark SQL:
-
Run the following command to create a temporary view named
spark_starrockson thescore_boardtable which belongs to thetestdatabase: -
Run the following command to read data from the temporary view:
Spark returns the following data:
Read data using Spark DataFrame
-
Run the following command in the Spark directory to start Spark Shell:
-
Run the following command to create a DataFrame named
starrocksSparkDFon thescore_boardtable which belongs to thetestdatabase: -
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
-
Run the following command in the Spark directory to start Spark Shell:
-
Run the following command to create an RDD named
starrocksSparkRDDon thescore_boardtable which belongs to thetestdatabase. -
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 ondefault_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>"toOPTIONS. -
Spark DataFrame:
-
Spark RDD: add
"starrocks.request.warehouse" -> "<warehouse_name>"tocfg.
Best practices
When you read data from the database using the Spark connector, you can use thestarrocks.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:-
Go to the
testdatabase and create a table namedmytable. -
Insert data into
mytable. -
Query the
mytabletable.
Full table scan
-
Run the following command in the Spark directory to create a DataFrame named
dfonmytabletable which belongs to thetestdatabase: -
View the FE log file fe.log of your cluster, and find the SQL statement executed to read data. Example:
-
In the
testdatabase, use EXPLAIN to obtain the execution plan of the SELECTk,b,dt,vfromtest.mytablestatement:
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
-
Run the following command, in which you use the
starrocks.filter.queryparameter to specify a filter conditiondt='2022-01-02 08:00:00for partition pruning, in the Spark directory to create a DataFrame nameddfon themytabletable which belongs to thetestdatabase: -
View the FE log file fe.log of your cluster, and find the SQL statement executed to read data. Example:
-
In the
testdatabase, use EXPLAIN to obtain the execution plan of the SELECTk,b,dt,vfromtest.mytablewhere dt=‘2022-01-02 08:00:00’ statement:
partitions=1/3) and all of the tablets (as suggested by tabletRatio=3/3) in that partition.
Bucket pruning
-
Run the following command, in which you use the
starrocks.filter.queryparameter to specify a filter conditionk=1for bucket pruning, in the Spark directory to create a DataFrame nameddfon themytabletable which belongs to thetestdatabase: -
View the FE log file fe.log of your cluster, and find the SQL statement executed to read data. Example:
-
In the
testdatabase, use EXPLAIN to obtain the execution plan of the SELECTk,b,dt,vfromtest.mytablewhere k=1 statement:
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
-
Run the following command, in which you use the
starrocks.filter.queryparameter to specify two filter conditionsk=7anddt='2022-01-02 08:00:00'for bucket pruning and partition pruning, in the Spark directory to create a DataFrame nameddfon themytabletable on thetestdatabase: -
View the FE log file fe.log of your cluster, and find the SQL statement executed to read data. Example:
-
In the
testdatabase, use EXPLAIN to obtain the execution plan of the SELECTk,b,dt,vfromtest.mytablewhere k=7 and dt=‘2022-01-02 08:00:00’ statement:
partitions=1/3) and only one tablet (as suggested by tabletRatio=1/3) in that partition.
Prefix index filtering
-
Insert more data records into a partition of the
mytabletable which belongs to thetestdatabase: -
Query the
mytabletable: -
Run the following command, in which you use the
starrocks.filter.queryparameter to specify a filter conditionk=1for prefix index filtering, in the Spark directory to create a DataFrame nameddfon themytabletable which belongs to thetestdatabase: -
In the
testdatabase, setis_report_successtotrueto enable profile reporting: -
Use a browser to open the
http://<fe_host>:<http_http_port>/querypage, and view the profile of the SELECT * FROM mytable where k=1 statement. Example:
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 typeSTRUCT, 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 (notablyDATE, 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:Current limitations
- Nested type inference is not automatic. You must supply
starrocks.column.typesfor every nested column. DATEandDATETIMEfields inside nested types are returned as SparkDateType/TimestampTypeonly when the declared type is provided. Without the declaration they fall back toStringType.