Use Stream Load to load data from a local file or data stream into StarRocks.
Background
Stream Load is a synchronous loading method that uses an HTTP request to load data from a local file or a data stream into StarRocks. Because the operation is synchronous, the HTTP response immediately returns the load result, letting you verify its success. Stream Load supports CSV and JSON formats, and each load must be smaller than 10 GB.
Creating a load task
Stream Load imports data via HTTP. This topic shows how to submit an import job with the curl command. You can also use other HTTP clients.
Syntax
curl --location-trusted -u <username>:<password> -XPUT <url>
(
data_desc
)
[opt_properties]
-
In the
Expectrequest header, we recommend specifying100-continue, i.e.,"Expect:100-continue". This prevents unnecessary data transfer and reduces resource overhead if the server rejects the import task request. -
In StarRocks, some words are SQL reserved keywords and cannot be used directly in SQL statements. To use a reserved keyword in an SQL statement, you must enclose it in backticks (). For more information about keywords, see Keywords.
Parameters
-
<username>:<password>: The username and password for your StarRocks cluster. This parameter is required. If the account has no password, use<username>:. -
PUT: The HTTP request method. This method is required. Stream Load only supports PUT. -
<url>: The URL of the destination StarRocks table. This parameter is required. Use the following format:http://<fe_host>:<fe_http_port>/api/<database_name>/<table_name>/_stream_load.Parameter
Required
Description
<fe_host>Yes
The IP address of an FE in the StarRocks cluster.
<fe_http_port>Yes
The HTTP port of the FE. Default: 18030.
You can find the port number by searching for the http_port parameter on the Configure tab of the StarRocks Services page. Additionally, you can run the
SHOW FRONTENDScommand to view the IP addresses and HTTP ports of the FE nodes.<database_name>Yes
The name of the database containing the destination table.
<table_name>Yes
The name of the destination StarRocks table.
-
desc: Describes the properties of the source data file, such as the file name, format, column separator, row delimiter, target partition, and the column mapping to the StarRocks table. The format is as follows.-T <file_path> -H "format: CSV | JSON" -H "column_separator: <column_separator>" -H "row_delimiter: <row_delimiter>" -H "columns: <column1_name>[, <column2_name>, ... ]" -H "partitions: <partition1_name>[, <partition2_name>, ...]" -H "temporary_partitions: <temporary_partition1_name>[, <temporary_partition2_name>, ...]" -H "jsonpaths: [ \"<json_path1>\"[, \"<json_path2>\", ...] ]" -H "strip_outer_array: true | false" -H "json_root: <json_path>" -H "ignore_json_size: true | false" -H "compression: <compression_algorithm> | Content-Encoding: <compression_algorithm>"The parameters in
data_desccan be divided into three types: common parameters, parameters for CSV, and parameters for JSON.-
Common parameters
Parameter
Required
Description
<file_path>Yes
The path to the source data file. The filename can optionally include an extension.
formatNo
The data format. Valid values:
CSVandJSON. Default:CSV.partitionsNo
The partitions to load data into. If this parameter is not specified, data is loaded into all table partitions.
temporary_partitionsNo
The temporary partitions to load data into.
columnsNo
Specifies the column mapping between the source data file and the StarRocks table.
-
This parameter is not required if the columns in the source data file match the order of the columns in the destination table.
-
If source file columns do not match the table schema, you must use this parameter to define the mapping. You can map columns in two ways:
-
Example 1: The table has columns
c1, c2, c3, but the source file columns are in the orderc3, c2, c1. You must specify-H "columns: c3, c2, c1". -
Example 2: The table has three columns
c1, c2, c3, and the source file's first three columns map to them, but the file has a fourth, unneeded column. In this case, specify-H "columns: c1, c2, c3, temp", using a placeholder for the extra column. -
Example 3: The table has three columns
year, month, day, but the source file has only a single timestamp column in the2018-06-01 01:02:03format. You can derive the table columns by specifying-H "columns: col, year = year(col), month=month(col), day=day(col)".
-
-
-
CSV-specific parameters
Parameter
Required
Description
column_separatorNo
The column separator in the source data file. Default:
\t.For non-printable characters, prefix with
\xand use a hexadecimal value. For example, for the Hive separator\x01, specify-H "column_separator:\x01".row_delimiterNo
The row delimiter in the source data file. Default:
\n.ImportantWhen using
curl, the shell might interpret \n as a literal string instead of a newline character.In Bash, use the
$'string'syntax to ensure escape sequences like\nand\tare interpreted correctly. For example:-H $'row_delimiter:\n'.skip_headerNo
The number of header rows to skip at the beginning of the CSV file. The value must be an integer. Default: 0.
Use this parameter to ignore metadata rows, such as column names or types, in the CSV file. For example, setting this parameter to 1 skips the first row.
The row delimiter used here must match the one specified in your load command.
whereNo
A condition to filter which rows to load.
For example, to load only rows where the k1 column equals 20180601, specify
-H "where: k1 = 20180601".max_filter_ratioNo
The maximum ratio of rows that can be discarded due to data quality errors. Default:
0(zero tolerance).NoteThis does not apply to rows filtered by the where condition.
partitionsNo
The destination partitions for the load job.
We recommend specifying this parameter if you can determine the target partitions. Data for other partitions will be discarded. For example, to load data into partitions p1 and p2, specify
-H "partitions: p1, p2".timeoutNo
The timeout for the load job, in seconds. Default: 600.
The value must be between 1 and 259200.
strict_modeNo
Enables or disables strict mode for this load job. Valid values:
-
false(default): Strict mode is disabled. -
true: Strict mode is enabled. When enabled, strict filtering is applied to column type conversions during loading.
timezoneNo
The time zone for the load job. Default:
Asia/Shanghai(UTC+08:00).This parameter affects all time-zone-related functions used in the load.
exec_mem_limitNo
The memory limit for the load job. Default: 2 GB.
-
-
JSON-specific parameters
Parameter
Required
Description
jsonpathsNo
Specifies the fields to extract from the JSON data. This is required only when you need to map specific source fields to table columns. The value must be in JSON format.
strip_outer_arrayNo
Specifies whether to strip the outermost array structure. Valid values:
-
false(default): Retains the original JSON structure. The entire JSON array is imported as a single value.For example, for the data
[{"k1" : 1, "k2" : 2},{"k1" : 3, "k2" : 4}], settingstrip_outer_arraytofalseimports the data as a single array. -
true: If the data to be loaded is a JSON array, you need to set
strip_outer_arrayto true.For example, for the data
[{"k1" : 1, "k2" : 2},{"k1" : 3, "k2" : 4}], settingstrip_outer_arraytotrueimports the data as two separate rows.
json_rootNo
Specifies the root element of the JSON data to import. This parameter is required only when you load JSON data by matching fields. The value must be a valid JsonPath string. Default: empty, which means the entire JSON file is imported.
ignore_json_sizeNo
Specifies whether to skip the size check for the JSON body in the HTTP request.
NoteBy default, the JSON body size in an HTTP request cannot exceed 100 MB. If the size exceeds this limit, an error is returned:
"The size of this batch exceed the max size [104857600] of json type data data [8617627793]. Set ignore_json_size to skip check, although it may lead huge memory consuming."To load larger files, add the header"ignore_json_size:true"to your request to bypass this check.compression, Content-EncodingNo
The compression algorithm used for the request body. Supported algorithms: GZIP, BZIP2, LZ4_FRAME, and ZSTD.
For example:
curl --location-trusted -u root: -v 'http://127.0.0.1:18030/api/db0/tbl_simple/_stream_load' \-X PUT -H "expect:100-continue" \-H 'format: json' -H 'compression: lz4_frame' -T ./b.json.lz4. -
-
-
opt_properties: Specifies optional parameters for the import. The specified parameters apply to the entire import task.The format is as follows:
-H "label: <label_name>" -H "where: <condition1>[, <condition2>, ...]" -H "max_filter_ratio: <num>" -H "timeout: <num>" -H "strict_mode: true | false" -H "timezone: <string>" -H "load_mem_limit: <num>" -H "partial_update: true | false" -H "partial_update_mode: row | column" -H "merge_condition: <column_name>"Parameter
Required
Description
labelNo
A unique label for the load job. StarRocks prevents using the same label for more than one successful job.
You can use labels to prevent duplicate loads. StarRocks retains labels from successful jobs for 30 minutes.
whereNo
Specifies a filter condition. StarRocks applies this filter to the data after any transformations from the
columnsparameter. Only data that satisfies thewhereclause is imported.For example, to import only the rows where the k1 column is 20180601, specify
-H "where: k1 = 20180601".max_filter_ratioNo
The maximum ratio of rows that can be discarded due to data quality errors. Default:
0(zero tolerance).NoteThis does not apply to rows filtered by the
wherecondition.log_rejected_record_numNo
Specifies the maximum number of rejected rows to log due to data quality issues. Supported since v3.1. Valid values:
0,-1, or a positive integer. Default:0.-
0: No rejected rows are logged. -
-1: All rejected rows are logged. -
A positive integer, such as
n: A maximum ofnrejected rows are logged on each BE (or CN) node.
timeoutNo
Load job timeout in seconds. Default: 600.
The value must be between 1 and 259200.
strict_modeNo
Specifies whether to enable strict mode for this load job.
-
false(default): Strict mode is disabled. -
true: Strict mode is enabled.
timezoneNo
The time zone for the load job. Default:
Asia/Shanghai(UTC+08:00).This parameter affects all time-zone-related functions used in the load.
load_mem_limitNo
The memory limit for the load job. Default: 2 GB.
partial_updateNo
Enables partial column updates. Valid values:
trueandfalse. Default:false.partial_update_modeNo
Specifies the mode for partial updates. Valid values:
rowandcolumn.-
row(default): Row mode. Best for real-time updates with many columns in small batches. -
column: Column mode. Best for batch updates of a few columns across many rows. This mode can significantly improve performance.For example, if you update 10 out of 100 columns (10%) for all rows in a table, column mode can improve performance by up to 10x.
merge_conditionNo
Specifies the column name to use as the update condition. An update to a row only occurs if the new data's value for this column is greater than or equal to the existing value.
NoteThe specified column must not be part of the primary key. This feature is available only for primary key tables.
-
Example
This example loads data.csv into the example_table table in the load_test database of a StarRocks cluster. For a complete example, see Complete data loading example.
curl --location-trusted -u "root:" \
-H "Expect:100-continue" \
-H "label:label2" \
-H "column_separator: ," \
-T data.csv -XPUT \
http://172.17.**.**:18030/api/load_test/example_table/_stream_load
Return value
When a load task completes, it returns the result in JSON format. The following is a sample.
{
"TxnId": 9,
"Label": "label2",
"Status": "Success",
"Message": "OK",
"NumberTotalRows": 4,
"NumberLoadedRows": 4,
"NumberFilteredRows": 0,
"NumberUnselectedRows": 0,
"LoadBytes": 45,
"LoadTimeMs": 235,
"BeginTxnTimeMs": 101,
"StreamLoadPlanTimeMs": 102,
"ReadDataTimeMs": 0,
"WriteDataTimeMs": 11,
"CommitAndPublishTimeMs": 19
}
The return value includes the following parameters:
|
Parameter |
Description |
|
|
The transaction ID of the load task. |
|
|
The label of the load task. |
|
|
The load status. Valid values are:
|
|
|
This field indicates the status of an import task that corresponds to a label that already exists. The field is displayed only when the
|
|
|
Details about the load status. If a load fails, this parameter provides the reason. |
|
|
The total number of rows read from the data stream. |
|
|
The number of rows loaded. This parameter applies only when |
|
|
The number of rows filtered out due to poor data quality. |
|
|
The number of rows filtered out by the |
|
|
The size of the source file in bytes. |
|
|
The duration of the load task, in milliseconds. |
|
|
The time taken to start the transaction, in milliseconds. |
|
|
The time taken to generate the execution plan, in milliseconds. |
|
|
The time taken to read data, in milliseconds. |
|
|
The time taken to write data, in milliseconds. |
|
|
Returned only when the load fails. You can use You can run the For example, to download the error row information:
The exported error rows are saved to a local file named Adjust the load task based on the error information, then retry it. |
Cancel load task
You cannot manually cancel a Stream Load task. The system automatically cancels the task if it times out or an import error occurs. Use the ErrorURL in the response to download the error details for troubleshooting.
Data loading example
This example shows how to submit a load task using the curl command.
-
Create a destination table.
-
Log in to the master node of the StarRocks cluster using SSH. For more information, see Log in to a cluster.
-
Run the following command to connect to the StarRocks cluster using a MySQL client.
mysql -h127.0.0.1 -P 9030 -uroot -
Run the following commands to create the database and table.
CREATE DATABASE IF NOT EXISTS load_test; USE load_test; CREATE TABLE IF NOT EXISTS example_table ( id INT, name VARCHAR(50), age INT ) DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 3 PROPERTIES ( "replication_num" = "1" -- Sets the replication number to 1. );Press
Ctrl+Dto exit the MySQL client.
-
-
Prepare test data.
CSV data
For example, create a file named
data.csvwith the following content:id,name,age 1,Alice,25 2,Bob,30 3,Charlie,35JSON data
For example, create a file named
json.datawith the following content:{"id":1,"name":"Emily","age":25} {"id":2,"name":"Benjamin","age":35} {"id":3,"name":"Olivia","age":28} {"id":4,"name":"Alexander","age":60} {"id":5,"name":"Ava","age":17} -
Run the following command to submit a load task.
CSV data
curl --location-trusted -u "root:" \ -H "Expect:100-continue" \ -H "label:label1" \ -H "column_separator: ," \ -T data.csv -XPUT \ http://172.17.**.**:18030/api/load_test/example_table/_stream_loadJSON data
curl --location-trusted -u "root:" \ -H "Expect:100-continue" \ -H "label:label2" \ -H "format:json" \ -T json.data -XPUT \ http://172.17.**.**:18030/api/load_test/example_table/_stream_load
Code integration examples
-
Developing Stream Load jobs in Java. See stream_load for details.
-
Integrating Stream Load with Spark. See 01_sparkStreaming2StarRocks for details.