SparkSession - Spark SQL 的 入口


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 可以看到相應的日志。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM