def main(args: Array[String]): Unit = { val conf = new SparkConf().setMaster("local").setAppName("ScalaSparkDataFrameOps") val sc = new SparkContext(conf) val sqlContext = new SQLContext(sc) /** * 创建DataFrame,使用sqlContext的read的方式 */ val df = sqlContext.read.json("E:/test/spark/sql/people.json") //打印出dataframe对应的前2两条记录,numRows就是要输出df中的numRows条记录 df.show(2) //默认实际就是df.show(20) df.show() //打印出DF中所有的schema信息 df.printSchema() //查询出name的列并打印出来 df.select("name").show() df.select(new Column("name"), new Column("age")).show() //过滤并打印出年龄超过14岁的人 df.select(new Column("age").gt(14), new Column("name"), new Column("age")).filter("name = 'Andy'").show //按照身高进行分组 df.select("name", "age", "height").groupBy("height").count().show() //查询出某列的数据,然后对列值接进行计算 df.select(new Column("name"), new Column("height"), new Column("age").+(10).as("age")).filter("age is not null").show() sc.stop() }
SparkSQL之查询,过滤
最新推荐文章于 2024-06-22 19:58:58 发布