zoukankan      html  css  js  c++  java
  • SparkSQL——DataFrame API基本操作

    package com.spark
    
    import org.apache.spark.sql.SparkSession
    
    /**
      * DataFrame API基本操作
      */
    object DataFrameAPP1 {
      def main(args: Array[String]): Unit = {
    
        val path="E:\data\infos.txt"
        val spark=SparkSession.builder().appName("DataFrameApp").master("local[2]").getOrCreate()
    
        val peopleDF=spark.read.format("json").load(path)
    
        peopleDF.printSchema()
    
        //输出前20条数据
        peopleDF.show()
    
        //select name from table
        peopleDF.select("name").show()
    
        //select name ,age+10 as age2 from table
        peopleDF.select(peopleDF.col("name"),(peopleDF.col("age")+10).as("age2")).show()
    
        //select * from table where age>19
        peopleDF.filter(peopleDF.col("age")>19).show()
    
        //select age,count(1) from table group by age
        peopleDF.groupBy("age").count().show()
    
        spark.stop()
      }
    }
  • 相关阅读:
    JDK6的switch支持不是很好
    团队作业(2)
    团队作业(1)
    4月30日
    重构:改善既有代码的设计有感
    4月28日
    4月27日
    4月26日
    4月25日
    4月24日
  • 原文地址:https://www.cnblogs.com/aishanyishi/p/10318175.html
Copyright © 2011-2022 走看看