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



我正在使用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];
...

非常感谢。

问题的根源是您在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      

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

相关内容

  • 没有找到相关文章

最新更新