forked from mrpowers-io/spark-style-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runnable.scala
53 lines (39 loc) · 1.26 KB
/
runnable.scala
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// snippets from this doc can be copy / pasted into different Spark runtimes
// most runtimes (Databricks, Spark shell) import implicits by default, so you don't need to run this
import spark.implicits._
val peopleDF = Seq(
("bob", 44),
("cindy", 10)
).toDF("first_name", "age")
// view the DataFrame
peopleDF.show()
//+----------+---+
//|first_name|age|
//+----------+---+
//| bob| 44|
//| cindy| 10|
//+----------+---+
// suffix DataFrames with DF
peopleDF.createOrReplaceTempView("people")
// once you create the view, you can query it with SQL
spark.sql("select * from people where age > 20").show()
//+----------+---+
//|first_name|age|
//+----------+---+
//| bob| 44|
//+----------+---+
// name the Column arguments with the col1, col2 convention
def fullName(col1: Column, col2: Column): Column = concat(col1, lit(" "), col2)
val namesDF = Seq(
("bob", "loblaw"),
("cindy", "crawford")
).toDF("first_name", "last_name")
namesDF
.withColumn("full_name", fullName($"first_name", $"last_name"))
.show()
//+----------+---------+--------------+
//|first_name|last_name| full_name|
//+----------+---------+--------------+
//| bob| loblaw| bob loblaw|
//| cindy| crawford|cindy crawford|
//+----------+---------+--------------+