gpt4 book ai didi

scala - 从 hive 表中获取数据到 spark 并在 RDD 上执行连接

转载 作者:行者123 更新时间:2023-12-01 02:13:53 27 4
gpt4 key购买 nike

我在 hive/impala 中有两张 table 。我想将表中的数据作为 rdds 提取到 spark 中并执行连接操作。

我不想在我的配置单元上下文中直接传递连接查询。这只是一个例子。
我有更多标准 HiveQL 无法实现的用例。
如何获取所有行、访问列并执行转换。

假设我有两个 rdd:

val table1 =  hiveContext.hql("select * from tem1")

val table2 = hiveContext.hql("select * from tem2")

我想在名为“account_id”的列上对 rdds 执行连接

理想情况下,我想使用 spark shell 使用 rdds 来做这样的事情。
select * from tem1 join tem2 on tem1.account_id=tem2.account_id; 

最佳答案

我不确定我是否理解这个问题,但作为替代方案,您可以使用 API 来加入数据帧,因此您可以通过编程方式决定许多事情(例如 join 函数可以作为参数传递给应用自定义的方法转型)。

对于您的示例,它会是这样的:

val table1 =  hiveContext.sql("select * from tem1")
val table2 = hiveContext.sql("select * from tem2")
val common_attributes = Seq("account_id")
val joined = table1.join(table2, common_attributes)

DataFrame API 中有许多常见的转换可用:
http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.DataFrame

干杯

关于scala - 从 hive 表中获取数据到 spark 并在 RDD 上执行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26785672/

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