SparkSession - Spark SQL 的 入口

翻译自:https://jaceklaskowski.gitbooks.io/mastering-apache-spark/content/spark-sql-SparkSession.html

概述

SparkSession 是 Spark SQL 的入口。使用 Dataset 或者 Datafram 编写 Spark SQL 应用的时候,第一个要创建的对象就是 SparkSession。

Note:在 Spark 2.0 中, SparkSession 合并了 SQLContext 和 HiveContext。


你可以通过 SparkSession.builder 来创建一个 SparkSession 的实例,并通过 stop 函数来停止 SparkSession。

import org.apache.spark.sql.SparkSession
val spark: SparkSession = SparkSession.builder
.appName("My Spark Application") // optional and will be autogenerated if not specified
.master("local[*]") // avoid hardcoding the deployment environment
.enableHiveSupport() // self-explanatory, isn't it?
.config("spark.sql.warehouse.dir", "target/spark-warehouse")
.getOrCreate

你可以在一个 Spark 应用中使用多个 SparkSession, 这样子就可以通过 SparSession 将多个关系实体隔离开来(可以参考 catalog 属性)。

scala> spark.catalog.listTables.show
+------------------+--------+-----------+---------+-----------+
| name|database|description|tableType|isTemporary|
+------------------+--------+-----------+---------+-----------+
|my_permanent_table| default| null| MANAGED| false|
| strs| null| null|TEMPORARY| true|
+------------------+--------+-----------+---------+-----------+



在 SparkSession 的内部, 包含了SparkContext, SharedState,SessionState 几个对象。下表中介绍了每个对象的大体功能:

Name Type Description
sparkContext SparkContext spark功能的主要入口点。可以通过 sparkConext在集群上创建RDD, accumulators 和 broadcast variables
existingSharedState Option[SharedState] 一个内部类负责保存不同session的共享状态
parentSessionState Option[SessionState] 复制父session的状态



下图是 SparkSession 的类和方法, 这些方法包含了创建 DataSet, DataFrame, Streaming 等等。

Method Description
builder "Opens" a builder to get or create a SparkSession instance
version Returns the current version of Spark.
implicits Use import spark.implicits._ to import the implicits conversions and create Datasets from (almost arbitrary) Scala objects.
emptyDataset[T] Creates an empty Dataset[T].
range Creates a Dataset[Long].
sql Executes a SQL query (and returns a DataFrame).
udf Access to user-defined functions (UDFs).
table Creates a DataFrame from a table.
catalog Access to the catalog of the entities of structured queries
read Access to DataFrameReader to read a DataFrame from external files and storage systems.
conf Access to the current runtime configuration.
readStream Access to DataStreamReader to read streaming datasets.
streams Access to StreamingQueryManager to manage structured streaming queries.
newSession Creates a new SparkSession.
stop Stops the SparkSession.




Builder

Builder 是 SparkSession 的构造器。 通过 Builder, 可以添加各种配置。

Builder 的方法如下:

Method Description
getOrCreate 获取或者新建一个 sparkSession
enableHiveSupport 增加支持 hive Support
appName 设置 application 的名字
config 设置各种配置
import org.apache.spark.sql.SparkSession
val spark: SparkSession = SparkSession.builder
.appName("My Spark Application") // optional and will be autogenerated if not specified
.master("local[*]") // avoid hardcoding the deployment environment
.enableHiveSupport() // self-explanatory, isn't it?
.getOrCreate




ShareState

ShareState 是 SparkSession 的一个内部类,负责保存多个有效session之间的共享状态。下表介绍了ShareState的属性。

Name Type Description
cacheManager CacheManager 这个是 SQLContext 的支持类,会自动保存 query 的查询结果。这样子查询在执行过程中,就可以使用这些查询结果
externalCatalog ExternalCatalog 保存外部系统的 catalog
globalTempViewManager GlobalTempViewManager 一个线程安全的类,用来管理 global temp view, 并提供 create , update , remove 的等原子操作,来管理这些 view
jarClassLoader NonClosableMutableURLClassLoader 加载用户添加的 jar 包
listener SQLListener 一个监听类
sparkContext SparkContext Spark 的核心入口类
warehousePath String MetaStore 的地址,可以通过 spark.sql.warehouse.dir 或者 hive-site.xml 中的 hive.metastore.warehouse.dir 来指定, Spark 会覆盖 hive 的参数

ShareState 会使用一个 sparkContext 作为构造参数。如果可以在 CLASSPATH 中找到 hive-site.xml,ShareState 会将它加入到 sparkContext 的 hadoop configuration 中。

通过设置 log4j.logger.org.apache.spark.sql.internal.SharedState=INFO 可以看到相应的日志。

最新文章

  1. [LeetCode] Remove Linked List Elements 移除链表元素
  2. backup
  3. Linux内核补丁批量自动下载工具
  4. 关于是用dotnet获取本机IP地址+计算机名的方法
  5. 03-c#入门(简易存款利息计算器v1.0)
  6. CEF3开发者系列之JS与C++交互之一
  7. alpha发布之小组评论
  8. xilinx 网站资源导读
  9. mysql 账户操作
  10. codeblocks创建和使用静态库(C语言)
  11. Linq 调试
  12. PHP安全编程:留心后门URL 直接可以通过URL访问(转)
  13. redis常见命令
  14. Firemonkey使用iOS的第三方静态库(Link Binary With Libraries)
  15. QT序列化操作(比较复杂和完善)
  16. BZOJ 1196 二分答案+并查集
  17. ecshop商品页增加编辑器fckeditor
  18. Go语言学习笔记(六)net
  19. AddIn 中当前完整文件名的获取
  20. TypeError: Cannot red property 'style' of null 错误解决

热门文章

  1. c:forTokens标签delims截取字符
  2. 13.ubuntu下Qt5无法使用中文的问题解决
  3. php.ini控制文件上传大小配置项
  4. 用VS2015创建ASP.NET Web Forms 应用程序
  5. Java NIO(四)文件通道
  6. Android 实现调用系统拍照相册,剪切功能
  7. 从源码中查看当前android版本
  8. LeetCode Golang 7. 整数反转
  9. jsp基本内置标签
  10. 解决MYSQL的错误:Got a packet bigger than 'max_allowed_packet' bytes