Creating DataFrame

1, created by way of case class

people.txt

// definition of case class, corresponding to the table structure 
Case  class People (var name: String, var Age: Int) 
Object TestDataFrame1 { 
  DEF main (args: the Array [String]): Unit = { 
    Val the conf = new new SparkConf () setAppName. ( "RDDToDataFrame"). a setMaster ( "local" ) 
    Val SC = new new SparkContext (the conf) 
    Val context = new new SqlContext (SC)
     // local read data RDD, and RDD case class associated with 
    val peopleRDD = sc. textFile ( "E: \\ \\ people.txt 666" ) 
      .map (Line => People (line.split ( ",") (0), line.split ( ",") (. 1 ) .trim.toInt ))
    Import context.implicits._
     // convert RDD to DataFrames 
    Val df = peopleRDD.toDF
     // will create a temporary DataFrames to view 
    df.createOrReplaceTempView ( "people" )
     // use SQL statements to query 
    context.sql ( "select from people * " ) the .Show () 
  } 
}

2, created by structType way

 

 

3, by json text

 

 

Reference blog: https://www.cnblogs.com/frankdeng/p/9301743.html

Guess you like

Origin www.cnblogs.com/guoyu1/p/12092353.html