You can load data into tables only as a user who has the INSERT privilege on those 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 cluster. The syntax is
GRANT INSERT ON TABLE <table_name> IN DATABASE <database_name> TO { ROLE <role_name> | USER <user_identity>}.
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 table, the data of the data file may not be completely mapped onto the data of the table. In this situation, you do not need to extract or transform the data before you load it into the table. The database 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 table, you can create a column mapping between the data file and the table.
- Filter out rows you do not want to load. You can specify filter conditions based on which the database 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 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.b. Create a data file namedfile2.csv. The file consists of only one column, which represents date. -
Create tables in your database
test_db.NOTE Since v2.5.7, the database 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 set the number of buckets.
a. Create a table namedtable1, which consists of three columns:event_date,event_type, anduser_id.b. Create a table namedtable2, which consists of four columns:date,year,month, andday. -
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 table may contain some columns that cannot be mapped to any columns of the table. In this situation, the database supports loading only the columns that can be mapped from the data file onto the columns of the 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
- The data of the columns that can be mapped onto and are temporarily named by using the names of the columns in the table is directly loaded.
- The columns that cannot be mapped onto the columns of the table are ignored, the data of these columns are not loaded.
- If some columns can be mapped onto the columns of the table but are not temporarily named in the job creation statement or command, the load job reports errors.
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
Iffile1.csv is stored in your local file system, run the following command to create a Stream Load job:
NOTE
If you choose Stream Load, you must use the columns parameter to temporarily name the columns of the data file to create a column mapping between the data file and the table.
For detailed syntax and parameter descriptions, see STREAM LOAD.
Load data from an HDFS cluster
Iffile1.csv is stored in your HDFS cluster, execute the following statement to create a Broker Load job:
NOTE
If you choose Broker Load, you must use the column_list parameter to temporarily name the columns of the data file to create a column mapping between the data file and the table.
For detailed syntax and parameter descriptions, see BROKER LOAD.
Load data from a Kafka cluster
If the data offile1.csv is published to topic1 of your Kafka cluster, execute the following statement to create a Routine Load job:
NOTE
If you choose Routine Load, you must use the COLUMNS parameter to temporarily name the columns of the data file to create a column mapping between the data file and the table.
For detailed syntax and parameter descriptions, see CREATE ROUTINE LOAD.
Query data
After the load of data from your local file system, HDFS cluster, or Kafka cluster is complete, query the data oftable1 to verify that the load is successful:
Filter out rows that you do not want to load
When you load a data file into a table, you may not want to load specific rows of the data file. In this situation, you can use the WHERE clause to specify the rows that you want to load. The database filters out all rows that do not meet the filter conditions specified in the WHERE clause. 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
file1.csv and table1 as an example. If you want to load only the rows whose event type is 1 from file1.csv into table1, you can use the WHERE clause to specify a filter condition event_type = 1.
Load data
Load data from a local file system
Iffile1.csv is stored in your local file system, run the following command to create a Stream Load job:
Load data from an HDFS cluster
Iffile1.csv is stored in your HDFS cluster, execute the following statement to create a Broker Load job:
Load data from a Kafka cluster
If the data offile1.csv is published to topic1 of your Kafka cluster, execute the following statement to create a Routine Load job:
Query data
After the load of data from your local file system, HDFS cluster, or Kafka cluster is complete, query the data oftable1 to verify that the load is successful:
Generate new columns from original columns
When you load a data file into a table, some data of the data file may require conversions before the data can be loaded into the table. In this situation, you can use functions or expressions in the job creation command or statement to implement data conversions. 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
file2.csv and table2 as an example. file2.csv consists of only one column that represents date. You can use the year, month, and day functions to extract the year, month, and day in each date from file2.csv and load the extracted data into the year, month, and day columns of table2.
Load data
Load data from a local file system
Iffile2.csv is stored in your local file system, run the following command to create a Stream Load job:
NOTEFor detailed syntax and parameter descriptions, see STREAM LOAD.
- In the
columnsparameter, you must first temporarily name all columns of the data file, and then temporarily name the new columns that you want to generate from the original columns of the data file. As shown in the preceding example, the only column offile2.csvis temporarily named asdate, and then theyear=year(date),month=month(date), andday=day(date)functions are invoked to generate three new columns, which are temporarily named asyear,month, andday.- Stream Load does not support
column_name = function(column_name)but supportscolumn_name = function(column_name).
Load data from an HDFS cluster
Iffile2.csv is stored in your HDFS cluster, execute the following statement to create a Broker Load job:
NOTE You must first use theFor detailed syntax and parameter descriptions, see BROKER LOAD.column_listparameter to temporarily name all columns of the data file, and then use the SET clause to temporarily name the new columns that you want to generate from the original columns of the data file. As shown in the preceding example, the only column offile2.csvis temporarily named asdatein thecolumn_listparameter, and then theyear=year(date),month=month(date), andday=day(date)functions are invoked in the SET clause to generate three new columns, which are temporarily named asyear,month, andday.
Load data from a Kafka cluster
If the data offile2.csv is published to topic2 of your Kafka cluster, execute the following statement to create a Routine Load job:
NOTE In theFor detailed syntax and parameter descriptions, see CREATE ROUTINE LOAD.COLUMNSparameter, you must first temporarily name all columns of the data file, and then temporarily name the new columns that you want to generate from the original columns of the data file. As shown in the preceding example, the only column offile2.csvis temporarily named asdate, and then theyear=year(date),month=month(date), andday=day(date)functions are invoked to generate three new columns, which are temporarily named asyear,month, andday.
Query data
After the load of data from your local file system, HDFS cluster, or Kafka cluster is complete, query the data oftable2 to verify that the load is successful:
Extract partition field values from a file path
If the file path that you specify contains partition fields, you can use theCOLUMNS FROM PATH AS parameter to specify the partition fields you want to extract from the file paths. The partition fields in file paths are equivalent to the columns in data files. The COLUMNS FROM PATH AS parameter is supported only when you load data from an HDFS cluster.
For example, you want to load the following four data files generated from Hive:
/user/starrocks/data/input/ path of your HDFS cluster. Each of these data files is partitioned by partition field date and consists of two columns, which represent event type and user ID in sequence.
Load data from an HDFS cluster
Execute the following statement to create a Broker Load job, which enables you to extract thedate partition field values from the /user/starrocks/data/input/ file path and use a wildcard (*) to specify that you want to load all data files in the file path to table1:
NOTE In the preceding example, theFor detailed syntax and parameter descriptions, see BROKER LOAD.datepartition field in the specified file path is equivalent to theevent_datecolumn oftable1. Therefore, you need to use the SET clause to map thedatepartition field onto theevent_datecolumn. If the partition field in the specified file path has the same name as a column of the table, you do not need to use the SET clause to create a mapping.
Query data
After the load of data from your HDFS cluster is complete, query the data oftable1 to verify that the load is successful: