我有一个Hive表与以下定义:
create table testforerror (
my_column MAP<BIGINT, ARRAY<String>>
);
表中有以下记录
hive> select * from testforerror;
OK
{16001:["0034000000a4WDAAA2"]}
{16001:["0034000000orWiFAAU"]}
{16001:["","0034000000VgrHdAAJ"]}
{16001:["0034000000cS4tDAAS"]}
{15001:["0037000001a7ofgAAA"]}
Time taken: 0.067 seconds, Fetched: 5 row(s)
我有一个查询,过滤记录与键的my_column。查询如下
select * from testforerror where my_column[16001] is not null;
这个查询在hive/beeline shell中执行得很好,并产生以下记录:
hive> select * from testforerror where my_column[16001] is not null;
OK
{16001:["0034000000a4WDAAA2"]}
{16001:["0034000000orWiFAAU"]}
{16001:["","0034000000VgrHdAAJ"]}
{16001:["0034000000cS4tDAAS"]}
Time taken: 2.224 seconds, Fetched: 4 row(s)
但是,当我试图从spark sqlContext执行时,我得到一个错误。下面是错误信息:
scala> val errorquery = "select * from testforerror where my_column[16001] is not null"
errorquery: String = select * from testforerror where my_column[16001] is not null
scala> sqlContext.sql(errorquery).show()
org.apache.spark.sql.AnalysisException: cannot resolve 'my_column[16001]' due to data type mismatch: argument 2 requires bigint type, however, '16001' is of int type.; line 1 pos 43
at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)
at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$$anonfun$checkAnalysis$1$$anonfun$apply$2.applyOrElse(CheckAnalysis.scala:65)
at org.apache.spark.sql.catalyst.analysis.CheckAnalysis$$anonfun$checkAnalysis$1$$anonfun$apply$2.applyOrElse(CheckAnalysis.scala:57)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$transformUp$1.apply(TreeNode.scala:335)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$transformUp$1.apply(TreeNode.scala:335)
at org.apache.spark.sql.catalyst.trees.CurrentOrigin$.withOrigin(TreeNode.scala:69)
at org.apache.spark.sql.catalyst.trees.TreeNode.transformUp(TreeNode.scala:334)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$5.apply(TreeNode.scala:332)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$5.apply(TreeNode.scala:332)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$4.apply(TreeNode.scala:281)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$class.foreach(Iterator.scala:727)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:103)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:47)
任何提示都很有帮助,谢谢
可以使用DSL和getItem
方法代替SQL和括号表示法:
sqlContext.table("testforerror").where($"mycolumn".getItem(1L).isNotNull)