Spark SQL

Introduktion till Spark SQL i Python

Mark Plutowski Phd

Data Scientist

Skapa en SQL-tabell och fråga mot den

Pyspark Shell-markör

Introduktion till Spark SQL i Python

Läs in en dataframe från fil

df = spark.read.csv(filename)
df = spark.read.csv(filename, header=True)
Introduktion till Spark SQL i Python

Skapa en SQL-tabell och fråga mot den

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|
+--------+--------+-----+
Introduktion till Spark SQL i Python

Granska tabellschema

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)
Introduktion till Spark SQL i Python

Ram

Introduktion till Spark SQL i Python

Dataframe med data

Introduktion till Spark SQL i Python

Tabelldata

+--------+-------------+-----+
|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|
+--------+-------------+-----+
Introduktion till Spark SQL i Python

två dataframes

Introduktion till Spark SQL i Python

två sammankopplade dataframes

Introduktion till Spark SQL i Python

en dataframe

Introduktion till Spark SQL i Python

dela upp en dataframe 1

Introduktion till Spark SQL i Python

dela upp en dataframe 2

Introduktion till Spark SQL i Python

dela upp en dataframe 3

Introduktion till Spark SQL i Python

dela upp en dataframe 4

Introduktion till Spark SQL i Python

dela upp en dataframe 5

Introduktion till Spark SQL i Python

dela upp en dataframe 6

Introduktion till Spark SQL i Python

dela upp en dataframe distribuerat

Introduktion till Spark SQL i Python

SQL

Introduktion till Spark SQL i Python

Structured Query Language

Introduktion till Spark SQL i Python

frågeställning

Introduktion till Spark SQL i Python

distribuerad data

Introduktion till Spark SQL i Python

distribuerad data + fråga

Introduktion till Spark SQL i Python

Läsa in avgränsad text

Läser in den kommaavgränsade filen trainsched.txt i en dataframe kallad df:

df = spark.read.csv("trainsched.txt", header=True)
Introduktion till Spark SQL i Python

Läsa in avgränsad text

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|
+--------+-------------+-----+
Introduktion till Spark SQL i Python

Pyspark Shell

Introduktion till Spark SQL i Python

Pyspark Shell-markör

Introduktion till Spark SQL i Python

Pyspark Shell

Introduktion till Spark SQL i Python

Pyspark Shell-markör

Introduktion till Spark SQL i Python

Pyspark Shell

Introduktion till Spark SQL i Python

Introduktion till Spark SQL i Python

Pyspark Shell

Introduktion till Spark SQL i Python

Pyspark Shell-markör

Introduktion till Spark SQL i Python

Pyspark Shell

Introduktion till Spark SQL i Python

Pyspark Shell-markör

Introduktion till Spark SQL i Python

Pyspark Shell

Introduktion till Spark SQL i Python

Pyspark Shell-markör

Introduktion till Spark SQL i Python

Nu kör vi en övning!

Introduktion till Spark SQL i Python

Preparing Video For Download...