Hive不支援寫入資料到Delta Lake和Hudi,但是可以通過外部表格的方式查詢Delta Lake和Hudi中的資料。本文通過樣本為您介紹如何使用EMR上的Hive訪問Delta Lake和Hudi資料。
前提條件
使用限制
EMR-3.36.0及後續版本和EMR-5.2.0及後續版本,支援Hive對Hudi進行讀操作。
Hive訪問Delta Lake資料
執行以下命令,進入Spark命令列。
spark-sql在Spark中建立並查詢表資料。
執行以下命令,在Spark中建立Delta表。
create table delta_table (id int) using delta;執行以下命令,向表中插入資料。
insert into delta_table values 0,1,2,3,4;執行以下命令,查看錶資料。
select * from delta_table;返回包含如下的資訊。
2 3 4 0 1 Time taken: 1.847 seconds, Fetched 5 row(s)
在Hive中查看Delta Lake資料。
執行以下命令,進入Hive命令列。
hive執行以下命令,在Hive中查看Delta Lake表。
desc formatted delta_table;執行以下命令,在Hive中查看Delta Lake表的資料。
select * from delta_table;返回如下資訊。
OK 2 3 4 0 1 Time taken: 1.897 seconds, Fetched: 5 row(s)說明查看資料與在Spark中插入的資料一致,說明Hive已經成功訪問了Delta Lake的資料。
Hive訪問Hudi資料
Spark讀寫Hudi
啟動方式
Spark2和Spark3 hudi0.11以下版本
spark-sql \ --conf 'spark.serializer=org.apache.spark.serializer.KryoSerializer' \ --conf 'spark.sql.extensions=org.apache.spark.sql.hudi.HoodieSparkSessionExtension'Spark3 hudi0.11及以上版本
spark-sql \ --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'
使用樣本
建表
create table h0 ( id bigint, name string, price double, ts long ) using hudi tblproperties ( primaryKey="id", preCombineField="ts" );查看錶詳情
desc formatted h0;返回資訊如下所示。
_hoodie_commit_time string _hoodie_commit_seqno string _hoodie_record_key string _hoodie_partition_path string _hoodie_file_name string id bigint name string price double ts bigint說明_hoodie_commit_time、_hoodie_commit_seqno、_hoodie_record_key、_hoodie_partition_path、_hoodie_file_name為Hudi預設添加的輔助欄位。資料操作
-- insert insert into h0 values (1, 'a1', 10, 1000), (2, 'a2', 11, 1000); -- update update h0 set name = 'a1_new' where id = 1; -- delete delete from h0 where id = 1;查詢
樣本1
select id, name, price, ts from h0;查詢結果如下所示。
2 a2 11.0 1000樣本2
select * from h0;查詢結果如下所示。
4.820221130150621338 20221130150621338_0_1 id:2 40d6507e-0579-42ce-a10f-c5e07a3981e5-0_0-29-2007_2022113015062****.parquet 2 a2 11.0 1000說明由於當前為非分區表,所以
_hoodie_partition_path為空白。前四個欄位為Hudi預設添加的輔助欄位的值。
更多Spark讀寫Hudi樣本,請參見Spark Guide。
Hive查詢Hudi
Hudi僅支援Spark或者Flink建表和寫入資料,Hive僅支援查詢。
直接輸入hive命令進入Hive CLI命令列或者通過Beeline串連Hive ThriftServer,然後執行SQL語句即可。樣本如下。
-- select
select * from h0;