gpt4 book ai didi

apache-spark - 每天自动更新一个 Hive View

转载 作者:可可西里 更新时间:2023-11-01 14:43:33 27 4
gpt4 key购买 nike

我有一个要求要满足。我需要将数据从 DB sqoop 到 Hive。我每天都在 sqooping,因为这些数据每天都会更新。

此数据将用作来自 spark 消费者的查找数据以进行充实。我们想保留我们收到的所有数据的历史记录,但我们不需要所有数据来查找最新数据(同一天)。我正在考虑从历史表中创建一个配置单元 View ,并且只显示当天插入的记录。有没有一种方法可以每天自动执行 View ,以便 View 查询始终具有最新数据?

最佳答案

Q: Is there a way to automate the view on a daily basis so that the view query will always have the latest data?

如果您获得基于日期的分区表,则无需更新/自动化该过程。

Q: We want to keep a history of all the data we have received but we don't need all the data for lookup only the latest data (same day).


注意:无论是配置单元 View 还是配置单元表,您都应该始终避免扫描全表数据,也就是全表扫描以获取最新的分区数据。

选项 1:查询数据的配置单元方法

如果你想适应配置单元的方法

您必须使用分区列,例如:partition_date 和 hive 中的分区表

 select *  from table where partition_column in 
(select max(distinct partition_date ) from yourpartitionedTable)

  select  * from (select *,dense_rank() over (order by partition_date  desc) dt_rnk from db.yourpartitionedTable ) myview
where myview.dt_rnk=1

总是给出最新的分区。 (如果分区数据中有同一天或今天的日期,那么它会给出同一天的分区数据,否则它会给出最大 partition_date)及其来自分区表的数据。

选项 2:查询数据的普通 spark 方法使用 spark show partitions 命令,即 spark.sql(s"show Partitions $yourpartitionedtablename") 在数组中获取结果并对其进行排序以获得最新的分区日期。使用它,您可以使用 spark 组件仅查询最新的分区日期作为查找数据。

将我的回答视为 getting latest partition date. 的想法

I prefer option2 since no hive query is needed and no full table query since we are using show partitions command. and no performance bottle necks and speed will be there.

另一个不同的想法是使用 HiveMetastoreClient 或选项 2 进行查询...请参阅此和 my answerother

关于apache-spark - 每天自动更新一个 Hive View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com