2016-08-15 41 views
0

我试图运行下面的查询:使用Scala中列出了星火SQL查询

val IgnoreList = List(""," ","0","-","{}","()","[]","null","Null","NULL","false","False","FALSE","NA","na","Na","n/a","N/a","N/A","nil","Nil","NIL") 
val df = sqlContext.sql(s"select userName from names where userName not in $IgnoreList") 

但是,这是行不通的。我也试过:

val IgnoreList = List(""," ","0","-","{}","()","[]","null","Null","NULL","false","False","FALSE","NA","na","Na","n/a","N/a","N/A","nil","Nil","NIL") 
sqlContext.udf.register("SqlList",(s: List[String]) => "('" + s.mkString("','") + "')") 
val df = sqlContext.sql(s"select userName from names where userName not in SqlList($IgnoreList)") 

但是这也行不通。有什么建议么?

回答

1

您的第一次尝试失败,因为它调用List的默认toString,它不会返回您需要的SQL有效语法。您的第二次尝试失败,因为使用UDF构建SQL字符串没有意义 - UDF将应用于记录(或列),而不是创建字符串查询。

您需要在第二个完成的格式化,在第一做了简单的字符串插值结合:

val IgnoreList = List(""," ","0","-","{}","()","[]","null","Null","NULL","false","False","FALSE","NA","na","Na","n/a","N/a","N/A","nil","Nil","NIL") 
val condition = "('" + IgnoreList.mkString("','") + "')" 
val df = sqlContext.sql(s"select userName from names where userName not in $condition") 

BTW,可能会更清楚这样格式化列表:

IgnoreList.map(s => s"'$s'").mkString(",")