Punktnotation och SQL

Introduktion till Spark SQL i Python

Mark Plutowski

Data Scientist

Vår tabell har 3 kolumner

df.columns
['train_id', 'station', 'time']
df.show(5)
+--------+-------------+-----+
|train_id|      station| time|
+--------+-------------+-----+
|     324|San Francisco|7:59 |
|     324|  22nd Street|8:03 |
|     324|     Millbrae|8:16 |
|     324|    Hillsdale|8:24 |
|     324| Redwood City|8:31 |
+--------+-------------+-----+
Introduktion till Spark SQL i Python

Vi behöver bara 2

df.select('train_id','station')
  .show(5)
+--------+-------------+
|train_id|      station|
+--------+-------------+
|     324|San Francisco|
|     324|  22nd Street|
|     324|     Millbrae|
|     324|    Hillsdale|
|     324| Redwood City|
+--------+-------------+
Introduktion till Spark SQL i Python

Tre sätt att välja 2 kolumner

  • df.select('train_id', 'station')
  • df.select(df.train_id, df.station)
  • from pyspark.sql.functions import col
  • df.select(col('train_id'), col('station'))
Introduktion till Spark SQL i Python

Två sätt att byta namn på en kolumn

df.select('train_id','station')
  .withColumnRenamed('train_id','train')
  .show(5)
+-----+-------------+
|train|      station|
+-----+-------------+
|  324|San Francisco|
|  324|  22nd Street|
|  324|     Millbrae|
|  324|    Hillsdale|
|  324| Redwood City|
+-----+-------------+
df.select(col('train_id').alias('train'), 'station')
Introduktion till Spark SQL i Python

Gör inte så här!

df.select('train_id', df.station, col('time'))

Introduktion till Spark SQL i Python

SQL-frågor med punktnotation

spark.sql('SELECT train_id AS train, station FROM schedule LIMIT 5')
     .show()
+-----+-------------+
|train|      station|
+-----+-------------+
|  324|San Francisco|
|  324|  22nd Street|
|  324|     Millbrae|
|  324|    Hillsdale|
|  324| Redwood City|
+-----+-------------+
df.select(col('train_id').alias('train'), 'station')
  .limit(5)
  .show()
Introduktion till Spark SQL i Python

Fönsterfunktion i SQL

query = """
SELECT *, 
ROW_NUMBER() OVER(PARTITION BY train_id ORDER BY time) AS id 
FROM schedule
"""
spark.sql(query)
     .show(11)
Introduktion till Spark SQL i Python

Fönsterfunktion i SQL

+--------+-------------+-----+---+
|train_id|      station| time| id|
+--------+-------------+-----+---+
|     217|       Gilroy|6:06 |  1|
|     217|   San Martin|6:15 |  2|
|     217|  Morgan Hill|6:21 |  3|
|     217| Blossom Hill|6:36 |  4|
|     217|      Capitol|6:42 |  5|
|     217|       Tamien|6:50 |  6|
|     217|     San Jose|6:59 |  7|
|     324|San Francisco|7:59 |  1|
|     324|  22nd Street|8:03 |  2|
|     324|     Millbrae|8:16 |  3|
|     324|    Hillsdale|8:24 |  4|
+--------+-------------+-----+---+
Introduktion till Spark SQL i Python

Fönsterfunktion med punktnotation

from pyspark.sql import Window, 
from pyspark.sql.functions import row_number
df.withColumn("id", row_number()
                    .over(
                           Window.partitionBy('train_id')
                                 .orderBy('time')
                         )
  )
  • ROW_NUMBER i SQL : pyspark.sql.functions.row_number
  • Innehållet i OVER-satsen : pyspark.sql.Window
  • PARTITION BY : pyspark.sql.Window.partitionBy
  • ORDER BY : pyspark.sql.Window.orderBy
Introduktion till Spark SQL i Python

Använda ett WindowSpec

  • Funktionen over i Spark SQL motsvarar en OVER-sats i SQL.
  • Klassen pyspark.sql.window.Window representerar innehållet i en OVER-sats.
window = Window.partitionBy('train_id').orderBy('time')
dfx = df.withColumn('next', lead('time',1).over(window))
  • Ovan är type(window) pyspark.sql.window.WindowSpec
Introduktion till Spark SQL i Python

Nu kör vi en övning!

Introduktion till Spark SQL i Python

Preparing Video For Download...