建表参数
这些参数中的大部分都是可选的,并且可以根据具体的使用情况进行设置。在创建Hudi表之前,建议仔细阅读Hudi文档,了解每个参数的含义和用法。此处列举常见的几种。
参数名 | 默认值 | 说明 |
---|---|---|
hudi.table.type | COPY_ON_WRITE | 表类型,可以是COPY_ON_WRITE或MERGE_ON_READ。 |
hudi.table.name | 无默认值 | 表名称。 |
hudi.enable.timeline.server | false | 启用或禁用Hudi时间线服务器。 |
hudi.cleaner.policy | KEEP_LATEST_COMMITS | 数据清理策略,可以是默认的保留最新版本,也可以是保留最新N个版本。 |
hudi.write.payload.class | org.apache.hudi.DefaultHoodieRecordPayload | 写入负载的类,用于自定义写入逻辑。 |
hudi.key.generator.class | org.apache.hudi.SimpleKeyGenerator | 键生成器的类,用于生成用于唯一标识表中每个记录的键。 |
hudi.record.key.field | "uuid" | 记录键字段的名称。 |
hudi.precombine.field | 无默认值 | 在进行合并时用于排序和去重的字段名称。 |
hudi.hive.use.jdbc | false | 是否使用JDBC连接Hive。 |
hudi.hive.database | "default" | Hive数据库名称。 |
创建非分区表
创建一个cow表,默认primaryKey 'uuid',不提供preCombineField
create table hudi_cow_nonpcf_tbl (
uuid int,
name string,
price double
) using hudi;
创建一个mor非分区表
create table hudi_mor_tbl (
id int,
name string,
price double,
ts bigint
) using hudi
tblproperties (
type = 'mor',
primaryKey = 'id',
preCombineField = 'ts'
);
创建分区表
创建一个cow分区外部表,指定primaryKey和preCombineField
create table hudi_cow_pt_tbl (
id bigint,
name string,
ts bigint,
dt string,
hh string
) using hudi
tblproperties (
type = 'cow',
primaryKey = 'id',
preCombineField = 'ts'
)
partitioned by (dt, hh)
location '/tmp/hudi/hudi_cow_pt_tbl';
在已有的hudi表上创建新表
不需要指定模式和非分区列(如果存在)之外的任何属性,Hudi可以自动识别模式和配置。
非分区表
create table hudi_existing_tbl0 using hudi
location 'file:///tmp/hudi/dataframe_hudi_nonpt_table';
分区表
create table hudi_existing_tbl1 using hudi
partitioned by (dt, hh)
location 'file:///tmp/hudi/dataframe_hudi_pt_table';
通过CTAS (Create Table As Select)建表
为了提高向hudi表加载数据的性能,CTAS使用批量插入作为写操作。
通过CTAS创建cow非分区表,不指定preCombineField
create table hudi_ctas_cow_nonpcf_tbl
using hudi
tblproperties (primaryKey = 'id')
as
select 1 as id, 'a1' as name, 10 as price;
通过CTAS创建cow分区表,指定preCombineField
create table hudi_ctas_cow_pt_tbl
using hudi
tblproperties (type = 'cow', primaryKey = 'id', preCombineField = 'ts')
partitioned by (dt)
as
select 1 as id, 'a1' as name, 10 as price, 1000 as ts, '2021-12-01' as dt;
通过CTAS从其他表加载数据
创建内部表
create table parquet_mngd using parquet location 'file:///tmp/parquet_dataset/*.parquet';
通过CTAS加载数据
create table hudi_ctas_cow_pt_tbl2 using hudi location 'file:/tmp/hudi/hudi_tbl/' options (
type = 'cow',
primaryKey = 'id',
preCombineField = 'ts'
)
partitioned by (datestr) as select * from parquet_mngd;