Hudi基础 -- Spark SQL DDL

本文介绍了如何在Spark中使用Hudi创建不同类型的表,包括cow类型的无主键表和有主键表,以及mor类型并设置主键更新的表。此外,还展示了如何创建分区表以及通过CTAS创建非分区和分区主键表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键参数:
参数名称
描述
可选项:默认值
primaryKey
表的主键名称,组合主键使用逗号分隔;
(Optional) :  id
type
表类型:cow’ 或 ‘mor’,默认是cow;
(Optional) :  cow
preCombineField
当数据的主键相同时,会根据这个字段判断是否要更新此主键的数据。 不指定默认保留 最新 ;
(Optional) :  ts

1.创建一个cow类型的表

-- create a non-primary key table
create table if not exists hudi_table2(
  id int,
  name string,
  price double
) using hudi
options (
  type = 'cow'
);

2.创建一个cow类型,主键为ID的表

-- create a managed cow table
create table if not exists hudi_table0 (
  id int,
  name string,
  price double
) using hudi
options (
  type = 'cow',
  primaryKey = 'id'
);

3.创建mor类型主键更新的表

create table if not exists hudi_table1 (
  id int,
  name string,
  price double,
  ts bigint
) using hudi
options (
  type = 'mor',
  primaryKey = 'id,name',
  preCombineField = 'ts'
);

4. 创建分区表

create table if not exists hudi_table_p0 (
id bigint,
name string,
dt string,
hh string  
) using hudi
options (
  type = 'cow',
  primaryKey = 'id',
  preCombineField = 'ts'
)
partitioned by (dt, hh);

5.CTAS(Create table as select) 创建表

Hudi支持 CTAS(Create table as select)的方式创建表

5.1 CTAS创建非分区表

create table h3 using hudi
as
select 1 as id, 'a1' as name, 10 as price;

5.2 CTAS创建分区、主键表

create table h2 using hudi
options (type = 'cow', primaryKey = 'id')
partitioned by (dt)
as
select 1 as id, 'a1' as name, 10 as price, 1000 as dt;
更多的 ddl 参考 SQL DDL | Apache Hudi
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值