-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspark_sql.py
More file actions
31 lines (21 loc) · 987 Bytes
/
spark_sql.py
File metadata and controls
31 lines (21 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from pyspark.sql import SparkSession
from pyspark.sql import Row
# Create a SparkSession
spark = SparkSession.builder.appName('SparkSQL').getOrCreate()
def mapper(line):
fields = line.split(',')
return Row(id=int(fields[0]), name=str(fields[1].encode('utf-8')),
age=int(fields[2]), num_friends=int(fields[3]))
lines = spark.sparkContext.textFile('fakefriends.csv')
people = lines.map(mapper)
# Infer the schema, and register the DataFrame as a table.
schema_people = spark.createDataFrame(people).cache()
schema_people.createOrReplaceTempView('people')
# SQL can be run over DataFrames that have been registered as a table.
teenagers = spark.sql('SELECT * FROM people WHERE age >= 13 AND age <= 19')
# The results of SQL queries are RDDs and support all the normal RDD operations.
for teen in teenagers.collect():
print(teen)
# We can also use functions instead of SQL queries:
schema_people.groupBy('age').count().orderBy('age').show()
spark.stop()