4

我在蜂巢/黑斑羚中有两张桌子。我想将表中的数据作为 rdds 提取到 spark 中并执行连接操作。

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

假设我有两个 rdds:

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

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

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

理想情况下,我想使用 rdds 使用 spark shell 来做这样的事情。

select * from tem1 join tem2 on tem1.account_id=tem2.account_id; 
4

4 回答 4

2

我不确定我是否理解了这个问题,但作为替代方案,您可以使用 API 来加入 DataFrame,因此您可以通过编程方式决定许多事情(例如,该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

干杯

于 2016-05-03T20:30:10.147 回答
1

So we could register table1 and table2 as temporary tables and then do the join on these temporary tables.

table1.registerTempTable("t1")
table2.registerTempTable("t2")
table3 = hiveContext.hql("select * from t1 join t2 on t1.account_id=t2.account_id")
于 2014-11-06T22:35:09.023 回答
0

table1 和 table2 是 DataFrame 类型。可以使用以下方法将它们转换为 rdd:

lazy val table1_rdd = table1.rdd
lazy val table2_rdd = table2.rdd

这应该是诀窍。在这些 rdd 上,您可以使用任何 rdd 操作。

另请参阅:https ://issues.apache.org/jira/browse/SPARK-6608和https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql .DataFrame

于 2015-06-19T14:39:35.600 回答
0

您可以从以下代码中直接选择所需的列:

val table1 =  hiveContext.hql("select account_id from tem1")
val table2 =  hiveContext.hql("select account_id from tem2")
val joinedTable = table1.join(table2) 
于 2017-02-01T20:15:19.437 回答