久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SparkSQL使用IDEA快速入門DataFrame與DataSet的完美教程

瀏覽:158日期:2024-07-12 15:04:25
目錄1.使用IDEA開(kāi)發(fā)Spark SQL1.1創(chuàng)建DataFrame/DataSet1.1.1指定列名添加Schema1.1.2StructType指定Schema1.1.3反射推斷Schema1.使用IDEA開(kāi)發(fā)Spark SQL1.1創(chuàng)建DataFrame/DataSet

1、指定列名添加Schema

2、通過(guò)StrucType指定Schema

3、編寫樣例類,利用反射機(jī)制推斷Schema

1.1.1指定列名添加Schema

//導(dǎo)包import org.apache.spark.rdd.RDDimport org.apache.spark.sql.SparkSession//代碼// 1.創(chuàng)建SparkSession val spark = SparkSession.builder().master('local[*]').appName('sql').getOrCreate()// 2.使用spark 獲取sparkContext 上下文對(duì)象 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.導(dǎo)入隱式類 import spark.implicits._//5.將RDD 轉(zhuǎn)換為DataFrame 指定元數(shù)據(jù)信息 val dataFrame = rowRDD.toDF('id','name','age')//6.數(shù)據(jù)展示 dataFrame.show()1.1.2StructType指定Schema

//導(dǎo)包import org.apache.spark.sql.{Row, SparkSession}import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}//編寫代碼//1.實(shí)例SparkSession val spark = SparkSession.builder().master('local[*]').appName('sql').getOrCreate()//2.根據(jù)SparkSession獲取SparkContext 上下文對(duì)象 val sc = spark.sparkContext// 3.使用SparkContext讀取文件并按照空開(kāi)切分并返回元組 val rowRDD = sc.textFile('./data/person.txt').map(_.split(' ')).map(x=>Row(x(0).toInt,x(1),x(2).toInt))// 4.導(dǎo)入隱式類 import spark.implicits._//5.使用StructType 添加元數(shù)據(jù)信息 val schema = StructType(List( StructField('id', IntegerType, true), StructField('name', StringType, true), StructField('age', IntegerType, true) ))//6.將數(shù)據(jù)與元數(shù)據(jù)進(jìn)行拼接 返回一個(gè)DataFrame val dataDF = spark.createDataFrame(rowRDD,schema)//7.數(shù)據(jù)展示 dataDF.show()1.1.3反射推斷Schema

//導(dǎo)包import org.apache.spark.rdd.RDDimport org.apache.spark.sql.SparkSession//定義單例對(duì)象 case class Person(Id:Int,name:String,age:Int)//編寫代碼//1.實(shí)例sparkSession val spark = SparkSession.builder().master('local[*]').appName('sql').getOrCreate()//2.通過(guò)sparkSession獲取sparkContext 上下文對(duì)象 val sc = spark.sparkContext//3.通過(guò)sparkContext 讀取文件并按照空格切分 將每一個(gè)數(shù)據(jù)保存到person中 val rowRDD: RDD[Person] = sc.textFile('./data/person.txt').map(_.split(' ')).map(x=>Person(x(0).toInt,x(1),x(2).toInt))// 4.導(dǎo)入隱式類 import spark.implicits._//5.將rowRDD轉(zhuǎn)換為dataFrame val dataFrame = rowRDD.toDF() //6.數(shù)據(jù)展示 dataFrame.show()

到此這篇關(guān)于SparkSQL使用IDEA快速入門DataFrame與DataSet的文章就介紹到這了,更多相關(guān)SparkSQL快速入門內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: IDEA
相關(guān)文章:
主站蜘蛛池模板: 宜川县| 万山特区| 浮梁县| 武强县| 宁安市| 长阳| 花垣县| 肇州县| 荆州市| 五华县| 正阳县| 神农架林区| 万源市| 兰考县| 刚察县| 元阳县| 临湘市| 都昌县| 正定县| 通城县| 闽侯县| 伽师县| 泗水县| 中卫市| 大余县| 绥德县| 巴彦县| 定边县| 和林格尔县| 沙田区| 扬中市| 北海市| 石屏县| 东莞市| 岳普湖县| 和硕县| 元谋县| 太和县| 巴彦县| 澜沧| 特克斯县|