2016-11-10 135 views
3

使用NOT IN从CSV文件我用星火SQL将数据加载到一个val这样如何星火

val customers = sqlContext.sql("SELECT * FROM customers") 

但是我有一个包含一列CUST_ID和50,00行的单独的txt文件。即

CUST_ID 
1 
2 
3 

我希望我的customers VAL有在customers表不在TXT文件中的所有客户。

使用SQL我会通过SELECT * FROM customers NOT IN cust_id ('1','2','3')

为此,我怎样才能做到这一点使用火花?

我读过的文本文件,我可以打印它行,但我不知道如何与我的sql查询匹配这个

scala> val custids = sc.textFile("cust_ids.txt") 
scala> custids.take(4).foreach(println) 
CUST_ID 
1 
2 
3 

回答

3

你可以导入文本文件作为一个数据帧,并做了左外连接:

val customers = Seq(("1", "AAA", "shipped"), ("2", "ADA", "delivered") , ("3", "FGA", "never received")).toDF("id","name","status") 
val custId = Seq(1,2).toDF("custId") 

customers.join(custId,'id === 'custId,"leftOuter") 
     .where('custId.isNull) 
     .drop("custId") 
     .show() 


+---+----+--------------+ 
| id|name|  status| 
+---+----+--------------+ 
| 3| FGA|never received| 
+---+----+--------------+ 
+0

我在火花壳得到这个错误,试图重新建立你的例子:'VAL TEST1 = SEQ(1,2).toDF( “客户ID”)'错误:'错误:值toDF不是Seq [Int]的成员' – Anthony

+1

您是否使用Spark的2.0版本?然后sc.parallelize(Seq(1,2))。toDF(“custId”) – ulrich

+0

我的spark版本是'version 1.5.0'和scala版本是'Using Scala version 2.10.4' – Anthony