Spark SQL, DataFrames and Datasets Guide

SQL:One use of Spark SQL is to execute SQL queries.


Datasets and DataFrames:

Datasets 是基于RDD的分布式数据容器。

而DataFrames则是有命名列名的Dataset。DataFrame的数据集都是按指定列存储,即结构化数据。类似于传统数据库中的表。 DataFrame的设计是为了让大数据处理起来更容易。DataFrame允许开发者把结构化数据集导入DataFrame,并做了higher-level的抽象; DataFrame提供特定领域的语言(DSL)API来操作你的数据集。


动手时刻:

1、创建SparkSession (spark2.0的特性)

2、可以将RDD、Hive表、Hive文件创建为DataFrame

val df = spark.read.json("examples/src/main/resources/people.json")

// Displays the content of the DataFrame to stdout
df.show()
// +----+-------+
// | age|   name|
// +----+-------+
// |null|Michael|
// |  30|   Andy|
// |  19| Justin|
// +----+-------+


3、对DataFrame的操作

// Select everybody, but increment the age by 1
df.select($"name", $"age" + 1).show()
// +-------+---------+
// |   name|(age + 1)|
// +-------+---------+
// |Michael|     null|
// |   Andy|       31|
// | Justin|       20|
// +-------+---------+
更多DataFrame操作:http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.functions$


4、如果你觉得上面的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|
// +----+-------+

5、创建DataSet

// 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)

// Encoders are created for case classes
val caseClassDS = Seq(Person("Andy", 32)).toDS()
caseClassDS.show()
// +----+---+
// |name|age|
// +----+---+
// |Andy| 32|
// +----+---+
// DataFrames can be converted to a Dataset by providing a class. Mapping will be done by name
val path = "examples/src/main/resources/people.json"
val peopleDS = spark.read.json(path).as[Person]
peopleDS.show()
// +----+-------+
// | age|   name|
// +----+-------+
// |null|Michael|
// |  30|   Andy|
// |  19| Justin|
// +----+-------+


6、根据RDD来创建DataSet

提供两种方式:

  • 利用反射机制来推断用特定类型对象的RDD的SCHEME,代码简洁,在事先知道scheme时效果好。
  • 利用编程接口,写个scheme,然后应用到RDD上,代码复杂。
7、数据来源

利用DataFrame接口,Spark Sql 支持操作来自各种数据源的数据。

  • 通用的LOAD、SAVE函数:
    val usersDF = spark.read.load("examples/src/main/resources/users.parquet")
    usersDF.select("name", "favorite_color").write.save("namesAndFavColors.parquet")
  • 手动设置读入、写出格式:
    val peopleDF = spark.read.format("json").load("examples/src/main/resources/people.json")
    peopleDF.select("name", "age").write.format("parquet").save("namesAndAges.parquet")
  • 当然,不想加载,还可以直接对文件写sql。。。。太变态了。。。。:
    val sqlDF = spark.sql("SELECT * FROM parquet.`examples/src/main/resources/users.parquet`")
  • 利用saveAsTable 将DataFrame保存为永久的HIVE表。

8、介绍了各种数据格式的注意事项,包括parquet、json

9、Hive table

Hive的依赖较多,必须在classpath里面找到这些依赖,所有的worknode也一样。

import org.apache.spark.sql.Row
import org.apache.spark.sql.SparkSession

case class Record(key: Int, value: String)

// warehouseLocation points to the default location for managed databases and tables
val warehouseLocation = "spark-warehouse"

val spark = SparkSession
  .builder()
  .appName("Spark Hive Example")
  .config("spark.sql.warehouse.dir", warehouseLocation)
  .enableHiveSupport()
  .getOrCreate()

import spark.implicits._
import spark.sql

sql("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")
sql("LOAD DATA LOCAL INPATH 'examples/src/main/resources/kv1.txt' INTO TABLE src")

// Queries are expressed in HiveQL
sql("SELECT * FROM src").show()
// +---+-------+
// |key|  value|
// +---+-------+
// |238|val_238|
// | 86| val_86|
// |311|val_311|
// ...


10、教你如何利用JDBC与其他数据库的数据进行交互

11、性能调优

  • 将数据缓存至内存中,spark.cacheTable("tableName") or dataFrame.cache()。spark.uncacheTable("tableName") to remove the table from memory.
  • 其他参数调节:


http://spark.apache.org/docs/latest/sql-programming-guide.html

你可能感兴趣的:(Spark)