日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

sparksql之通過 structType 創建 DataFrames(編程接口)

作者:qq_35561207 更新時間: 2022-02-26 編程語言
import org.apache.avro.generic.GenericData.StringType
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{IntegerType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.spark_project.dmg.pmml.True

object TestDataFrame2 {
  def main(args: Array[String]): Unit = {
    val conf=new SparkConf().setAppName("TestDataFrame2").setMaster("local")
    val sc=new SparkContext(conf)
    val SQLContext=new SQLContext(sc)
    //將本地的數據讀入RDD
    val fileRDD=sc.textFile("D:\\Users\\shashahu\\Desktop\\work\\spark-2.4.4\\examples\\src\\main\\resources\\people.txt")
    //將RDD數據映射成Row,需要import org.apache.spark.sql.row
    val rowRDD:RDD[Row]=fileRDD.map(line=>{
      val fields=line.split(",")
      Row(fields(0),fields(1).trim.toInt)
    })

    //創建StructType來定義結構
    val structType: StructType = StructType(
      //字段名,字段類型,是否可以為空
//      StructField("name", StringType, true) ::
        StructField("age", IntegerType, true) :: Nil
    )
    /**
      * rows: java.util.List[Row],
      * schema: StructType
      * */
    val df: DataFrame = SQLContext.createDataFrame(rowRDD,structType)
    df.createOrReplaceTempView("people")
    SQLContext.sql("select * from people").show()
  }
}

使用結構體來進行創建表的相關語句。

原文鏈接:https://blog.csdn.net/qq_35561207/article/details/103037917

欄目分類
最近更新