Spark SQL

Introduction à Spark SQL en Python

Mark Plutowski Phd

Data Scientist

Créer une table SQL et l'interroger

Curseur du shell PySpark

Introduction à Spark SQL en Python

Charger un dataframe à partir d'un fichier

df = spark.read.csv(filename)
df = spark.read.csv(filename, header=True)
Introduction à Spark SQL en Python

Créer une table SQL et l'interroger

df.createOrReplaceTempView("schedule")

spark.sql("SELECT * FROM schedule WHERE station = 'San Jose'") .show()
+--------+--------+-----+
|train_id| station| time|
+--------+--------+-----+
|     324|San Jose|9:05a|
|     217|San Jose|6:59a|
+--------+--------+-----+
Introduction à Spark SQL en Python

Inspecter le schéma d'une table

result = spark.sql("SHOW COLUMNS FROM tablename")
result = spark.sql("SELECT * FROM tablename LIMIT 0")
result = spark.sql("DESCRIBE tablename")
result.show()
print(result.columns)
Introduction à Spark SQL en Python

Trame

Introduction à Spark SQL en Python

Trame de données

Introduction à Spark SQL en Python

Données tabulaires

+--------+-------------+-----+
|train_id|      station| time|
+--------+-------------+-----+
|     324|San Francisco|7:59a| 
|     324|  22nd Street|8:03a|
|     324|     Millbrae|8:16a|
|     324|    Hillsdale|8:24a|
|     324| Redwood City|8:31a|
|     324|    Palo Alto|8:37a|
|     324|     San Jose|9:05a|
|     217|       Gilroy|6:06a|
|     217|   San Martin|6:15a|
|     217|  Morgan Hill|6:21a|
|     217| Blossom Hill|6:36a|
|     217|      Capitol|6:42a|
|     217|       Tamien|6:50a|
|     217|     San Jose|6:59a|
+--------+-------------+-----+
Introduction à Spark SQL en Python

deux dataframes

Introduction à Spark SQL en Python

deux dataframes concaténés

Introduction à Spark SQL en Python

un dataframe

Introduction à Spark SQL en Python

division d'un dataframe 1

Introduction à Spark SQL en Python

division d'un dataframe 2

Introduction à Spark SQL en Python

division d'un dataframe 3

Introduction à Spark SQL en Python

division d'un dataframe 4

Introduction à Spark SQL en Python

division d'un dataframe 5

Introduction à Spark SQL en Python

division d'un dataframe 6

Introduction à Spark SQL en Python

division d'un dataframe distribué

Introduction à Spark SQL en Python

SQL

Introduction à Spark SQL en Python

Structured Query Language

Introduction à Spark SQL en Python

interroger

Introduction à Spark SQL en Python

données distribuées

Introduction à Spark SQL en Python

données distribuées + requête

Introduction à Spark SQL en Python

Charger un texte délimité

Charge un fichier à valeurs séparées par des virgules trainsched.txt dans un dataframe nommé df :

df = spark.read.csv("trainsched.txt", header=True)
Introduction à Spark SQL en Python

Charger un texte délimité

df = spark.read.csv("trainsched.txt", header=True)
df.show()
+--------+-------------+-----+
|train_id|      station| time|
+--------+-------------+-----+
|     324|San Francisco|7:59a|
|     324|  22nd Street|8:03a|
|     324|     Millbrae|8:16a|
|     324|    Hillsdale|8:24a|
|     324| Redwood City|8:31a|
|     ...|          ...|  ...|
|     217| Blossom Hill|6:36a|
|     217|      Capitol|6:42a|
|     217|       Tamien|6:50a|
|     217|     San Jose|6:59a|
+--------+-------------+-----+
Introduction à Spark SQL en Python

PySpark Shell

Introduction à Spark SQL en Python

Curseur du shell PySpark

Introduction à Spark SQL en Python

PySpark Shell

Introduction à Spark SQL en Python

Curseur du shell PySpark

Introduction à Spark SQL en Python

PySpark Shell

Introduction à Spark SQL en Python

Introduction à Spark SQL en Python

PySpark Shell

Introduction à Spark SQL en Python

Curseur du shell PySpark

Introduction à Spark SQL en Python

PySpark Shell

Introduction à Spark SQL en Python

Curseur du shell PySpark

Introduction à Spark SQL en Python

PySpark Shell

Introduction à Spark SQL en Python

Curseur du shell PySpark

Introduction à Spark SQL en Python

Passons à la pratique !

Introduction à Spark SQL en Python

Preparing Video For Download...