Configuring Default Values for Hudi Data Columns
Scenarios
Configuring default values for Hudi data columns allows you to assign initial values to new columns when they are added to a table. When historical data is queried, new columns return their default values to ensure data consistency and integrity.
By configuring default values for Hudi columns, you ensure data consistency and integrity across various scenarios. This not only simplifies data processing and query logic but also improves data quality and system stability. In practical applications, selecting appropriate default values based on specific requirements helps you better meet business needs.
- Historical data migration: During migration, data from an old table is moved to a new table that includes additional columns.
- Data model evolution: As business requirements change, new columns are added to existing tables to record additional information.
- Data backup and restoration: During backup and restoration, you must ensure that the restored data remains consistent and complete.
- Business logic requirements: Certain business rules may require specific columns to contain values in every row, even if those values are defaults.
- Data audit and logging: During auditing and logging, the initial or default values of specific columns must be recorded.
Notes and Constraints
- This section applies only to MRS 3.3.0-LTS or later.
- If data has been rewritten before default values are set for a new column, the default values of the column cannot be returned when historical data is queried. In this case, NULL values are returned. Some or all data will be rewritten when data is imported to the database, updated, compacted, or clustered.
- The default values of a column must match the column type. If they do not match, the type will be forcibly converted. This loses the precision of the default values or changes the default values to NULL.
- The default values of historical data are the default values set for the column for the first time. Changing the default values of a column for multiple times does not affect the query result of historical data.
- After the default value is set, it cannot be rolled back.
- Currently, Spark SQL does not support the function of viewing default column values. You can run the show create table command on Hive beeline to view default column values.
Scope
Currently, only the int, bigint, float, double, decimal, string, date, timestamp, boolean, and binary data types are supported.
| Engine | DDL Operation | Support for Write Operation | Support for Read Operation |
|---|---|---|---|
| SparkSQL | Y | Y | Y |
| Spark DataSource | N | N | Y |
| Flink | N | N | Y |
| HetuEngine | N | N | Y |
| Hive | N | N | Y |
Example
For details about the SQL syntax, see Hudi SQL Syntax Reference.
The following is an example:
- Create a table and specify default values for columns.
create table if not exists h3( id bigint, name string, price double default 12.34 ) using hudi options ( primaryKey = 'id', type = 'mor', preCombineField = 'name' );
- Add columns and specify default values for the columns.
alter table h3 add columns(col1 string default 'col1_value'); alter table h3 add columns(col2 string default 'col2_value', col3 int default 1);
- Change default values of columns.
alter table h3 alter column price set default 14.56;
- Inset data and use column default values.
insert into h3(id, name) values(1, 'aaa'); insert into h3(id, name, price) select 2, 'bbb', 12.5;
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot