2016-08-15 40 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(",")