正如 Yaron 所说,where 和 filter 之间没有任何区别。
filter 是一个采用列或字符串参数的重载方法。无论您使用何种语法,性能都是相同的。
我们可以使用explain() 来查看所有不同的过滤语法生成相同的物理计划。假设您有一个包含 person_name 和 person_country 列的数据集。以下所有代码 sn-ps 将返回以下相同的物理计划:
df.where("person_country = 'Cuba'").explain()
df.where($"person_country" === "Cuba").explain()
df.where('person_country === "Cuba").explain()
df.filter("person_country = 'Cuba'").explain()
这些都返回这个物理计划:
== Physical Plan ==
*(1) Project [person_name#152, person_country#153]
+- *(1) Filter (isnotnull(person_country#153) && (person_country#153 = Cuba))
+- *(1) FileScan csv [person_name#152,person_country#153] Batched: false, Format: CSV, Location: InMemoryFileIndex[file:/Users/matthewpowers/Documents/code/my_apps/mungingdata/spark2/src/test/re..., PartitionFilters: [], PushedFilters: [IsNotNull(person_country), EqualTo(person_country,Cuba)], ReadSchema: struct<person_name:string,person_country:string>
语法不会改变过滤器在后台的执行方式,但执行查询的文件格式/数据库会改变。 Spark 将在 Postgres(支持谓词下推过滤)、Parquet(列修剪)和 CSV 文件上以不同方式执行相同的查询。 See here了解更多详情。