forked from hemant-rout/LearningSpark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTime.scala
74 lines (62 loc) · 2.04 KB
/
DateTime.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package dataframe
import java.sql.{Date, Timestamp}
import org.apache.spark.sql.{Row, SparkSession}
import org.apache.spark.sql.types._
import org.apache.spark.sql.functions._
//
// Functions for querying against columns of DateType and TimestampType in
// a DataFrame.
//
object DateTime {
def main(args: Array[String]) {
val spark =
SparkSession.builder()
.appName("DataFrame-DateTime")
.master("local[4]")
.getOrCreate()
import spark.implicits._
val schema = StructType(
Seq(
StructField("id", IntegerType, true),
StructField("dt", DateType, true),
StructField("ts", TimestampType, true)
)
)
val rows = spark.sparkContext.parallelize(
Seq(
Row(
1,
Date.valueOf("1999-01-11"),
Timestamp.valueOf("2011-10-02 09:48:05.123456")
),
Row(
1,
Date.valueOf("2004-04-14"),
Timestamp.valueOf("2011-10-02 12:30:00.123456")
),
Row(
1,
Date.valueOf("2008-12-31"),
Timestamp.valueOf("2011-10-02 15:00:00.123456")
)
), 4)
val tdf = spark.createDataFrame(rows, schema)
println("DataFrame with both DateType and TimestampType")
tdf.show()
println("Pull a DateType apart when querying")
tdf.select($"dt", year($"dt"), quarter($"dt"), month($"dt"),
weekofyear($"dt"), dayofyear($"dt"), dayofmonth($"dt")).show()
println("Date arithmetic")
tdf.select($"dt", datediff(current_date(), $"dt"),
date_sub($"dt", 20),
date_add($"dt", 10),
add_months($"dt", 6)).show()
println("Date truncation")
tdf.select($"dt", trunc($"dt", "YYYY"), trunc($"dt", "YY"),
trunc($"dt", "MM")).show()
println("Date formatting")
tdf.select($"dt", date_format($"dt", "MMM dd, YYYY")).show()
println("Pull a Timestamp type apart when querying")
tdf.select($"ts", year($"ts"), hour($"ts"), minute($"ts"), second($"ts")).show()
}
}