Spark Doris Connector
Spark Doris Connector can support reading data stored in Doris and writing data to Doris through Spark.
Github: <https://github.com/apache/doris-spark-connector>
- Support reading data from
Doris. - Support
Spark DataFramebatch/stream writing data toDoris - You can map the
Doristable toDataFrameorRDD, it is recommended to useDataFrame. - Support the completion of data filtering on the
Dorisside to reduce the amount of data transmission.
Version Compatibility
| Connector | Spark | Doris | Java | Scala |
|---|---|---|---|---|
| 1.3.1 | 3.4 ~ 3.1, 2.4, 2.3 | 1.0 + | 8 | 2.12, 2.11 |
| 1.2.0 | 3.2, 3.1, 2.3 | 1.0 + | 8 | 2.12, 2.11 |
| 1.1.0 | 3.2, 3.1, 2.3 | 1.0 + | 8 | 2.12, 2.11 |
| 1.0.1 | 3.1, 2.3 | 0.12 - 0.15 | 8 | 2.12, 2.11 |
Build and Install
Ready to work
- Modify the
custom_env.sh.tplfile and rename it tocustom_env.sh - Execute following command in source dir:
sh build.shFollow the prompts to enter the Scala and Spark versions you need to start compiling.
After the compilation is successful, the target jar package will be generated in the dist directory, such as: spark-doris-connector-3.1_2.12-1.2.0-SNAPSHOT.jar. Copy this file to ClassPath in Spark to use Spark-Doris-Connector. For example, Spark running in Local mode, put this file in the jars/ folder. Spark running in Yarn cluster mode, put this file in the pre-deployment package.
For example upload spark-doris-connector-3.1_2.12-1.2.0-SNAPSHOT.jar to hdfs and add hdfs file path in spark.yarn.jars.
- Upload
spark-doris-connector-3.1_2.12-1.2.0-SNAPSHOT.jarJar to hdfs.
1. hdfs dfs -mkdir /spark-jars/
2. hdfs dfs -put /your_local_path/spark-doris-connector-3.1_2.12-1.2.0-SNAPSHOT.jar /spark-jars/
- Add
spark-doris-connector-3.1_2.12-1.2.0-SNAPSHOT.jardependency in Cluster.
1. spark.yarn.jars=hdfs:///spark-jars/spark-doris-connector-3.1_2.12-1.2.0-SNAPSHOT.jar
Using Maven
1. <dependency>
2. <groupId>org.apache.doris</groupId>
3. <artifactId>spark-doris-connector-3.4_2.12</artifactId>
4. <version>1.3.0</version>
5. </dependency>
Notes
Please replace the Connector version according to the different Spark and Scala versions.
Example
Read
SQL
1. CREATE
2. TEMPORARY VIEW spark_doris
3. USING doris
4. OPTIONS(
5. "table.identifier"="$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME",
6. "fenodes"="$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT",
7. "user"="$YOUR_DORIS_USERNAME",
8. "password"="$YOUR_DORIS_PASSWORD"
9. );
11. SELECT *
12. FROM spark_doris;
DataFrame
1. val dorisSparkDF = spark.read.format("doris")
2. .option("doris.table.identifier", "$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME")
3. .option("doris.fenodes", "$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT")
4. .option("user", "$YOUR_DORIS_USERNAME")
5. .option("password", "$YOUR_DORIS_PASSWORD")
6. .load()
8. dorisSparkDF.show(5)
RDD
1. import org.apache.doris.spark._
3. val dorisSparkRDD = sc.dorisRDD(
4. tableIdentifier = Some("$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME"),
5. cfg = Some(Map(
6. "doris.fenodes" -> "$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT",
7. "doris.request.auth.user" -> "$YOUR_DORIS_USERNAME",
8. "doris.request.auth.password" -> "$YOUR_DORIS_PASSWORD"
9. ))
10. )
12. dorisSparkRDD.collect()
pySpark
1. dorisSparkDF = spark.read.format("doris")
2. .option("doris.table.identifier", "$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME")
3. .option("doris.fenodes", "$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT")
4. .option("user", "$YOUR_DORIS_USERNAME")
5. .option("password", "$YOUR_DORIS_PASSWORD")
6. .load()
7. // show 5 lines data
8. dorisSparkDF.show(5)
Write
SQL
1. CREATE
2. TEMPORARY VIEW spark_doris
3. USING doris
4. OPTIONS(
5. "table.identifier"="$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME",
6. "fenodes"="$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT",
7. "user"="$YOUR_DORIS_USERNAME",
8. "password"="$YOUR_DORIS_PASSWORD"
9. );
11. INSERT INTO spark_doris
12. VALUES ("VALUE1", "VALUE2", ...);
13. # or
14. INSERT INTO spark_doris
15. SELECT *
16. FROM YOUR_TABLE
17. # or
18. INSERT OVERWRITE
19. SELECT *
20. FROM YOUR_TABLE
DataFrame(batch/stream)
1. ## batch sink
2. val mockDataDF = List(
3. (3, "440403001005", "21.cn"),
4. (1, "4404030013005", "22.cn"),
5. (33, null, "23.cn")
6. ).toDF("id", "mi_code", "mi_name")
7. mockDataDF.show(5)
9. mockDataDF.write.format("doris")
10. .option("doris.table.identifier", "$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME")
11. .option("doris.fenodes", "$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT")
12. .option("user", "$YOUR_DORIS_USERNAME")
13. .option("password", "$YOUR_DORIS_PASSWORD")
14. //other options
15. //specify the fields to write
16. .option("doris.write.fields", "$YOUR_FIELDS_TO_WRITE")
17. // Support setting Overwrite mode to overwrite data
18. // .option("save_mode", SaveMode.Overwrite)
19. .save()
21. ## stream sink(StructuredStreaming)
23. ### Result DataFrame with structured data, the configuration method is the same as the batch mode.
24. val sourceDf = spark.readStream.
25. .format("your_own_stream_source")
26. .load()
28. val resultDf = sourceDf.<transformations>
30. resultDf.writeStream
31. .format("doris")
32. .option("checkpointLocation", "$YOUR_CHECKPOINT_LOCATION")
33. .option("doris.table.identifier", "$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME")
34. .option("doris.fenodes", "$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT")
35. .option("user", "$YOUR_DORIS_USERNAME")
36. .option("password", "$YOUR_DORIS_PASSWORD")
37. .start()
38. .awaitTermination()
40. ### There is a column value in the Result DataFrame that can be written directly, such as the value in the kafka message that conforms to the import format
42. val kafkaSource = spark.readStream
43. .format("kafka")
44. .option("kafka.bootstrap.servers", "$YOUR_KAFKA_SERVERS")
45. .option("startingOffsets", "latest")
46. .option("subscribe", "$YOUR_KAFKA_TOPICS")
47. .load()
48. kafkaSource.selectExpr("CAST(key AS STRING)", "CAST(value as STRING)")
49. .writeStream
50. .format("doris")
51. .option("checkpointLocation", "$YOUR_CHECKPOINT_LOCATION")
52. .option("doris.table.identifier", "$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME")
53. .option("doris.fenodes", "$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT")
54. .option("user", "$YOUR_DORIS_USERNAME")
55. .option("password", "$YOUR_DORIS_PASSWORD")
56. // Set this option to true, and the value column in the Kafka message will be written directly without processing.
57. .option("doris.sink.streaming.passthrough", "true")
58. .option("doris.sink.properties.format", "json")
59. //other options
60. .start()
61. .awaitTermination()
Configuration
General
| Key | Default Value | Comment |
|---|---|---|
| doris.fenodes | — | Doris FE http address, support multiple addresses, separated by commas |
| doris.table.identifier | — | Doris table identifier, eg, db1.tbl1 |
| doris.request.retries | 3 | Number of retries to send requests to Doris |
| doris.request.connect.timeout.ms | 30000 | Connection timeout for sending requests to Doris |
| doris.request.read.timeout.ms | 30000 | Read timeout for sending request to Doris |
| doris.request.query.timeout.s | 3600 | Query the timeout time of doris, the default is 1 hour, -1 means no timeout limit |
| doris.request.tablet.size | Integer.MAX_VALUE | The number of Doris Tablets corresponding to an RDD Partition. The smaller this value is set, the more partitions will be generated. This will increase the parallelism on the Spark side, but at the same time will cause greater pressure on Doris. |
| doris.read.field | — | List of column names in the Doris table, separated by commas |
| doris.batch.size | 1024 | The maximum number of rows to read data from BE at one time. Increasing this value can reduce the number of connections between Spark and Doris. Thereby reducing the extra time overhead caused by network delay. |
| doris.exec.mem.limit | 2147483648 | Memory limit for a single query. The default is 2GB, in bytes. |
| doris.deserialize.arrow.async | false | Whether to support asynchronous conversion of Arrow format to RowBatch required for spark-doris-connector iteration |
| doris.deserialize.queue.size | 64 | Asynchronous conversion of the internal processing queue in Arrow format takes effect when doris.deserialize.arrow.async is true |
| doris.write.fields | — | Specifies the fields (or the order of the fields) to write to the Doris table, fileds separated by commas. By default, all fields are written in the order of Doris table fields. |
| doris.sink.batch.size | 100000 | Maximum number of lines in a single write BE |
| doris.sink.max-retries | 0 | Number of retries after writing BE failed |
| doris.sink.properties.format | — | Data format of the stream load. Supported formats: csv, json, arrow(since version 1.4.0) More Multi-parameter details |
| doris.sink.properties.* | — | Import parameters for Stream Load. For example: Specify column separator: ‘doris.sink.properties.column_separator’ = ‘,’. More parameter details |
| doris.sink.task.partition.size | — | The number of partitions corresponding to the Writing task. After filtering and other operations, the number of partitions written in Spark RDD may be large, but the number of records corresponding to each Partition is relatively small, resulting in increased writing frequency and waste of computing resources. The smaller this value is set, the less Doris write frequency and less Doris merge pressure. It is generally used with doris.sink.task.use.repartition. |
| doris.sink.task.use.repartition | false | Whether to use repartition mode to control the number of partitions written by Doris. The default value is false, and coalesce is used (note: if there is no Spark action before the write, the whole computation will be less parallel). If it is set to true, then repartition is used (note: you can set the final number of partitions at the cost of shuffle). |
| doris.sink.batch.interval.ms | 50 | The interval time of each batch sink, unit ms. |
| doris.sink.enable-2pc | false | Whether to enable two-stage commit. When enabled, transactions will be committed at the end of the job, and all pre-commit transactions will be rolled back when some tasks fail. |
| doris.sink.auto-redirect | true | Whether to redirect StreamLoad requests. After being turned on, StreamLoad will write through FE and no longer obtain BE information explicitly. |
SQL & Dataframe Configuration
| Key | Default Value | Comment |
|---|---|---|
| user | — | Doris username |
| password | — | Doris password |
| doris.filter.query.in.max.count | 100 | In the predicate pushdown, the maximum number of elements in the in expression value list. If this number is exceeded, the in-expression conditional filtering is processed on the Spark side. |
| doris.ignore-type | — | In a temporary view, specify the field types to ignore when reading the schema. eg: when ‘doris.ignore-type’=’bitmap,hll’ |
Structured Streaming Configuration
| Key | Default Value | Comment |
|---|---|---|
| doris.sink.streaming.passthrough | false | Write the value of the first column directly without processing. |
RDD Configuration
| Key | Default Value | Comment |
|---|---|---|
| doris.request.auth.user | — | Doris username |
| doris.request.auth.password | — | Doris password |
| doris.filter.query | — | Filter expression of the query, which is transparently transmitted to Doris. Doris uses this expression to complete source-side data filtering. |
tip
- In Spark SQL, when writing data through insert into, if the target table of doris contains
BITMAPorHLLtype data, you need to set the parameterdoris.ignore-typeto the corresponding type, and setdoris.write.fieldsmaps the corresponding columns, the usage is as follows:
BITMAP
1. CREATE TEMPORARY VIEW spark_doris 2. USING doris 3. OPTIONS( 4. "table.identifier"="$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME", 5. "fenodes"="$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT", 6. "user"="$YOUR_DORIS_USERNAME", 7. "password"="$YOUR_DORIS_PASSWORD" 8. "doris.ignore-type"="bitmap", 9. "doris.write.fields"="col1,col2,col3,bitmap_col2=to_bitmap(col2),bitmap_col3=bitmap_hash(col3)" 10. );HLL
1. CREATE TEMPORARY VIEW spark_doris 2. USING doris 3. OPTIONS( 4. "table.identifier"="$YOUR_DORIS_DATABASE_NAME.$YOUR_DORIS_TABLE_NAME", 5. "fenodes"="$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT", 6. "user"="$YOUR_DORIS_USERNAME", 7. "password"="$YOUR_DORIS_PASSWORD" 8. "doris.ignore-type"="hll", 9. "doris.write.fields"="col1,hll_col1=hll_hash(col1)" 10. );2. Since version 1.3.0, the default value ofdoris.sink.max-retriesconfiguration is 0, which means no retries are performed by default. When this parameter is set greater than 0, batch-level failure retries will be performed, and data of the configured size ofdoris.sink.batch.sizewill be cached in the Spark Executor memory. The memory allocation may need to be appropriately increased. 3. Since version 1.3.0, overwrite mode insertion is supported (only full table-level overwrite insertion is supported). The specific usage is as followsDataFrame
1. resultDf.format("doris") 2. .option("doris.fenodes","$YOUR_DORIS_FE_HOSTNAME:$YOUR_DORIS_FE_RESFUL_PORT") 3. // your own options 4. .option("save_mode", SaveMode.Overwrite) 5. .save()SQL
1. INSERT OVERWRITE your_target_table 2. SELECT * FROM your_source_table
Doris & Spark Column Type Mapping
| Doris Type | Spark Type |
|---|---|
| NULL_TYPE | DataTypes.NullType |
| BOOLEAN | DataTypes.BooleanType |
| TINYINT | DataTypes.ByteType |
| SMALLINT | DataTypes.ShortType |
| INT | DataTypes.IntegerType |
| BIGINT | DataTypes.LongType |
| FLOAT | DataTypes.FloatType |
| DOUBLE | DataTypes.DoubleType |
| DATE | DataTypes.DateType |
| DATETIME | DataTypes.StringType1 |
| DECIMAL | DecimalType |
| CHAR | DataTypes.StringType |
| LARGEINT | DecimalType |
| VARCHAR | DataTypes.StringType |
| TIME | DataTypes.DoubleType |
| HLL | Unsupported datatype |
| Bitmap | Unsupported datatype |
- Note: In Connector,
DATETIMEis mapped toString. Due to the processing logic of the Doris underlying storage engine, when the time type is used directly, the time range covered cannot meet the demand. So useStringtype to directly return the corresponding time readable text.
