-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathSchemaConversion.scala
More file actions
43 lines (27 loc) · 885 Bytes
/
SchemaConversion.scala
File metadata and controls
43 lines (27 loc) · 885 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
32
33
34
35
36
37
38
39
40
41
42
43
package sql
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.sql._
import org.apache.spark.sql.types._
case class CC(i: Integer)
object SchemaConversion {
val spark =
SparkSession.builder()
.appName("SQL-SchemaConversion")
.master("local[4]")
.getOrCreate()
def toDataFrame(o: RDD[CC]) : DataFrame = {
val schema = StructType(
Seq(StructField("i", IntegerType, true)))
val rows = o.map(cc => Row(cc.i))
spark.createDataFrame(rows, schema)
}
def main (args: Array[String]) {
val nums = 1 to 100
val data = spark.sparkContext.parallelize(nums.map(i => CC(i)), 4)
val sdata = toDataFrame(data)
sdata.createOrReplaceTempView("mytable")
val results = spark.sql("SELECT COUNT(i) FROM mytable WHERE i > 50")
results.foreach(r => println(r))
}
}