Hudi集成Spark

浩客1年前技术文章929

环境准备


安装Spark

1)Hudi支持的Spark版本

Hudi

Supported Spark 3 version

0.12.x

3.3.x3.2.x3.1.x

0.11.x

3.2.xdefault build, Spark bundle only),3.1.x

0.10.x

3.1.x(default build), 3.0.x

0.7.0-0.9.0

3.0.x

0.6.0 and prior

Not supported

2)下载Spark并安装配置好
# 拷贝编译好的包到spark的jars目录

cp /opt/hudi-0.12.0/packaging/hudi-spark-bundle/target/hudi-spark3.2-bundle_2.12-0.12.0.jar /opt/spark-3.2.2/jars

# 不自己编译,去maven里面下载对应版本的jar包放到spark的jars目录下也可以
https://search.maven.org/artifact/org.apache.hudi/hudi-spark3.3-bundle_2.12/0.13.1/jar


Spark SQL方式


创建表

1)启动spark-sql

spark-sql \ 
  --master yarn --deploy-mode client \
  --conf 'spark.serializer=org.apache.spark.serializer.KryoSerializer' \
  --conf 'spark.sql.catalog.spark_catalog=org.apache.spark.sql.hudi.catalog.HoodieCatalog' \
  --conf 'spark.sql.extensions=org.apache.spark.sql.hudi.HoodieSparkSessionExtension'

WPS图片(1).png

2)创建分区表

# 创建一个cow分区外部表,指定primaryKey和preCombineField

create table spark_hudi (
    id int, name string,price double, ts bigint
) using hudi 
tblproperties (type = 'cow', primaryKey = 'id', preCombineField = 'ts');

WPS图片2.png



3)向分区表插入数据

# 默认情况下,如果提供了preCombineKey,则insert into的写操作类型为upsert,否则使用insert。

insert into spark_hudi select 1, 'a1', 20, 1000;

WPS图片3.png

4)时间旅行查询

# 修改id为1的数据

insert into spark_hudi select 1, 'a1_1', 20,1000;

# 再基于第一次提交时间进行时间旅行查询

select * from spark_hudi timestamp as of '20231126202835692' where id = 1;

# 再次查询会发现查询结果为第一次插入的数据

WPS图片4.png

5update

# 更新操作需要指定preCombineField

update spark_hudi set price = price * 2, ts = 1111 where id = 1;

WPS图片7.png

6执行mergeinto

# 准备source表并插入数据

create table merge_source (
   id int, name string, price double, ts bigint
) using hudi 
tblproperties (primaryKey = 'id', preCombineField = 'ts'); 

insert into merge_source values (1, "old_a1", 22.22, 2900), (2, "new_a2", 33.33, 2000), (3, "new_a3", 44.44, 2000);  

merge into spark_hudi 
  as target using merge_source as source 
  on target.id = source.id 
  when matched then update 
  set * when not matched then insert *;

WPS图片8.png

7)执行delete

delete from spark_hudi where id = 1;

WPS图片9.png

8)执行bulk_insert

set hoodie.sql.bulk.insert.enable=true; 
set hoodie.sql.insert.mode=non-strict; 
insert into spark_hudi select 2, 'a1_2', 20, 1002;

WPS图片11.png

相关文章

MySQL keepalived安装配置(二)

MySQL keepalived安装配置(二)

一、keepalived安装配置1.1、主备库安装keepalived服务:yum install -y keepalived yum install -y python MySQL-python1...

oracle数据库日志清理

1、查看日志执行命令:SQL> show parameter dest;找到audit_file_dest,background_dump_dest,user_dump_dest,core_du...

HDP实操--NameNode开启高可用

HDP实操--NameNode开启高可用

为了确定在namenode组件失败后集群中有其他的namenode可以工作,需要对hdp集群配置高可用,当前我们配置的非安全集群的高可用。前置条件:(1)确保你的集群至少有3个节点并且至少有3个Apa...

oracle自带存储过程的压测使用

1、使用前提条件:A、timed_statistics参数为true B、sysdba权限 C、11g及以上版本 D、ASYNCH_IO开启通过运行以下查询,确保为数据文件启用异步 I/OCOL NA...

docker私有仓库搭建及containerd使用私有仓库

docker私有仓库搭建及containerd使用私有仓库

这里我们要搭建的私有仓库非harbor,而是更轻量的docker-registry。使用的工具是containerd私有仓库搭建```Plain Text创建目录mkdir -p /opt/docke...

Flume使用详解

Flume使用详解

一、Flume概念Flume 是 Cloudera 提供的日志收集系统,具有分布式、高可靠、高可用性等特点,对海量 日志采集、聚合和传输,Flume 支持在日志系统中定制各类数据发送方,同时,Flum...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。