Notation par points et SQL

Introduction à Spark SQL en Python

Mark Plutowski

Data Scientist

Notre table a 3 colonnes

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 |
+--------+-------------+-----+
Introduction à Spark SQL en Python

Nous n'en avons besoin que de 2

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

Trois façons de sélectionner 2 colonnes

  • 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'))
Introduction à Spark SQL en Python

Deux façons de renommer une colonne

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')
Introduction à Spark SQL en Python

À ne pas faire !

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

Introduction à Spark SQL en Python

Requêtes SQL avec la notation par points

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()
Introduction à Spark SQL en Python

Fonction de fenêtrage en SQL

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

Fonction de fenêtrage en 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|
+--------+-------------+-----+---+
Introduction à Spark SQL en Python

Fonction de fenêtrage avec notation par points

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 en SQL : pyspark.sql.functions.row_number
  • L'intérieur de la clause OVER : pyspark.sql.Window
  • PARTITION BY : pyspark.sql.Window.partitionBy
  • ORDER BY : pyspark.sql.Window.orderBy
Introduction à Spark SQL en Python

Utiliser un WindowSpec

  • La fonction over dans Spark SQL correspond à une clause OVER en SQL.
  • La classe pyspark.sql.window.Window représente l'intérieur d'une clause OVER.
window = Window.partitionBy('train_id').orderBy('time')
dfx = df.withColumn('next', lead('time',1).over(window))
  • Ci-dessus, type(window) est pyspark.sql.window.WindowSpec
Introduction à Spark SQL en Python

Passons à la pratique !

Introduction à Spark SQL en Python

Preparing Video For Download...