Puntnotatie en SQL

Introductie tot Spark SQL in Python

Mark Plutowski

Data Scientist

Onze tabel heeft 3 kolommen

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 |
+--------+-------------+-----+
Introductie tot Spark SQL in Python

We hebben er maar 2 nodig

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

Drie manieren om 2 kolommen te selecteren

  • 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'))
Introductie tot Spark SQL in Python

Twee manieren om een kolom te hernoemen

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')
Introductie tot Spark SQL in Python

Doe dit niet!

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

Introductie tot Spark SQL in Python

SQL-queries met puntnotatie

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()
Introductie tot Spark SQL in Python

Windowfunctie in SQL

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

Windowfunctie in 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|
+--------+-------------+-----+---+
Introductie tot Spark SQL in Python

Windowfunctie met puntnotatie

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 in SQL: pyspark.sql.functions.row_number
  • De binnenkant van de OVER-clausule: pyspark.sql.Window
  • PARTITION BY: pyspark.sql.Window.partitionBy
  • ORDER BY: pyspark.sql.Window.orderBy
Introductie tot Spark SQL in Python

Een WindowSpec gebruiken

  • De functie over in Spark SQL komt overeen met een OVER-clausule in SQL.
  • De klasse pyspark.sql.window.Window stelt de binnenkant van een OVER-clausule voor.
window = Window.partitionBy('train_id').orderBy('time')
dfx = df.withColumn('next', lead('time',1).over(window))
  • Hierboven is type(window) pyspark.sql.window.WindowSpec
Introductie tot Spark SQL in Python

Laten we oefenen!

Introductie tot Spark SQL in Python

Preparing Video For Download...