-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataFrame.scala
51 lines (45 loc) · 1.46 KB
/
DataFrame.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
import org.apache.spark.sql.SparkSession
object DataFrame {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder
.appName(this.getClass().getSimpleName())
.master("local[*]")
.config("", "")
.getOrCreate
import spark.implicits._
spark.read.textFile("data/clm.txt")
.map(_.split(","))
.map(x => (x(0), x(1), x(2)))
.toDF("id", "name", "age")
.cache().createTempView("clm")
spark.read.textFile("data/clm2.txt")
.map(_.split(","))
.map(x => (x(0), x(1)))
.toDF("id", "place")
.cache()
.createTempView("clm2")
// not in 与左连接 查询数据表1 并且数据表1的数据不在数据表2中
var sqltext = "select id,name,age from clm where id not in (select id from clm2)"
spark.sql(sqltext).show()
val sqltext2 = "select clm.id,name,age from clm left join clm2 on clm.id=clm2.id where clm2.id is Null"
spark.sql(sqltext2).show()
// 1, 测试默认数据类型
spark.read.
textFile("data/clm.txt")
.map(_.split(","))
.map(x => (x(0), x(1), x(2)))
.toDF("id", "name", "age")
.dtypes
.foreach(println)
//2, 把数值型的列转为IntegerType
spark.read.
textFile("data/clm.txt")
.map(_.split(","))
.map(x => (x(0), x(1), x(2)))
.toDF("id", "name", "age")
.select($"id".cast("int"), $"name", $"age".cast("int"))
.dtypes
.foreach(println)
spark.stop()
}
}