Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark 如何对文本文件中的值使用isin函数?_Apache Spark_Apache Spark Sql - Fatal编程技术网

Apache spark 如何对文本文件中的值使用isin函数?

Apache spark 如何对文本文件中的值使用isin函数?,apache-spark,apache-spark-sql,Apache Spark,Apache Spark Sql,我想使用外部文件过滤数据帧 这就是我现在使用过滤器的方式: val Insert = Append_Ot.filter( col("Name2").equalTo("brazil") || col("Name2").equalTo("france") || col("Name2").equalTo("algeria") || col("Name2").equalTo("tunisia") || col("Name2").equalTo("egypte")) 我不想使用硬编码

我想使用外部文件过滤数据帧

这就是我现在使用过滤器的方式:

val Insert = Append_Ot.filter(
  col("Name2").equalTo("brazil") ||
  col("Name2").equalTo("france") ||
  col("Name2").equalTo("algeria") ||
  col("Name2").equalTo("tunisia") ||
  col("Name2").equalTo("egypte"))
我不想使用硬编码的字符串文字,而是想创建一个外部文件,其中包含要筛选的值

所以我创建了这个文件:

val filter_numfile = sc.textFile("/user/zh/worskspace/filter_nmb.txt")
  .map(_.split(" ")(1))
  .collect
这给了我:

filter_numfile: Array[String] = Array(brazil, france, algeria, tunisia, egypte)
然后,我在
Name2
列上使用
isin
函数

val Insert = Append_Ot.where($"Name2".isin(filter_numfile: _*))

但这给了我一个空的数据帧。为什么?

我只是想在Philantrover的回答中添加一些信息

他的答案是完美的,但可能有一些案例不匹配,所以你也必须检查案例不匹配


tl;dr确保字母大小写一致,即它们都是大写或小写。只需使用
上部
下部
标准功能即可


假设您将输入文件设置为

1 Algeria
2 tunisia
3 brazil
4 Egypt
您读取文本文件并将所有国家/地区更改为小写

val countries = sc.textFile("path to input file").map(_.split(" ")(1).trim)
  .collect.toSeq
val array = Array(countries.map(_.toLowerCase) : _*)
然后就有了数据帧

val Append_Ot = sc.parallelize(Seq(("brazil"),("tunisia"),("algeria"),("name"))).toDF("Name2")
当您应用以下条件时

import org.apache.spark.sql.functions._
val Insert = Append_Ot.where(lower($"Name2").isin(array : _* ))
您应该将输出设置为

+-------+
|Name2  |
+-------+
|brazil |
|tunisia|
|algeria|
+-------+
空数据框也可能是由于拼写不匹配造成的