save(“hdfs:///hudi/hudi_mor_tbl_shell”)
验证方法使用普通查询。
### Insert overwrite
import org.apache.spark.sql._
import org.apache.spark.sql.types._
val fields = Array(
StructField(“id”, IntegerType, true),
StructField(“name”, StringType, true),
StructField(“price”, DoubleType, true),
StructField(“ts”, LongType, true)
)
val simpleSchema = StructType(fields)
val data = Seq(Row(99, “a99”, 20.0, 900L))
val df = spark.createDataFrame(data, simpleSchema)
df.write.format(“hudi”).
option(OPERATION.key(),“insert_overwrite”).
option(PRECOMBINE_FIELD.key(), “ts”).
option(RECORDKEY_FIELD.key(), “id”).
option(TBL_NAME.key(), “hudi_mor_tbl_shell”).
option(TABLE_TYPE_OPT_KEY, “MERGE_ON_READ”).
mode(Append).
save(“hdfs:///hudi/hudi_mor_tbl_shell”)
验证方法使用普通查询。发现只有新增的这一条数据。
### 删除数据
import org.apache.spark.sql._
import org.apache.spark.sql.types._
val fields = Array(
StructField(“id”, IntegerType, true),
StructField(“name”, StringType, true),
StructField(“price”, DoubleType, true),
StructField(“ts”, LongType, true)
)
val simpleSchema = StructType(fields)
val data = Seq(Row(2, “a2”, 400.0, 2222L))
val df = spark.createDataFrame(data, simpleSchema)
df.write.format(“hudi”).
option(OPERATION_OPT_KEY,“delete”).
option(PRECOMBINE_FIELD_OPT_KEY, “ts”).
option(RECORDKEY_FIELD_OPT_KEY, “id”).
option(TABLE_NAME, “hudi_mor_tbl_shell”).
mode(Append).
save(“hdfs:///hudi/hudi_mor_tbl_shell”)
验证方法使用普通查询。
### Spark SQL方式
启动Hudi spark sql的方法:
./spark-sql
–master yarn
–conf ‘spark.serializer=org.apache.spark.serializer.KryoSerializer’
–conf ‘spark.sql.extensions=org.apache.spark.sql.hudi.HoodieSparkSessionExtension’
–conf ‘spark.sql.catalog.spark_catalog=org.apache.spark.sql.hudi.catalog.HoodieCatalog’
如果使用Hudi的版本为0.11.x,需要执行:
./spark-sql
–master yarn
–conf ‘spark.serializer=org.apache.spark.serializer.KryoSerializer’
–conf ‘spark.sql.extensions=org.apache.spark.sql.hudi.HoodieSparkSessionExtension’
创建表:
create table hudi_mor_tbl (
id int,
name string,
price double,
ts bigint
) using hudi
tblproperties (
type = ‘mor’,
primaryKey = ‘id’,
preCombineField = ‘ts’
)
location ‘hdfs:///hudi/hudi_mor_tbl’;
验证:
show tables;
### 插入数据
SQL方式:
insert into hudi_mor_tbl select 1, ‘a1’, 20, 1000;
验证:
select * from hudi_mor_tbl;
### 普通查询
SQL方式:
select * from hudi_mor_tbl;
### 修改数据
SQL方式:
update hudi_mor_tbl set price = price * 2, ts = 1111 where id = 1;
验证:
select * from hudi_mor_tbl;
### insert overwrite
SQL方式:
insert overwrite hudi_mor_tbl select 99, ‘a99’, 20.0, 900;
验证:
select * from hudi_mor_tbl;
发现只有新增的这一条数据。
### 删除数据
SQL方式:
delete from hudi_mor_tbl where id % 2 = 1;
验证:
select * from hudi_mor_tbl;
### Kerberos和权限配置
例如,如果要允许Hudi用户对Hudi表进行操作,提交队列为default,表路径为hdfs:///hudi/t1,可以通过以下步骤使用Ranger进行设置:
1、在Ranger中创建一个名为hudi的用户。
2、分配给hudi用户以下目录的读写权限:/hdfs/hudi/t1,/tmp,/user/hudi。
3、赋予hudi用户对yarn default队列的权限。
如果启用了Kerberos,还需要执行以下额外步骤:
1、在Kerberos中创建hudi@PAULTECH.COM主体,并生成相应的keytab文件。
2、在执行kinit之后,确保hudi用户具有相应的权限以执行相关操作。
通过这些设置,Hudi用户应该能够在指定的表路径下执行操作,并具有必要的HDFS和YARN权限,确保了对应用程序的顺利运行。
### FAQ
**1、spark-sql或者spark-shell启动出现NoClassDefFoundError: org/apache/hadoop/shaded/javax/ws/rs/core/NoContentException**
问题日志:
Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/hadoop/shaded/javax/ws/rs/core/NoContentException
at org.apache.hadoop.yarn.util.timeline.TimelineUtils.(TimelineUtils.java:60)
at org.apache.hadoop.yarn.client.api.impl.YarnClientImpl.serviceInit(YarnClientImpl.java:200)
at org.apache.hadoop.service.AbstractService.init(AbstractService.java:164)
at org.apache.spark.deploy.yarn.Client.submitApplication(Client.scala:191)
at org.apache.spark.scheduler.cluster.YarnClientSchedulerBackend.start(YarnClientSchedulerBackend.scala:62)
at org.apache.spark.scheduler.TaskSchedulerImpl.start(TaskSchedulerImpl.scala:222)
at org.apache.spark.SparkContext.(SparkContext.scala:585)
at org.apache.spark.SparkContext . g e t O r C r e a t e ( S p a r k C o n t e x t . s c a l a : 2704 ) a t o r g . a p a c h e . s p a r k . s q l . S p a r k S e s s i o n .getOrCreate(SparkContext.scala:2704) at org.apache.spark.sql.SparkSession .getOrCreate(SparkCont<