val conf = new SparkConf().setAppName("df").setMaster("local")
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
//创建一个普通RDD
val rdd = sc.textFile("G:\\qf大数据\\spark\\day06_sql\\students.txt")
val student: RDD[Student] = rdd.map(x => {
val sp = x.split(" ")
Student(sp(0).toInt, sp(1), sp(2).toInt)
})
import sqlContext.implicits._
val df: DataFrame = student.toDF()
df.registerTempTable("student")
val df1 = sqlContext.sql("select * from student where age<18")
df1.show()
//将数据使用json的格式保存,并且这里使用的追加的操作。
df1.write.mode(SaveMode.Append).json("")
val conf = new SparkConf().setAppName("df02").setMaster("local")
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
val rdd = sc.textFile("G:\\qf大数据\\spark\\day06_sql\\students.txt")
rdd.map(x=>{
val str = x.split(",")
Row(str(0).toInt,str(1),str(2).toInt)
})
val structType = StructType(Array(StructField("id",IntegerType,true),StructField("name",StringType,true),StructField("age",IntegerType,true)))
val df = sqlContext.createDataFrame(structType)
df.registerTempTable("student")
sqlContext.sql("select * from student")