1.使用IDEA開發Spark SQL
1.1創建DataFrame/DataSet
1、指定列名添加Schema
2、通過StrucType指定Schema
3、編寫樣例類,利用反射機制推斷Schema
1.1.1指定列名添加Schema
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//導包 import org.apache.spark.rdd.RDD import org.apache.spark.sql.SparkSession //代碼 // 1.創建SparkSession val spark = SparkSession.builder().master( "local[*]" ).appName( "sql" ).getOrCreate() // 2.使用spark 獲取sparkContext 上下文對象 val sc = spark.sparkContext // 3.使用SparkContext 讀取文件并按照空格切分 返回RDD val rowRDD: RDD[(Int, String, Int)] = sc.textFile( "./data/person.txt" ).map(_.split( " " )).map(x=>(x( 0 ).toInt,x( 1 ),x( 2 ).toInt)) // 4.導入隱式類 import spark.implicits._ //5.將RDD 轉換為DataFrame 指定元數據信息 val dataFrame = rowRDD.toDF( "id" , "name" , "age" ) //6.數據展示 dataFrame.show() |
1.1.2StructType指定Schema
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//導包 import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType} //編寫代碼 //1.實例SparkSession val spark = SparkSession.builder().master( "local[*]" ).appName( "sql" ).getOrCreate() //2.根據SparkSession獲取SparkContext 上下文對象 val sc = spark.sparkContext // 3.使用SparkContext讀取文件并按照空開切分并返回元組 val rowRDD = sc.textFile( "./data/person.txt" ).map(_.split( " " )).map(x=>Row(x( 0 ).toInt,x( 1 ),x( 2 ).toInt)) // 4.導入隱式類 import spark.implicits._ //5.使用StructType 添加元數據信息 val schema = StructType(List( StructField( "id" , IntegerType, true ), StructField( "name" , StringType, true ), StructField( "age" , IntegerType, true ) )) //6.將數據與元數據進行拼接 返回一個DataFrame val dataDF = spark.createDataFrame(rowRDD,schema) //7.數據展示 dataDF.show() |
1.1.3反射推斷Schema
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//導包 import org.apache.spark.rdd.RDD import org.apache.spark.sql.SparkSession //定義單例對象 case class Person(Id:Int,name:String,age:Int) //編寫代碼 //1.實例sparkSession val spark = SparkSession.builder().master( "local[*]" ).appName( "sql" ).getOrCreate() //2.通過sparkSession獲取sparkContext 上下文對象 val sc = spark.sparkContext //3.通過sparkContext 讀取文件并按照空格切分 將每一個數據保存到person中 val rowRDD: RDD[Person] = sc.textFile( "./data/person.txt" ).map(_.split( " " )).map(x=>Person(x( 0 ).toInt,x( 1 ),x( 2 ).toInt)) // 4.導入隱式類 import spark.implicits._ //5.將rowRDD轉換為dataFrame val dataFrame = rowRDD.toDF() //6.數據展示 dataFrame.show() |
到此這篇關于SparkSQL使用IDEA快速入門DataFrame與DataSet的文章就介紹到這了,更多相關SparkSQL快速入門內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_43791724/article/details/105468076