zoukankan      html  css  js  c++  java
  • SCALAsparkSQL

    1.sparkSQL

    import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
    import org.apache.spark.sql.Encoder
    import org.apache.spark.sql.Row
    import org.apache.spark.sql.SparkSession
    import org.apache.spark.sql.types._
    
    object SparkSQLExample {
      // Note: Case classes in Scala 2.10 can support only up to 22 fields. To work around this limit,
      // you can use custom classes that implement the Product interface
      case class Person(name: String, age: Long)
    
      def main(args: Array[String]):Unit = {
        val spark = SparkSession
          .builder()
          .master("local")
          .appName("Spark SQL basic example")
          .config("spark.some.config.option", "some-value")
          .getOrCreate()
    
        // For implicit conversions like converting RDDs to DataFrames
        import spark.implicits._
        // $example off:init_session$
    
        runBasicDataFrameExample(spark)
        runDatasetCreationExample(spark)
        runInferSchemaExample(spark)
        runProgrammaticSchemaExample(spark)
    
        spark.stop()
      }
    
      private def runBasicDataFrameExample(spark: SparkSession): Unit = {
        // $example on:create_df$
        val df = spark.read.json("people.json")
    
        // Displays the content of the DataFrame to stdout
        df.show()
        // +----+-------+
        // | age|   name|
        // +----+-------+
        // |null|Michael|
        // |  30|   Andy|
        // |  19| Justin|
        // +----+-------+
        // $example off:create_df$
    
        // $example on:untyped_ops$
        // This import is needed to use the $-notation
        import spark.implicits._
        // Print the schema in a tree format
        df.printSchema()
        // root
        // |-- age: long (nullable = true)
        // |-- name: string (nullable = true)
    
        // Select only the "name" column
        df.select("name").show()
        // +-------+
        // |   name|
        // +-------+
        // |Michael|
        // |   Andy|
        // | Justin|
        // +-------+
    
        // Select everybody, but increment the age by 1
        df.select($"name", $"age" + 1).show()
        // +-------+---------+
        // |   name|(age + 1)|
        // +-------+---------+
        // |Michael|     null|
        // |   Andy|       31|
        // | Justin|       20|
        // +-------+---------+
    
        // Select people older than 21
        df.filter($"age" > 21).show()
        // +---+----+
        // |age|name|
        // +---+----+
        // | 30|Andy|
        // +---+----+
    
        // Count people by age
        df.groupBy("age").count().show()
        // +----+-----+
        // | age|count|
        // +----+-----+
        // |  19|    1|
        // |null|    1|
        // |  30|    1|
        // +----+-----+
        // $example off:untyped_ops$
    
        // $example on:run_sql$
        // Register the DataFrame as a SQL temporary view
        df.createOrReplaceTempView("people")
    
        val sqlDF = spark.sql("SELECT * FROM people")
        sqlDF.show()
        // +----+-------+
        // | age|   name|
        // +----+-------+
        // |null|Michael|
        // |  30|   Andy|
        // |  19| Justin|
        // +----+-------+
        // $example off:run_sql$
      }
    
      private def runDatasetCreationExample(spark: SparkSession): Unit = {
        import spark.implicits._
        // $example on:create_ds$
        // Encoders are created for case classes
        val caseClassDS = Seq(Person("Andy", 32)).toDS()
        caseClassDS.show()
        // +----+---+
        // |name|age|
        // +----+---+
        // |Andy| 32|
        // +----+---+
    
        // Encoders for most common types are automatically provided by importing spark.implicits._
        val primitiveDS = Seq(1, 2, 3).toDS()
        primitiveDS.map(_ + 1).collect() // Returns: Array(2, 3, 4)
    
        // DataFrames can be converted to a Dataset by providing a class. Mapping will be done by name
        val path = "people.json"
        val peopleDS = spark.read.json(path).as[Person]
        peopleDS.show()
        // +----+-------+
        // | age|   name|
        // +----+-------+
        // |null|Michael|
        // |  30|   Andy|
        // |  19| Justin|
        // +----+-------+
        // $example off:create_ds$
      }
    
      private def runInferSchemaExample(spark: SparkSession): Unit = {
        // $example on:schema_inferring$
        // For implicit conversions from RDDs to DataFrames
        import spark.implicits._
    
        // Create an RDD of Person objects from a text file, convert it to a Dataframe
        val peopleDF = spark.sparkContext
          .textFile("people.txt")
          .map(_.split(","))
          .map(attributes => Person(attributes(0), attributes(1).trim.toInt))
          .toDF()
        // Register the DataFrame as a temporary view
        peopleDF.createOrReplaceTempView("people")
    
        // SQL statements can be run by using the sql methods provided by Spark
        val teenagersDF = spark.sql("SELECT name, age FROM people WHERE age BETWEEN 13 AND 19")
    
        // The columns of a row in the result can be accessed by field index
        teenagersDF.map(teenager => "Name: " + teenager(0)).show()
        // +------------+
        // |       value|
        // +------------+
        // |Name: Justin|
        // +------------+
    
        // or by field name
        teenagersDF.map(teenager => "Name: " + teenager.getAs[String]("name")).show()
        // +------------+
        // |       value|
        // +------------+
        // |Name: Justin|
        // +------------+
    
        // No pre-defined encoders for Dataset[Map[K,V]], define explicitly
        implicit val mapEncoder = org.apache.spark.sql.Encoders.kryo[Map[String, Any]]
        // Primitive types and case classes can be also defined as
        // implicit val stringIntMapEncoder: Encoder[Map[String, Any]] = ExpressionEncoder()
    
        // row.getValuesMap[T] retrieves multiple columns at once into a Map[String, T]
        teenagersDF.map(teenager => teenager.getValuesMap[Any](List("name", "age"))).collect()
        // Array(Map("name" -> "Justin", "age" -> 19))
        // $example off:schema_inferring$
      }
    
      private def runProgrammaticSchemaExample(spark: SparkSession): Unit = {
        import spark.implicits._
        // $example on:programmatic_schema$
        // Create an RDD
        val peopleRDD = spark.sparkContext.textFile("people.txt")
    
        // The schema is encoded in a string
        val schemaString = "name age"
    
        // Generate the schema based on the string of schema
        val fields = schemaString.split(" ")
          .map(fieldName => StructField(fieldName, StringType, nullable = true))
        val schema = StructType(fields)
    
        // Convert records of the RDD (people) to Rows
        val rowRDD = peopleRDD
          .map(_.split(","))
          .map(attributes => Row(attributes(0), attributes(1).trim))
    
        // Apply the schema to the RDD
        val peopleDF = spark.createDataFrame(rowRDD, schema)
    
        // Creates a temporary view using the DataFrame
        peopleDF.createOrReplaceTempView("people")
    
        // SQL can be run over a temporary view created using DataFrames
        val results = spark.sql("SELECT name FROM people")
    
        // The results of SQL queries are DataFrames and support all the normal RDD operations
        // The columns of a row in the result can be accessed by field index or by field name
        results.map(attributes => "Name: " + attributes(0)).show()
        // +-------------+
        // |        value|
        // +-------------+
        // |Name: Michael|
        // |   Name: Andy|
        // | Name: Justin|
        // +-------------+
        // $example off:programmatic_schema$
      }
    }
    

    2.SQL查询

    import org.apache.spark.sql.SparkSession
    
    object SQLDataSourceExample {
    
      case class Person(name: String, age: Long)
    
      def main(args: Array[String]) {
        val spark = SparkSession
          .builder()
          .master("local")
          .appName("Spark SQL data sources example")
          .config("spark.some.config.option", "some-value")
          .getOrCreate()
    
        runBasicDataSourceExample(spark)
        runBasicParquetExample(spark)
        runParquetSchemaMergingExample(spark)
        runJsonDatasetExample(spark)
        //runJdbcDatasetExample(spark)
    
        spark.stop()
      }
    
      private def runBasicDataSourceExample(spark: SparkSession): Unit = {
        // $example on:generic_load_save_functions$
        val usersDF = spark.read.load("users.parquet")
        usersDF.select("name", "favorite_color").write.save("namesAndFavColors.parquet")
        // $example off:generic_load_save_functions$
        // $example on:manual_load_options$
        val peopleDF = spark.read.format("json").load("people.json")
        peopleDF.select("name", "age").write.format("parquet").save("namesAndAges.parquet")
        // $example off:manual_load_options$
        // $example on:direct_sql$
        val sqlDF = spark.sql("SELECT * FROM parquet.`users.parquet`")
        // $example off:direct_sql$
      }
    
      private def runBasicParquetExample(spark: SparkSession): Unit = {
        // $example on:basic_parquet_example$
        // Encoders for most common types are automatically provided by importing spark.implicits._
        import spark.implicits._
    
        val peopleDF = spark.read.json("people.json")
    
        // DataFrames can be saved as Parquet files, maintaining the schema information
        peopleDF.write.parquet("people.parquet")
    
        // Read in the parquet file created above
        // Parquet files are self-describing so the schema is preserved
        // The result of loading a Parquet file is also a DataFrame
        val parquetFileDF = spark.read.parquet("people.parquet")
    
        // Parquet files can also be used to create a temporary view and then used in SQL statements
        parquetFileDF.createOrReplaceTempView("parquetFile")
        val namesDF = spark.sql("SELECT name FROM parquetFile WHERE age BETWEEN 13 AND 19")
        namesDF.map(attributes => "Name: " + attributes(0)).show()
        // +------------+
        // |       value|
        // +------------+
        // |Name: Justin|
        // +------------+
        // $example off:basic_parquet_example$
      }
    
      private def runParquetSchemaMergingExample(spark: SparkSession): Unit = {
        // $example on:schema_merging$
        // This is used to implicitly convert an RDD to a DataFrame.
        import spark.implicits._
    
        // Create a simple DataFrame, store into a partition directory
        val squaresDF = spark.sparkContext.makeRDD(1 to 5).map(i => (i, i * i)).toDF("value", "square")
        squaresDF.write.parquet("data/test_table/key=1")
    
        // Create another DataFrame in a new partition directory,
        // adding a new column and dropping an existing column
        val cubesDF = spark.sparkContext.makeRDD(6 to 10).map(i => (i, i * i * i)).toDF("value", "cube")
        cubesDF.write.parquet("data/test_table/key=2")
    
        // Read the partitioned table
        val mergedDF = spark.read.option("mergeSchema", "true").parquet("data/test_table")
        mergedDF.printSchema()
    
        // The final schema consists of all 3 columns in the Parquet files together
        // with the partitioning column appeared in the partition directory paths
        // root
        //  |-- value: int (nullable = true)
        //  |-- square: int (nullable = true)
        //  |-- cube: int (nullable = true)
        //  |-- key: int (nullable = true)
        // $example off:schema_merging$
      }
    
      private def runJsonDatasetExample(spark: SparkSession): Unit = {
        // $example on:json_dataset$
        // A JSON dataset is pointed to by path.
        // The path can be either a single text file or a directory storing text files
        val path = "people.json"
        val peopleDF = spark.read.json(path)
    
        // The inferred schema can be visualized using the printSchema() method
        peopleDF.printSchema()
        // root
        //  |-- age: long (nullable = true)
        //  |-- name: string (nullable = true)
    
        // Creates a temporary view using the DataFrame
        peopleDF.createOrReplaceTempView("people")
    
        // SQL statements can be run by using the sql methods provided by spark
        val teenagerNamesDF = spark.sql("SELECT name FROM people WHERE age BETWEEN 13 AND 19")
        teenagerNamesDF.show()
        // +------+
        // |  name|
        // +------+
        // |Justin|
        // +------+
    
        // Alternatively, a DataFrame can be created for a JSON dataset represented by
        // an RDD[String] storing one JSON object per string
        val otherPeopleRDD = spark.sparkContext.makeRDD(
          """{"name":"Yin","address":{"city":"Columbus","state":"Ohio"}}""" :: Nil)
        val otherPeople = spark.read.json(otherPeopleRDD)
        otherPeople.show()
        // +---------------+----+
        // |        address|name|
        // +---------------+----+
        // |[Columbus,Ohio]| Yin|
        // +---------------+----+
        // $example off:json_dataset$
      }
    
      private def runJdbcDatasetExample(spark: SparkSession): Unit = {
        // $example on:jdbc_dataset$
        val jdbcDF = spark.read
          .format("jdbc")
          .option("url", "jdbc:postgresql:dbserver")
          .option("dbtable", "schema.tablename")
          .option("user", "username")
          .option("password", "password")
          .load()
        // $example off:jdbc_dataset$
      }
    }
    
  • 相关阅读:
    apache开源项目 -- Wicket
    读书笔记--《机器人时代》
    apache开源项目--dbutils
    apache开源项目--mina
    apache开源项目--OpenMeetings
    apache开源项目--lume
    apache开源项目--Sirona
    shared_ptr的简单实现
    高并发网络编程之epoll详解
    最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和
  • 原文地址:https://www.cnblogs.com/apppointint/p/8885289.html
Copyright © 2011-2022 走看看