The ByteHouse Cloud Data Warehouse Edition Spark Connector is specifically designed for loading data into ByteHouse Cloud Data Warehouse Edition via Spark. This article introduces three methods for connecting to ByteHouse and processing data: Spark SQL, EMR-supported Serverless Spark, and Spark Jar.
Please configure the dependencies for the following drivers in the program according to the method below.
For Maven projects that need to be compiled using the Spark connector, add the following dependencies to the project's pom.xml file.
Add the following dependencies.
Spark version | Dependencies | Download |
|---|---|---|
Spark 3.5 |
|
Add the following repositories to the pom.xml file:
<repository> <id>bytedance</id> <name>ByteDance Public Repository</name> <url>https://artifact.bytedance.com/repository/releases</url> </repository>
The general parameters and configuration instructions required to connect to ByteHouse are as follows. For more information about configuring connection parameters, see the section below 配置参数.
Parameters | Configuration instructions |
|---|---|
| Configure as the public network connection domain name for ByteHouse You can view the relevant information in the ByteHouse console under Tenant Management > Basic Information > Network Information. For details, see 步骤二:配置网络信息. |
| That is, the value of the |
| Configure as the name of the ByteHouse database you want to connect to. You can view and copy the name of the database you need to connect to through ByteHouse Console > Database. |
| Set the compute group name to ByteHouse. You can view the default compute group via ByteHouse Console > Tenant Management > Parameter Settings. |
Before running local tests, please load the following JAR files into ${SPARK_HOME/jars}, or specify the local path to the JAR files using --jars when submitting the command.
Replace CLICKHOUSE_HOST, CLICKHOUSE_PASSWORD, CLICKHOUSE_DATABASE, and CLICKHOUSE_VW with actual values. For information on how to obtain these values, see 连接参数配置说明.
export SPARK_LOCAL_IP=localhost export SPARK_HOME=/opt/tiger/spark export CLICKHOUSE_HOST=bytehouse-<REGION>.volces.com export CLICKHOUSE_PASSWORD=<your-api-key> export CLICKHOUSE_DATABASE=<database> export CLICKHOUSE_VW=<your-virtual-warehouse> $SPARK_HOME/bin/spark-sql \ --conf spark.sql.catalog.clickhouse=com.bytehouse.ByteHouseCatalog \ --conf spark.sql.catalog.clickhouse.host=${CLICKHOUSE_HOST:-127.0.0.1} \ --conf spark.sql.catalog.clickhouse.password=${CLICKHOUSE_PASSWORD:-} \ --conf spark.sql.catalog.clickhouse.database=${CLICKHOUSE_DATABASE} \ --conf spark.sql.catalog.clickhouse.option.vw=${CLICKHOUSE_VW} \ --conf spark.driver.bindAddress=127.0.0.1 \ --conf spark.bytehouse.write.batchSize=50000 \ --conf spark.bytehouse.write.format=jdbc \ --jars clickhouse-jdbc-0.4.6-all.jar,driver-java-1.1.75-all.jar,clickhouse-spark-runtime-3.5_2.12-0.8.0.15.jar
Parameters | Configuration instructions |
|---|---|
| Set to the actual path of the JAR file in use. |
When using, replace spark.sql.catalog.clickhouse.host, spark.sql.catalog.clickhouse.password, spark.sql.catalog.clickhouse.database, and spark.sql.catalog.clickhouse.option.vw with actual values. For information on how to obtain these values, see 连接参数配置说明.
import org.apache.spark.sql.SparkSession object Main { def main(args: Array[String]): Unit = { val spark = SparkSession .builder() .appName("App") .master("local") .config("spark.sql.catalog.clickhouse", "com.bytehouse.ByteHouseCatalog") .config("spark.sql.catalog.clickhouse.host", "<your-host>") .config("spark.sql.catalog.clickhouse.protocol", "http") .config("spark.sql.catalog.clickhouse.password", "<your-api-key>") .config("spark.sql.catalog.clickhouse.database", "<database>") .config("spark.sql.catalog.clickhouse.option.vw", "<your-virtual-warehouse>") .config("spark.sql.catalog.clickhouse.option.query_timeout", "600000") .config("spark.sql.catalog.clickhouse.option.connect_timeout", "600000") .config("spark.sql.catalog.clickhouse.option.receive_timeout", "600000") .config("spark.sql.catalog.clickhouse.option.send_timeout", "600000") .config("spark.sql.catalog.clickhouse.option.max_execution_time", "108000000") .config("spark.bytehouse.write.batchSize", "50000") .config("spark.bytehouse.write.format", "jdbc") .getOrCreate() val sql = "select * from clickhouse.xx.xx" val df = spark.sql(sql) spark.stop() } }
When using, replace spark.sql.catalog.clickhouse.host, spark.sql.catalog.clickhouse.password, spark.sql.catalog.clickhouse.database, and spark.sql.catalog.clickhouse.option.vw with actual values. For information on how to obtain these values, see 连接参数配置说明.
from pyspark.sql import SparkSession spark = SparkSession\ .builder\ .appName("App")\ .master("local")\ .config("spark.sql.catalog.clickhouse", "com.bytehouse.ByteHouseCatalog")\ .config("spark.sql.catalog.clickhouse.host", "<your-host>")\ .config("spark.sql.catalog.clickhouse.protocol", "http")\ .config("spark.sql.catalog.clickhouse.password", "<your-api-key>")\ .config("spark.sql.catalog.clickhouse.database", "<database>")\ .config("spark.sql.catalog.clickhouse.option.vw", "<your-virtual-warehouse>")\ .config("spark.sql.catalog.clickhouse.option.query_timeout", "600000")\ .config("spark.sql.catalog.clickhouse.option.connect_timeout", "600000")\ .config("spark.sql.catalog.clickhouse.option.receive_timeout", "600000")\ .config("spark.sql.catalog.clickhouse.option.send_timeout", "600000")\ .config("spark.sql.catalog.clickhouse.option.max_execution_time", "108000000")\ .config("spark.bytehouse.write.batchSize", "50000")\ .config("spark.bytehouse.write.format", "jdbc")\ .config("spark.jars", "clickhouse-jdbc-0.4.6-all.jar,driver-java-1.1.75-all.jar,clickhouse-spark-runtime-3.5_2.12-0.8.0.15.jar").\ getOrCreate() sql = "select count(*) from clickhouse.xxx.xxx" df = spark.sql(sql) df.show() spark.stop()
The Serverless Spark method is applicable to the Volcengine EMR (E-MapReduce) service. You can refer to the following commands to connect to ByteHouse using the Serverless Spark method. For Serverless Spark job development, please refer to relevant documentation, such as the Serverless Spark Job Development Guide.
You can connect ByteHouse and Volcano Engine EMR (E-MapReduce) services using Serverless Spark, which supports connections via Spark SQL, Spark Jar, and PySpark.
You can refer to the following command to connect to ByteHouse using Spark SQL CLI. For details, see Spark SQL Job Development Guide.
The following command uses the Spark SQL default compute group to connect to ByteHouse. When using the command, replace the placeholders with the actual parameter values. For information on how to obtain these values, see the parameter descriptions below.
set serverless.spark.analysis=true; set spark.hadoop.fs.tos.skip.resolve = true; set las.cross.vpc.access.enabled = true; set las.cross.vpc.vpc.id = <your-vpc-id>; -- 使用可用区 B 子网 set las.cross.vpc.subnet.id = <your-subnet-id>; set las.cross.vpc.security.group.id = <your-securitygroup-id>; set las.cross.vpc.accountId=<your-account-id>; set las.cluster.type = vke; set emr.serverless.spark.only.parse.enabled = true; set serverless.spark.access.key=<your-tos-ak>; set serverless.spark.secret.key=<your-tos-sk>; set spark.sql.catalog.clickhouse=com.bytehouse.ByteHouseCatalog; set spark.sql.catalog.clickhouse.host=tenant-<your-account-id>-<REGION>.bytehouse.ivolces.com; set spark.sql.catalog.clickhouse.protocol=http; set spark.sql.catalog.clickhouse.password=<your-api-key>; set spark.sql.catalog.clickhouse.database=<database>; set spark.sql.catalog.clickhouse.option.vw=<your-virtual-warehouse>; set las.spark.jar.depend.jars = [{"schema":"","fileName":"tos://test-bh-tos/cdw/driver-java-1.1.75-all.jar"},{"schema":"","fileName":"tos://test-bh-tos/cdw/clickhouse-jdbc-0.4.6-all.jar"},{"schema":"","fileName":"tos://test-bh-tos/cdw/clickhouse-spark-runtime-3.5_2.12-0.8.0.15.jar"}]; use clickhouse; -- 查询 hive -- select * from spark_catalog.hive_db.hive_table; -- select * from people_t_copy; insert into people_t_copy select * from people_t;
Parameter description
Parameters | Configuration instructions |
|---|---|
las.cross.vpc | To configure VPC network information, go to the ByteHouse console and navigate to Tenant Management > Basic Information. In the Network Information section, click Details next to the private network domain name to view the VPC in use and related parameter information.
warning When configuring a subnet, you must configure it in Availability Zone B. |
las.cross.vpc.accountId | Configure as the Volcengine ID you use. You can click Personal Center in the top right corner to copy your account ID. |
serverless.spark.access.key、serverless.spark.secret.key | Configure with AK/SK (access key and secret key). For details, see Obtain Access Key. |
spark.sql.catalog.clickhouse |
|
las.spark.jar.depend.jars | Configure the TOS path for the jar that is actually used. |
Other | For more instructions on configuring connection parameters, please refer to the 配置参数 section below. |
warning
The expected format for the set parameter is "SET", "SET key", or "SET key=value". If the key needs to contain special characters, or the value contains a semicolon (;), you can enclose the key or value in backticks (````).
If you have created a Spark SQL compute group and want to use it to connect to ByteHouse, you can do so as follows. For an introduction to Spark SQL compute groups, see Queue Management.
Log in to the Volcengine EMR console. Under the Resource Management tab, click Serverless; click the queue you are using; click Compute Group; click Spark SQL Compute Group; click Parameter Configuration; then configure the following spark.jars custom parameters.
{ "spark.jars": "tos://bytehouse-spark/cdw/driver-java-1.1.75-all.jar,tos://bytehouse-spark/cdw/clickhouse-jdbc-0.4.6-all.jar,tos://bytehouse-spark/cdw/clickhouse-spark-runtime-3.5_2.12-0.8.0.15.jar" }
After configuration is complete, you can use the following commands to connect to ByteHouse. When using the command, replace the placeholders with the actual parameter values. For information on how to obtain these values, refer to the parameter description below.
set serverless.spark.analysis=true; set spark.hadoop.fs.tos.skip.resolve = true; set las.cluster.type = vke; set emr.serverless.spark.only.parse.enabled = true; set serverless.spark.access.key=<your-tos-ak>; set serverless.spark.secret.key=<your-tos-sk>; set spark.sql.catalog.clickhouse=com.bytehouse.ByteHouseCatalog; set spark.sql.catalog.clickhouse.host=tenant-<your-account-id>-<REGION>.bytehouse.ivolces.com; set spark.sql.catalog.clickhouse.protocol=http; set spark.sql.catalog.clickhouse.password=<your-api-key>; set spark.sql.catalog.clickhouse.database=<database>; set spark.sql.catalog.clickhouse.option.vw=<your-virtual-warehouse>; use clickhouse; -- 查询 hive -- select * from spark_catalog.hive_db.hive_table; -- select * from people_t_copy; insert into people_t_copy select * from people_t;
Parameter description
Parameters | Configuration instructions |
|---|---|
serverless.spark.access.key、serverless.spark.secret.key | Configure to use AK/SK (access key, secret key). You can refer to Obtain an Access Key. |
spark.sql.catalog.clickhouse |
|
las.spark.jar.depend.jars | Configure the TOS path to the jar that is actually used. |
Other | For more information about configuring connection parameters, see the section below 配置参数. |
warning
The expected format for the set parameter is "SET", "SET key", or "SET key=value". If a key needs to contain special characters, or a value contains a semicolon (;), you can enclose it in backticks (````).
You can refer to the following commands to connect to ByteHouse using the Spark Jar method. Spark Jar job development guide: For details, see Spark Jar job development guide.
When using, replace spark.sql.catalog.clickhouse.host, spark.sql.catalog.clickhouse.password, spark.sql.catalog.clickhouse.database, and spark.sql.catalog.clickhouse.option.vw with actual values. For information on how to obtain these values, see 连接参数配置说明.
import org.apache.spark.sql.SparkSession object Main { def main(args: Array[String]): Unit = { val spark = SparkSession .builder() .appName("App") .master("local") .config("spark.sql.catalog.clickhouse", "com.bytehouse.ByteHouseCatalog") .config("spark.sql.catalog.clickhouse.host", "<your-host>") .config("spark.sql.catalog.clickhouse.protocol", "http") .config("spark.sql.catalog.clickhouse.password", "<your-api-key>") .config("spark.sql.catalog.clickhouse.database", "<database>") .config("spark.sql.catalog.clickhouse.option.vw", "<your-virtual-warehouse>") .config("spark.sql.catalog.clickhouse.option.query_timeout", "600000") .config("spark.sql.catalog.clickhouse.option.connect_timeout", "600000") .config("spark.sql.catalog.clickhouse.option.receive_timeout", "600000") .config("spark.sql.catalog.clickhouse.option.send_timeout", "600000") .config("spark.sql.catalog.clickhouse.option.max_execution_time", "108000000") .config("spark.bytehouse.write.batchSize", "50000") .config("spark.bytehouse.write.format", "jdbc") .getOrCreate() val sql = "select * from clickhouse.xx.xx" val df = spark.sql(sql) spark.stop() } }
You can refer to the following command to connect to ByteHouse using PySpark. For details on the PySpark Job Development Guide, see PySpark Job Development Guide.
When using, replace spark.sql.catalog.clickhouse.host, spark.sql.catalog.clickhouse.password, spark.sql.catalog.clickhouse.database, and spark.sql.catalog.clickhouse.option.vw with actual values. For information on how to obtain these values, see 连接参数配置说明.
from pyspark.sql import SparkSession spark = SparkSession\ .builder\ .appName("App")\ .master("local")\ .config("spark.sql.catalog.clickhouse", "com.bytehouse.ByteHouseCatalog")\ .config("spark.sql.catalog.clickhouse.host", "<your-host>")\ .config("spark.sql.catalog.clickhouse.protocol", "http")\ .config("spark.sql.catalog.clickhouse.password", "<your-api-key>")\ .config("spark.sql.catalog.clickhouse.database", "<database>")\ .config("spark.sql.catalog.clickhouse.option.vw", "<your-virtual-warehouse>")\ .config("spark.sql.catalog.clickhouse.option.query_timeout", "600000")\ .config("spark.sql.catalog.clickhouse.option.connect_timeout", "600000")\ .config("spark.sql.catalog.clickhouse.option.receive_timeout", "600000")\ .config("spark.sql.catalog.clickhouse.option.send_timeout", "600000")\ .config("spark.sql.catalog.clickhouse.option.max_execution_time", "108000000")\ .config("spark.bytehouse.write.batchSize", "50000")\ .config("spark.bytehouse.write.format", "jdbc")\ getOrCreate() sql = "select count(*) from clickhouse.xxx.xxx" df = spark.sql(sql) df.show() spark.stop()
Parameters | Required or optional | Default value | Data types | Description | Initial version |
|---|---|---|---|---|---|
spark.sql.catalog.${catalog}.host | ✅ | No default value | string | The format is | 0.8.0.2 |
spark.sql.catalog.${catalog}.http_port | No | 19000 | int | Keep the default value. | 0.8.0.2 |
spark.sql.catalog.${catalog}.protocol | No | http | string | Keep the default value. | 0.8.0.2 |
spark.sql.catalog.${catalog}.user | No | bytehouse | string | Simply keep the default value. | 0.8.0.2 |
spark.sql.catalog.${catalog}.password | ✅ | No default value | string | Configure the API Key for ByteHouse. You can refer to the 获取 API Key section to obtain API Key information. | 0.8.0.2 |
spark.sql.catalog.${catalog}.database | No | default | string | Configure the default database name. After configuration, when you use Spark SQL to query tables in this database, you can directly use the table name to query, without needing to use the | 0.8.0.2 |
spark.sql.catalog.${catalog}.gateway_version | No | auto | string | v1, v2, or auto. If the gateway is set to auto, v1 or v2 is selected automatically. | 0.8.0.10 |
spark.sql.catalog.${catalog}.timezone | No | server | string | The time zone in use can be configured as server, client, UTC+3, Asia/Shanghai, and more. | 0.8.0.2 |
spark.sql.catalog.${catalog}.dedup_key_mode | No | No default value | string | Strategies for handling duplicate key values in data are supported:
| 0.8.0.3 |
spark.sql.catalog.${catalog}.option | ✅ | No default value | map | The vw parameter is required and should be configured as follows: | 0.8.0.10 |
spark.bytehouse.write.batchSize | No | 10000 | int | None. | 0.8.0.2 |
spark.bytehouse.write.maxRetry | No | 30 | int | Maximum number of retries on write failure | 0.8.0.3 |
spark.bytehouse.write.retryInterval | No | 10s | time | Write failure retry interval | 0.8.0.3 |
spark.bytehouse.write.retryExponentialBackoffEnabled | No | true | boolean | During write operations, exponential backoff can be enabled for retry intervals. | 0.8.0.9 |
spark.bytehouse.write.sharding-strategy | No | No default value | string | ClickHouse distributed sharding strategy. Supported values:
| 0.8.0.9 |
spark.bytehouse.write.sharding-key | No | No default value | string | A hash partition key can consist of multiple fields, separated by commas. | 0.8.0.9 |
spark.bytehouse.write.sharding-expression | No | No default value | string | The hash partitioning expression. If a hash partitioning expression is set, all involved field names must also be listed in the | 0.8.0.9 |
spark.bytehouse.read.byPartition | No | No default value | boolean | If set to true, the query will be split by partition. | 0.8.0.9 |
spark.bytehouse.read.byPartition.strategy | No | manual | string | Query partition policies. Supported values:
| 0.8.0.15 |
spark.bytehouse.read.byPartition.count | No | 100 | int | Number of partitions. | 0.8.0.15 |
spark.bytehouse.read.partition.lower | No | No default value | string | The lower bound range (including the lower bound value) used when scanning by partition. | 0.8.0.9 |
spark.bytehouse.read.partition.upper | No | No default value | string | Upper limit range for partition-based scanning (including the upper limit value). | 0.8.0.9 |
spark.bytehouse.read.partition.stringAsDate | No | false | boolean | Supports partition keys of type String with automatic partitioning based on values in the yyyy-MM-dd format. | 0.8.0.15 |
spark.bytehouse.read.partition.stringAsInt | No | true | boolean | Supports partition keys of type String with automatic partitioning based on values in the yyyyMMdd format. | 0.8.0.15 |
spark.bytehouse.read.partition.detectMinMax | No | true | boolean | When the partitioning strategy is set to automatic, if the query condition contains only the upper or lower bound of the partition, you can query the maximum and minimum values of the partition column. | 0.8.0.15 |
spark.bytehouse.read.byBucket | No | No default value | boolean | If set to true, the query will be split by bucket. | 0.8.0.9 |
spark.bytehouse.read.maxRetry | No | 30 | int | Maximum number of retry attempts on read failure. | 0.8.0.9 |
spark.bytehouse.read.retryInterval | No | 10s | time | Read failure retry interval. | 0.8.0.9 |
spark.bytehouse.read.retryExponentialBackoffEnabled | No | true | boolean | Enable exponential backoff for retry intervals during read operations. When this setting is configured to true, if a read operation encounters an error or failure, the system will retry the operation at intervals that increase exponentially. | 0.8.0.9 |
You can set the timeout period: using the following parameters.
set spark.sql.catalog.clickhouse.option.socket_timeout=60000; set spark.sql.catalog.clickhouse.option.query_timeout=60000; set spark.sql.catalog.clickhouse.option.connect_timeout=60000; set spark.sql.catalog.clickhouse.option.receive_timeout=60000; set spark.sql.catalog.clickhouse.option.send_timeout=60000; set spark.sql.catalog.clickhouse.option.max_execution_time=10800000;
If you need to add custom parameters, you can do so by following the spark.sql.catalog.clickhouse.option.xxx pattern.
set spark.sql.catalog.clickhouse.option.xxx=xxx;
Spark supports parallel reading of bucket tables and partitioned tables, and also allows both bucket parallel reading and partition parallel reading to be enabled simultaneously, with the overall parallelism equal to the product of the two.
If the table being read is a bucket table, parallel reading by bucket can be enabled, with the degree of parallelism equal to the number of buckets. The table creation statement in this example specifies a parallelism of 32.
CREATE TABLE IF NOT EXISTS test.createTable( id UInt32, name String DEFAULT '' ) ENGINE=CnchMergeTree ORDER BY id CLUSTER BY EXPRESSION cityHash64(id) % 32 INTO 32 BUCKETS
# 开启按 bucket 并行读 set spark.bytehouse.read.byBucket=true;
If the table being read is a partitioned table, parallel reading by partition can be enabled.
An example of a table creation statement is as follows:
CREATE TABLE IF NOT EXISTS test.createTable( id UInt32, name String DEFAULT '', date Date ) ENGINE=CnchMergeTree ORDER BY id PARTITION BY date
Suppose you need to query data for date within the time range [2025-01-01, 2025-01-10]; you can use the following configuration with a parallelism of 10.
set spark.bytehouse.read.byPartition=true; set spark.bytehouse.read.partition.lower=20250101; set spark.bytehouse.read.partition.upper=20250110;
Spark supports writing to bucket tables. The following example demonstrates how to create the test.createTable table and enable data writing by configuring driver parameters.
CREATE TABLE IF NOT EXISTS test.createTable( id UInt32, name String DEFAULT '' ) ENGINE=CnchMergeTree ORDER BY id CLUSTER BY EXPRESSION cityHash64(id) % 32 INTO 32 BUCKETS
set spark.bytehouse.write.sharding-strategy=HASH; set spark.bytehouse.write.sharding-key=id; set spark.bytehouse.write.sharding-expression=`cityHash64(id) % 32`;
Scenario | Support status | Spark SQL syntax | ByteHouse Cloud Data Warehouse Edition SQL syntax |
|---|---|---|---|
listTables | ✅ | show tables |
|
loadTable | ✅ | show create table xx |
|
createTable | ❌ | None |
|
dropTable | ✅ | drop table |
|
renameTable | ✅ | rename table xx to xxx |
|
listNamespaces | ✅ | show databases |
|
createNamespace | ✅ | create database xx |
|
dropNamespace | ✅ | drop database xx |
|
Data types | Read | Write | |
|---|---|---|---|
Integer types | UInt8 | ✅ | ✅ |
UInt16 | ✅ | ✅ | |
UInt32 | ✅ | ✅ | |
UInt64 | ✅ | ✅ | |
Int8 | ✅ | ✅ | |
Int16 | ✅ | ✅ | |
Int32 | ✅ | ✅ | |
Int64 | ✅ | ✅ | |
Floating-point numbers | Float32 | ✅ | ✅ |
Float64 | ✅ | ✅ | |
Decimal | ✅ | ✅ | |
Decimal32 | ✅ | ✅ | |
Decimal64 | ✅ | ✅ | |
Boolean | bool | ✅ | ✅ |
Strings | String | ✅ | ✅ |
FixedString | ✅ | ✅ | |
Dates | Date | ✅ | ✅ |
DateTime | ✅ | ✅ | |
DateTime64 | ✅ | ✅ | |
UUID | UUID | ✅ | ✅ |
Enum | Enum8 | ✅ | ✅ |
Enum16 | ✅ | ✅ | |
Arrays | Array(T) | ✅ | ✅ |
Maps | Map(K, V) | ✅ | ✅ |