Transform data at loading
StarRocks supports data transformation at loading.
This feature supports Stream Load, Broker Load, and Routine Load but does not support Spark Load.
You can load data into StarRocks tables only as a user who has the INSERT privilege on those StarRocks tables. If you do not have the INSERT privilege, follow the instructions provided in GRANT to grant the INSERT privilege to the user that you use to connect to your StarRocks cluster.
This topic uses CSV data as an example to describe how to extract and transform data at loading. The data file formats that are supported vary depending on the loading method of your choice.
NOTE
For CSV data, you can use a UTF-8 string, such as a comma (,), tab, or pipe (|), whose length does not exceed 50 bytes as a text delimiter.
Scenarios
When you load a data file into a StarRocks table, the data of the data file may not be completely mapped onto the data of the StarRocks table. In this situation, you do not need to extract or transform the data before you load it into the StarRocks table. StarRocks can help you extract and transform the data during loading:
-
Skip columns that do not need to be loaded.
You can skip the columns that do not need to be loaded. Additionally, if the columns of the data file are in a different order than the columns of the StarRocks table, you can create a column mapping between the data file and the StarRocks table.
-
Filter out rows you do not want to load.
You can specify filter conditions based on which StarRocks filters out the rows that you do not want to load.
-
Generate new columns from original columns.
Generated columns are special columns that are computed from the original columns of the data file. You can map the generated columns onto the columns of the StarRocks table.
-
Extract partition field values from a file path.
If the data file is generated from Apache Hive™, you can extract partition field values from the file path.
Data examples
-
Create data files in your local file system.
a. Create a data file named
file1.csv. The file consists of four columns, which represent user ID, user gender, event date, and event type in sequence.354,female,2020-05-20,1
465,male,2020-05-21,2
576,female,2020-05-22,1
687,male,2020-05-23,2b. Create a data file named
file2.csv. The file consists of only one column, which represents date.2020-05-20
2020-05-21
2020-05-22
2020-05-23 -
Create tables in your StarRocks database
test_db.NOTE
Since v2.5.7, StarRocks can automatically set the number of buckets (BUCKETS) when you create a table or add a partition. You no longer need to manually set the number of buckets. For detailed information, see determine the number of buckets.
a. Create a table named
table1, which consists of three columns:event_date,event_type, anduser_id.MySQL [test_db]> CREATE TABLE table1
(
`event_date` DATE COMMENT "event date",
`event_type` TINYINT COMMENT "event type",
`user_id` BIGINT COMMENT "user ID"
)
DISTRIBUTED BY HASH(user_id);b. Create a table named
table2, which consists of four columns:date,year,month, andday.MySQL [test_db]> CREATE TABLE table2
(
`date` DATE COMMENT "date",
`year` INT COMMENT "year",
`month` TINYINT COMMENT "month",
`day` TINYINT COMMENT "day"
)
DISTRIBUTED BY HASH(date); -
Upload
file1.csvandfile2.csvto the/user/starrocks/data/input/path of your HDFS cluster, publish the data offile1.csvtotopic1of your Kafka cluster, and publish the data offile2.csvtotopic2of your Kafka cluster.
Skip columns that do not need to be loaded
The data file that you want to load into a StarRocks table may contain some columns that cannot be mapped to any columns of the StarRocks table. In this situation, StarRocks supports loading only the columns that can be mapped from the data file onto the columns of the StarRocks table.
This feature supports loading data from the following data sources:
-
Local file system
-
HDFS and cloud storage
NOTE
This section uses HDFS as an example.
-
Kafka
In most cases, the columns of a CSV file are not named. For some CSV files, the first row is composed of column names, but StarRocks processes the content of the first row as common data rather than column names. Therefore, when you load a CSV file, you must temporarily name the columns of the CSV file in sequence in the job creation statement or command. These temporarily named columns are mapped by name onto the columns of the StarRocks table. Take note of the following points about the columns of the data file:
-
The data of the columns that can be mapped onto and are temporarily named by using the names of the columns in the StarRocks table is directly loaded.
-
The columns that cannot be mapped onto the columns of the StarRocks table are ignored, the data of these columns are not loaded.
-
If some columns can be mapped onto the columns of the StarRocks table but are not temporarily named in the job creation statement or command, the load job reports errors.
This section uses file1.csv and table1 as an example. The four columns of file1.csv are temporarily named as user_id, user_gender, event_date, and event_type in sequence. Among the temporarily named columns of file1.csv, user_id, event_date, and event_type can be mapped onto specific columns of table1, whereas user_gender cannot be mapped onto any column of table1. Therefore, user_id, event_date, and event_type are loaded into table1, but user_gender is not.
Load data
Load data from a local file system
If file1.csv is stored in your local file system, run the following command to create a Stream Load job:
curl --location-trusted -u <username>:<password> \
-H "Expect:100-continue" \
-H "column_separator:," \
-H "columns: user_id, user_gender, event_date, event_type" \
-T file1.csv -XPUT \
http://<fe_host>:<fe_http_port>/api/test_db/table1/_stream_load
NOTE
If you choose Stream Load, you must use the
columnsparameter to temporarily name the columns of the data file to create a column mapping between the data file and the StarRocks table.
For detailed syntax and parameter descriptions, see STREAM LOAD.