Spark SQL'爆炸'命令在AWS EC2上失败但在本地成功

时间:2015-11-05 22:54:15

标签: scala amazon-ec2 apache-spark apache-spark-sql

我正在使用Spark SQL(我提到它是在Spark中,以防影响SQL语法 - 我还不够熟悉)我有一个表,我试图重新构建。我有一种在本地工作的方法但是当我尝试在AWS EC2实例上运行相同的命令时,我收到错误报告我有一个未解析的运算符'

基本上我的数据看起来像:

userId    someString      varA   
   1      "example1"     [0,2,5] 
   2      "example2"     [1,20,5] 

我使用'爆炸' varA上的sqlContext中的命令。当我在本地运行时,事情会正确返回,但在AWS上它们会失败。

我可以使用以下命令重现这一点:

val data = List(
  ("1", "example1", Array(0,2,5)), ("2", "example2", Array(1,20,5)))
val distData = sc.parallelize(data)
val distTable = distData.toDF("userId", "someString", "varA")
distTable.registerTempTable("distTable_tmp")
val temp1 = sqlContext.sql("select userId, someString, varA from distTable_tmp")
val temp2 = sqlContext.sql(
  "select userId, someString, explode(varA) as varA from distTable_tmp")

在本地,temp1.show()和temp2.​​show()返回我期望的内容,即:

scala> temp1.show()
+------+----------+----------+
|userId|someString|      varA|
+------+----------+----------+
|     1|  example1| [0, 2, 5]|
|     2|  example2|[1, 20, 5]|
+------+----------+----------+

scala> temp2.show()
+------+----------+----+
|userId|someString|varA|
+------+----------+----+
|     1|  example1|   0|
|     1|  example1|   2|
|     1|  example1|   5|
|     2|  example2|   1|
|     2|  example2|  20|
|     2|  example2|   5|
+------+----------+----+

但在AWS上,temp1 sqlContext命令工作正常,但temp2失败并显示消息:

scala> val temp2 = sqlContext.sql("select userId, someString, explode(varA) as varA from distTable_tmp")
15/11/05 22:46:49 INFO parse.ParseDriver: Parsing command: select userId, someString, explode(varA) as varA from distTable_tmp
15/11/05 22:46:49 INFO parse.ParseDriver: Parse Completed
org.apache.spark.sql.AnalysisException: unresolved operator 'Project [userId#3,someString#4,HiveGenericUdtf#org.apache.hadoop.hive.ql.udf.generic.GenericUDTFExplode(varA#5) AS varA#6];
...

非常感谢。

1 个答案:

答案 0 :(得分:1)

问题的根源是您在EC2上使用的Spark版本。 Spark 1.4中引入了explode函数,因此无法在1.3.1上运行。可以像这样使用RDDflatMap

import org.apache.spark.sql.Row
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{StructType, StructField, IntegerType}

val rows: RDD[Row] = distTable.rdd.flatMap(
  row => row.getAs[Seq[Int]](2).map(v => Row.fromSeq(row.toSeq :+ v)))
val newSchema = StructType(
  distTable.schema.fields :+ StructField("varA_exploded", IntegerType, true))

sqlContext.createDataFrame(rows, newSchema).show

// userId someString varA                 varA_exploded
// 1      example1   ArrayBuffer(0, 2, 5) 0            
// 1      example1   ArrayBuffer(0, 2, 5) 2            
// 1      example1   ArrayBuffer(0, 2, 5) 5            
// 2      example2   ArrayBuffer(1, 20... 1            
// 2      example2   ArrayBuffer(1, 20... 20           
// 2      example2   ArrayBuffer(1, 20... 5      

但是它怀疑它值得大惊小怪。