我使用 Spark sql 像这样将数据加载到 val
val customers = sqlContext.sql("SELECT * FROM customers")
但我有一个单独的 txt 文件,其中包含一列 CUST_ID
和 50,00 行。即
CUST_ID
1
2
3
我希望我的 customers
val 将所有不在 TXT 文件中的客户都放在 customers
表中。
使用 Sql,我将通过 SELECT * FROM customers NOT IN cust_id ('1','2','3')
我如何使用 Spark 执行此操作?
我已经阅读了文本文件并且可以打印它的行,但是我不确定如何将它与我的 sql 查询相匹配
scala> val custids = sc.textFile("cust_ids.txt")
scala> custids.take(4).foreach(println)
CUST_ID
1
2
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|
+---+----+--------------+
关于scala - 如何在 Spark 中使用 CSV 文件中的 NOT IN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40534129/