过滤器和scala spark sql中的区别

Ish*_*han 13 scala apache-spark apache-spark-sql

我试过了两个,但它的工作原理相同

val items =  List(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

使用过滤器

employees.filter($"emp_id".isin(items:_*)).show
Run Code Online (Sandbox Code Playgroud)

在哪里使用

employees.where($"emp_id".isin(items:_*)).show
Run Code Online (Sandbox Code Playgroud)

两者的结果相同

+------+------+------+-------+------+-------+
|EMP_ID|F_NAME|SALARY|DEPT_ID|L_NAME|MANAGER|
+------+------+------+-------+------+-------+
|     6|    E6|  2000|      4|    L6|      2|
|     7|    E7|  3000|      4|    L7|      1|
|     8|    E8|  4000|      2|    L8|      2|
|     9|    E9|  1500|      2|    L9|      1|
|    10|   E10|  1000|      2|   L10|      1|
|     4|    E4|   400|      3|    L4|      1|
|     2|    E2|   200|      1|    L2|      1|
|     3|    E3|   700|      2|    L3|      2|
|     5|    E5|   300|      2|    L5|      2|
+------+------+------+-------+------+-------+
Run Code Online (Sandbox Code Playgroud)

Ale*_*nov 30

where文件:

使用给定条件过滤行.这是过滤器的别名.

filter对于这样的函数来说,它只是标准的Scala(和一般的FP)名称,适用where于喜欢SQL的人.


小智 5

这也与 Spark 优化有关。看一下简短的示例:HDFS 中的大镶木地板文件,包含结构和数据:

[hadoop@hdpnn ~]$ hadoop fs -ls /user/tickers/ticks.parquet
Found 27 items
drwxr-xr-x   - root root          0 2019-01-16 12:55 /user/tickers/ticks.parquet/ticker_id=1
drwxr-xr-x   - root root          0 2019-01-16 13:58 /user/tickers/ticks.parquet/ticker_id=10
drwxr-xr-x   - root root          0 2019-01-16 14:04 /user/tickers/ticks.parquet/ticker_id=11
drwxr-xr-x   - root root          0 2019-01-16 14:10 /user/tickers/ticks.parquet/ticker_id=12
...
Run Code Online (Sandbox Code Playgroud)

每个分区内部都有分区(按日期)

[hadoop@hdpnn ~]$ hadoop fs -ls /user/tickers/ticks.parquet/ticker_id=1
Found 6 items
drwxr-xr-x   - root root          0 2019-01-16 12:55 /user/tickers/ticks.parquet/ticker_id=1/ddate=2019-01-09
drwxr-xr-x   - root root          0 2019-01-16 12:50 /user/tickers/ticks.parquet/ticker_id=1/ddate=2019-01-10
drwxr-xr-x   - root root          0 2019-01-16 12:53 /user/tickers/ticks.parquet/ticker_id=1/ddate=2019-01-11
...
Run Code Online (Sandbox Code Playgroud)

结构:

scala> spark.read.parquet("hdfs://hdpnn:9000/user/tickers/ticks.parquet").printSchema
root
 |-- ticker_id: integer (nullable = true)
 |-- ddate: date (nullable = true)
 |-- db_tsunx: long (nullable = true)
 |-- ask: double (nullable = true)
 |-- bid: double (nullable = true)
Run Code Online (Sandbox Code Playgroud)

例如,您有这样的 DS:

val maxTsunx = spark.read.parquet("hdfs://hdpnn:9000/user/tickers/ticks.parquet").select(col("ticker_id"),col("db_tsunx")).groupBy("ticker_id").agg(max("db_tsunx"))
Run Code Online (Sandbox Code Playgroud)

包含每个ticker_id的max(db_tsunx)

FE:您只想从此 DS 获取一个股票行情的数据

你有2种方法:

1) maxTsunx.filter(r => r.get(0) == 1)
2) maxTsunx.where(col("ticker_id")===1)
Run Code Online (Sandbox Code Playgroud)

这是一个非常不同的“物理计划”

看1)

    == Physical Plan ==
    *(2) Filter <function1>.apply
    +- *(2) HashAggregate(keys=[ticker_id#37], functions=[max(db_tsunx#39L)], output=[ticker_id#37, max(db_tsunx)#52L])
       +- Exchange hashpartitioning(ticker_id#37, 200)
          +- *(1) HashAggregate(keys=[ticker_id#37], functions=[partial_max(db_tsunx#39L)], output=[ticker_id#37, max#61L])
             +- *(1) Project [ticker_id#37, db_tsunx#39L]
                +- *(1) FileScan parquet [db_tsunx#39L,ticker_id#37,ddate#38]    Batched: true, Format: Parquet, 
Location: InMemoryFileIndex[hdfs://hdpnn:9000/user/tickers/ticks.parquet],
PartitionCount: 162, 
    PartitionFilters: [], 
    PushedFilters: [], 
    ReadSchema: struct<db_tsunx:bigint>
Run Code Online (Sandbox Code Playgroud)

2)

== Physical Plan ==
*(2) HashAggregate(keys=[ticker_id#84], functions=[max(db_tsunx#86L)], output=[ticker_id#84, max(db_tsunx)#99L])
+- Exchange hashpartitioning(ticker_id#84, 200)
   +- *(1) HashAggregate(keys=[ticker_id#84], functions=[partial_max(db_tsunx#86L)], output=[ticker_id#84, max#109L])
      +- *(1) Project [ticker_id#84, db_tsunx#86L]
         +- *(1) FileScan parquet [db_tsunx#86L,ticker_id#84,ddate#85] Batched: true, Format: Parquet, 
Location: InMemoryFileIndex[hdfs://hdpnn:9000/user/tickers/ticks.parquet], 
PartitionCount: 6, 
PartitionFilters: [isnotnull(ticker_id#84), (ticker_id#84 = 1)], 
PushedFilters: [], 
ReadSchema: struct<db_tsunx:bigint>
Run Code Online (Sandbox Code Playgroud)

比较 162 和 6 以及 PartitionFilters: [], PartitionFilters: [isnotnull(ticker_id#84), (ticker_id#84 = 1)],

这意味着对来自 DS 的数据进行过滤操作,并进入 Spark 并用于优化。