SparkSQL使用IDEA快速入門DataFrame與DataSet的完美教程
1、指定列名添加Schema
2、通過StrucType指定Schema
3、編寫樣例類,利用反射機制推斷Schema
1.1.1指定列名添加Schema//導包import org.apache.spark.rdd.RDDimport 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
//導包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
//導包import org.apache.spark.rdd.RDDimport 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快速入門內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
