You can connect EMR Serverless Spark to Doris using the official Apache Doris Spark Connector and adding the required configurations. This topic describes how to read data from and write data to Doris in an EMR Serverless Spark environment.
Background information
Apache Doris is a high-performance, real-time analytic database that supports scenarios such as report analysis, ad hoc queries, and federated query acceleration over data lakes. For more information, see Introduction to Apache Doris.
EMR Serverless Spark is a high-performance Lakehouse product that is compatible with open source Spark and provides an enterprise-grade, fully managed data platform service. By combining Apache Doris with EMR Serverless Spark, you can efficiently read, write, and analyze data to implement end-to-end data processing workflows.
Prerequisites
-
A Serverless Spark workspace is created. For more information, see Create a workspace.
-
A Doris cluster is created.
If you create an EMR on ECS OLAP cluster that includes the Doris service, see Create a cluster. This topic uses a Doris cluster created on EMR on ECS as an example, which is referred to as an EMR Doris cluster.
Limits
The EMR Serverless Spark engine version must be esr-2.6.0, esr-3.2.0, esr-4.2.0, or a later version.
Procedure
Step 1: Obtain the Doris Spark Connector JAR and upload it to OSS
Review the official Apache Doris documentation for the Spark Doris Connector. The documentation lists the compatibility between connector versions and Spark engine versions. You must confirm that your Spark version is compatible with the Doris Spark Connector version.
-
Go to the Doris Spark Connector GitHub repository and download the appropriate version.
The Doris Spark Connector Java Archive (JAR) file follows this naming format:
spark-doris-connector-spark-${spark_version}-${connector_version}.jar. For example, if your engine version is esr-3.1.0 (Spark 3.4.3, Scala 2.12), downloadspark-doris-connector-spark-3.4-24.0.0.jar. -
Upload the downloaded Spark Connector JAR file to Alibaba Cloud OSS. For more information, see Simple upload.
Step 2: Create a network connection
EMR Serverless Spark must establish network connectivity with the EMR Doris cluster to access the Doris service. For more information about network connectivity, see Network connectivity between EMR Serverless Spark and other VPCs.
When you add security group rules, open only the required ports in the Destination Port Range. Valid port values range from 1 to 65535. For example, open the HTTP port (8031), RPC port (9061), and Webserver port (8041).
Step 3: Create a database and table in the EMR Doris cluster
-
Log on to the cluster using SSH. For more information, see Log on to a cluster.
-
Run the following command to connect to the EMR Doris cluster.
mysql -h127.0.0.1 -P 9031 -uroot -
Create a database and a table.
CREATE DATABASE IF NOT EXISTS testdb; USE testdb; CREATE TABLE test ( id INT, name STRING ) PROPERTIES("replication_num" = "1"); -
Insert test data.
INSERT INTO test VALUES (1, 'a'), (2, 'b'), (3, 'c'); -
Query the data.
SELECT * FROM test;The following output is returned.
MySQL [testdb]> SELECT * FROM test; +------+------+ | id | name | +------+------+ | 1 | a | | 2 | b | | 3 | c | +------+------+ 3 rows in set (0.177 sec)
Step 4: Read Doris tables using Serverless Spark
Read Doris tables using an SQL session
-
Create an SQL session. For more information, see Manage SQL sessions.
When creating the session, select the engine version that matches your Doris Spark Connector from the Engine Version drop-down list. For Normal Network Connection, select the connection created in Step 2. For Spark Configuration, add the following parameter to load the Doris Spark Connector.
spark.emr.serverless.user.defined.jars oss://<bucketname>/path/connector.jarReplace
oss://<bucketname>/path/connector.jarwith the OSS path of the Doris Spark Connector JAR file that you uploaded in Step 1. For example:oss://emr-oss/spark/spark-doris-connector-spark-3.4-24.0.0.jar. -
On the Development page, create a SparkSQL task, and then select the SQL session that you created from the upper-right corner.
For more information, see Develop SparkSQL.
-
You can copy the following code into the new SparkSQL tab, update the parameters as needed, and then click Run.
CREATE TEMPORARY VIEW test USING doris OPTIONS( "table.identifier" = "testdb.test", "fenodes" = "<doris_address>:<http_port>", "user" = "<user>", "password" = "<password>" ); SELECT * FROM test;The following describes the parameters.
Parameter
Description
Example
testdb.testThe actual database and table name in Doris.
-
If you use a different Doris cluster, configure the parameters accordingly.
-
If you use a cluster created on EMR on ECS, use the following values:
-
testdb.test: This example usestestdb.test. -
<doris_address>: In the EMR on ECS console, go to the Doris cluster’s Nodes page, click the icon next to emr-master, and view the private IP address. -
<http_port>: Default is 8031. -
<user>: Default username isroot. -
<password>: Default password is empty.
-
<doris_address>The private IP address of the node running the Doris service.
<http_port>The port number Doris uses to listen for HTTP requests.
<user>The username used to connect to Doris.
<password>The password used to connect to Doris.
If data is returned successfully, your configuration is correct.
-
Read Doris tables using a Notebook session
-
Create a Notebook session. For more information, see Manage Notebook sessions.
When creating the session, select the engine version that matches your Doris Spark Connector from the Engine Version drop-down list. Under Normal Network Connection, select the connection that you created in Step 2. Under Spark Configuration, add the following parameter to load the Doris Spark Connector.
spark.emr.serverless.user.defined.jars oss://<bucketname>/path/connector.jarReplace
oss://<bucketname>/path/connector.jarwith the OSS path of the Doris Spark Connector JAR file that you uploaded in Step 1. For example:oss://emr-oss/spark/spark-doris-connector-spark-3.4-24.0.0.jar. -
On the Development page, create a task of type Interactive development > Notebook, and then select the Notebook session that you created in the upper-right corner.
For more information, see Manage Notebook sessions.
-
Copy the following code into the new Notebook tab, update the parameters as needed, and click Run.
dorisSparkDF = spark.read.format("doris") \ .option("doris.table.identifier", "testdb.test") \ .option("doris.fenodes", "<doris_address>:<http_port>") \ .option("user", "<user>") \ .option("password", "<password>") \ .load() dorisSparkDF.show(3)The following describes the parameters.
Parameter
Description
Example
testdb.testThe actual database and table name in Doris.
-
If you use a different Doris cluster, configure the parameters accordingly.
-
If you use a cluster created on EMR on ECS, use the following values:
-
testdb.test: This example usestestdb.test. -
<doris_address>: In the EMR on ECS console, go to the Doris cluster’s Nodes page, click the icon next to emr-master, and view the private IP address. -
<http_port>: Default is 8031. -
<user>: Default username isroot. -
<password>: Default password is empty.
-
<doris_address>The private IP address of the node running the Doris service.
<http_port>The port number Doris uses to listen for HTTP requests.
<user>The username used to connect to Doris.
<password>The password used to connect to Doris.
The first three rows of the testdb.test table are returned. If data is returned successfully, your configuration is correct.
-
Step 5: Write to Doris tables using Serverless Spark
Write to Doris tables using an SQL session
Copy the following code into the SparkSQL tab created in the previous step, update the parameters as needed, and click Run.
CREATE TEMPORARY VIEW test_write
USING doris
OPTIONS(
"table.identifier" = "testdb.test",
"fenodes" = "<doris_address>:<http_port>",
"user" = "<user>",
"password" = "<password>"
);
INSERT INTO test_write VALUES (4, 'd'), (5, 'e');
SELECT * FROM test_write;
If the following data is displayed, the write operation is successful.
The expected result contains two columns, id and name, with five rows. The rows (1, a), (2, b), and (3, c) are the original rows. The rows (4, d) and (5, e) are the newly inserted rows.
Write to Doris tables using a Notebook session
Copy the following code into the Notebook tab created in the previous step, update the parameters as needed, and click Run.
data = [(7, 'f'), (8, 'g')]
mockDataDF = spark.createDataFrame(data, ["id", "name"])
mockDataDF.write.mode("append").format("doris") \
.option("doris.table.identifier", "testdb.test") \
.option("doris.fenodes", "<doris_address>:<http_port>") \
.option("user", "<user>") \
.option("password", "<password>") \
.save()
dorisSparkDF = spark.read.format("doris") \
.option("doris.table.identifier", "testdb.test") \
.option("doris.fenodes", "<doris_address>:<http_port>") \
.option("user", "<user>") \
.option("password", "<password>") \
.load()
dorisSparkDF.show(10)If the following data is returned, the write operation was successful.
+---+-----+
| id| name|
+---+-----+
| 1| a|
| 2| b|
| 3| c|
| 7| f|
| 8| g|
+---+-----+