关注 spark技术分享,
撸spark源码 玩spark最佳实践

Basic Actions

Dataset API — Basic Actions

Basic actions are part of the Dataset API for transforming a Dataset into a session-scoped or global temporary view and other basic actions (FIXME).

Note
Basic actions are the methods in the Dataset Scala class that are grouped in basic group name, i.e. @group basic.
Table 1. Dataset API’s Basic Actions
Action Description

cache

Caches the Dataset

checkpoint

Checkpoints the Dataset in a reliable way (using a reliable HDFS-compliant file system, e.g. Hadoop HDFS or Amazon S3)

columns

createGlobalTempView

createOrReplaceGlobalTempView

createOrReplaceTempView

createTempView

dtypes

explain

Displays the logical and physical plans of the Dataset, i.e. displays the logical and physical plans (with optional cost and codegen summaries) to the standard output

hint

inputFiles

isEmpty

(New in 2.4.0)

isLocal

localCheckpoint

Checkpoints the Dataset locally on executors (and therefore unreliably)

persist

Persists the Dataset

printSchema

rdd

schema

storageLevel

toDF

unpersist

Unpersists the Dataset

write

Returns a DataFrameWriter for saving the content of the (non-streaming) Dataset out to an external storage

Caching Dataset — cache Basic Action

cache merely executes the no-argument persist basic action.

Reliably Checkpointing Dataset — checkpoint Basic Action

  1. eager and reliableCheckpoint flags enabled

  2. reliableCheckpoint flag enabled

Note
checkpoint is an experimental operator and the API is evolving towards becoming stable.

checkpoint simply requests the Dataset to checkpoint with the given eager flag and the reliableCheckpoint flag enabled.

createTempView Basic Action

createTempView…​FIXME

Note
createTempView is used when…​FIXME

createOrReplaceTempView Basic Action

createOrReplaceTempView…​FIXME

Note
createOrReplaceTempView is used when…​FIXME

createGlobalTempView Basic Action

createGlobalTempView…​FIXME

Note
createGlobalTempView is used when…​FIXME

createOrReplaceGlobalTempView Basic Action

createOrReplaceGlobalTempView…​FIXME

Note
createOrReplaceGlobalTempView is used when…​FIXME

createTempViewCommand Internal Method

createTempViewCommand…​FIXME

Note
createTempViewCommand is used when the following Dataset operators are used: Dataset.createTempView, Dataset.createOrReplaceTempView, Dataset.createGlobalTempView and Dataset.createOrReplaceGlobalTempView.

Displaying Logical and Physical Plans, Their Cost and Codegen — explain Basic Action

  1. Turns the extended flag on

explain prints the logical and (with extended flag enabled) physical plans, their cost and codegen to the console.

Tip
Use explain to review the structured queries and optimizations applied.

Internally, explain creates a ExplainCommand logical command and requests SessionState to execute it (to get a QueryExecution back).

Note
explain uses ExplainCommand logical command that, when executed, gives different text representations of QueryExecution (for the Dataset’s LogicalPlan) depending on the flags (e.g. extended, codegen, and cost which are disabled by default).

explain then requests QueryExecution for the optimized physical query plan and collects the records (as InternalRow objects).

Note

explain uses Dataset’s SparkSession to access the current SessionState.

In the end, explain goes over the InternalRow records and converts them to lines to display to console.

Note
explain “converts” an InternalRow record to a line using getString at position 0.
Tip
If you are serious about query debugging you could also use the Debugging Query Execution facility.

Specifying Hint — hint Basic Action

hint operator is part of Hint Framework to specify a hint (by name and parameters) for a Dataset.

Internally, hint simply attaches UnresolvedHint unary logical operator to an “analyzed” Dataset (i.e. the analyzed logical plan of a Dataset).

Note
hint adds an UnresolvedHint unary logical operator to an analyzed logical plan that indirectly triggers analysis phase that executes logical commands and their unions as well as resolves all hints that have already been added to a logical plan.

Locally Checkpointing Dataset — localCheckpoint Basic Action

  1. eager flag enabled

localCheckpoint simply uses Dataset.checkpoint operator with the input eager flag and reliableCheckpoint flag disabled (false).

checkpoint Internal Method

checkpoint requests QueryExecution (of the Dataset) to generate an RDD of internal binary rows (aka internalRdd) and then requests the RDD to make a copy of all the rows (by adding a MapPartitionsRDD).

Depending on reliableCheckpoint flag, checkpoint marks the RDD for (reliable) checkpointing (true) or local checkpointing (false).

With eager flag on, checkpoint counts the number of records in the RDD (by executing RDD.count) that gives the effect of immediate eager checkpointing.

checkpoint requests QueryExecution (of the Dataset) for optimized physical query plan (the plan is used to get the outputPartitioning and outputOrdering for the result Dataset).

Note
checkpoint is used in the Dataset untyped transformations, i.e. checkpoint and localCheckpoint.

Persisting Dataset — persist Basic Action

persist caches the Dataset using the default storage level MEMORY_AND_DISK or newLevel and returns it.

Internally, persist requests CacheManager to cache the structured query (that is accessible through SharedState of the current SparkSession).

Caution
FIXME

Generating RDD of Internal Binary Rows — rdd Basic Action

Whenever you are in need to convert a Dataset into a RDD, executing rdd method gives you the RDD of the proper input object type (not Row as in DataFrames) that sits behind the Dataset.

Internally, it looks ExpressionEncoder (for the Dataset) up and accesses the deserializer expression. That gives the DataType of the result of evaluating the expression.

Note
A deserializer expression is used to decode an InternalRow to an object of type T. See ExpressionEncoder.

It then executes a DeserializeToObject logical operator that will produce a RDD[InternalRow] that is converted into the proper RDD[T] using the DataType and T.

Note
It is a lazy operation that “produces” a RDD[T].

Accessing Schema — schema Basic Action

A Dataset has a schema.

Tip

You may also use the following methods to learn about the schema:

Converting Typed Dataset to Untyped DataFrame — toDF Basic Action

toDF converts a Dataset into a DataFrame.

Internally, the empty-argument toDF creates a Dataset[Row] using the Dataset‘s SparkSession and QueryExecution with the encoder being RowEncoder.

Caution
FIXME Describe toDF(colNames: String*)

Unpersisting Cached Dataset — unpersist Basic Action

unpersist uncache the Dataset possibly by blocking the call.

Internally, unpersist requests CacheManager to uncache the query.

Caution
FIXME

Accessing DataFrameWriter (to Describe Writing Dataset) — write Basic Action

write gives DataFrameWriter for records of type T.

isEmpty Typed Transformation

isEmpty…​FIXME

isLocal Typed Transformation

isLocal…​FIXME

赞(0) 打赏
未经允许不得转载:spark技术分享 » Basic Actions
分享到: 更多 (0)

关注公众号:spark技术分享

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏