Spark连接两个大表并存储在Hive中

时间:2018-06-20 07:59:51

标签: apache-spark join

我试图在Spark中加入两个大表,最后将其存储回Hive。我们可以在Hive本身中完成此操作,但这大约需要1个小时,因此我们正在尝试查看火花是否可以提供更好的性能。

下面是代码段。

val m_d = hiveContext.sql("select cp,c,s,t,d,a,tr,n from m_d").repartition(2000,$"c",$"s",$"t",$"d",$"a")
//-- count 60321929
val m_e = hiveContext.sql("select cp,c,s,t,d,a,tr,n from m_e").repartition(2000,$"c",$"s",$"t",$"d",$"a")
//-- count 268135916

m_d.registerTempTable( "m_d" )
m_e.registerTempTable( "m_e" )


val joined_df = m_d.join(m_e, m_d("c") === m_e("c") 
&& m_d("s") === m_e("s") && m_d("t") === m_e("t")
&& m_d("d") === m_e("d") && m_d("a") === m_e("a") 
)

joined_df.registerTempTable( "joined_df" )
joined_df.cache()
joined_df.first()
joined_df.show()

显示效果很好,但是即使我运行

joined_df.count 

hiveContext.sql("create table joined_df as select * from joined_df");

joined_df.rdd.mapPartitionsWithIndex{case (i,rows) => Iterator((i,rows.size))}.toDF("partition_number","number_of_records").collect.foreach(println)

我遇到了错误。

  

java.lang.IllegalArgumentException:大小超过Integer.MAX_VALUE

当我用google搜索该错误时,似乎分区大小之一超过20gb左右。因此,我收到此错误。但我认为联接中使用的列对于联接而言非常独特,应该使数据倾斜。 我也将分区更改为2000,以确保数据均匀分布。 对于这个阶段,我将获得Shuffle Read-15.9 GB。输入-59.7 MB。

任何改进连接或克服错误的建议都非常感谢。 请注意,尽管两个数据都很大,但我没有尝试广播加入。

0 个答案:

没有答案