docs/src/content/docs/delta-storage.mdx
import { Tabs, TabItem, Aside } from "@astrojs/starlight/components";
Delta Lake ACID guarantees are predicated on the atomicity and durability guarantees of the storage system. Specifically, Delta Lake relies on the following when interacting with storage systems:
Because storage systems do not necessarily provide all of these guarantees out-of-the-box, Delta Lake transactional operations typically go through the LogStore API instead of accessing the storage system directly. To provide the ACID guarantees for different storage systems, you may have to use different LogStore implementations. This article covers how to configure Delta Lake for various storage systems. There are two categories of storage systems:
Storage systems with built-in support: For some storage systems, you do not need additional configurations. Delta Lake uses the scheme of the path (that is, s3a in s3a://path) to dynamically identify the storage system and use the corresponding LogStore implementation that provides the transactional guarantees. However, for S3, there are additional caveats on concurrent writes. See the section on S3 for details.
Other storage systems: The LogStore, similar to Apache Spark, uses Hadoop FileSystem API to perform reads and writes. So Delta Lake supports concurrent reads on any storage system that provides an implementation of FileSystem API. For concurrent writes with transactional guarantees, there are two cases based on the guarantees provided by FileSystem implementation. If the implementation provides consistent listing and atomic renames-without-overwrite (that is, rename(..., overwrite = false) will either generate the target file atomically or fail if it already exists with java.nio.file.FileAlreadyExistsException), then the default LogStore implementation using renames will allow concurrent writes with guarantees. Otherwise, you must configure a custom implementation of LogStore by setting the following Spark configuration:
spark.delta.logStore.<scheme>.impl=<full-qualified-class-name>
where <scheme> is the scheme of the paths of your storage system. This configures Delta Lake to dynamically use the given LogStore implementation only for those paths. You can have multiple such configurations for different schemes in your application, thus allowing it to simultaneously read and write from different storage systems.
If you see an error like java.lang.NoClassDefFoundError: io/delta/storage/LogStore it usually means the Delta Storage dependency is missing from the Spark classpath.
com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: io/delta/storage/LogStore
Please ensure that the delta-storage dependency is included.
If using Python, please ensure you call `configure_spark_with_delta_pip` or use
`--packages io.delta:delta-spark_<scala-version>:<delta-lake-version>`.
See https://docs.delta.io/latest/quick-start.html#python.
More information about this dependency and how to include it can be found here:
https://docs.delta.io/latest/porting.html#delta-lake-1-1-or-below-to-delta-lake-1-2-or-above.
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2084)
at com.google.common.cache.LocalCache.get(LocalCache.java:4017)
at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4898)
at org.apache.spark.sql.delta.DeltaLog$.getDeltaLogFromCache$1(DeltaLog.scala:995)
at org.apache.spark.sql.delta.DeltaLog$.initializeDeltaLog$1(DeltaLog.scala:1006)
at org.apache.spark.sql.delta.DeltaLog$.apply(DeltaLog.scala:1017)
at org.apache.spark.sql.delta.DeltaLog$.forTable(DeltaLog.scala:801)
at org.apache.spark.sql.delta.sources.DeltaDataSource.createRelation(DeltaDataSource.scala:197)
at org.apache.spark.sql.execution.datasources.SaveIntoDataSourceCommand.run(SaveIntoDataSourceCommand.scala:55)
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:79)
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:77)
at org.apache.spark.sql.execution.command.ExecutedCommandExec.executeCollect(commands.scala:88)
at org.apache.spark.sql.execution.QueryExecution.$anonfun$eagerlyExecuteCommands$2(QueryExecution.scala:155)
at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId0$8(SQLExecution.scala:162)
at org.apache.spark.sql.execution.SQLExecution$.withSessionTagsApplied(SQLExecution.scala:268)
at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId0$7(SQLExecution.scala:124)
at org.apache.spark.JobArtifactSet$.withActiveJobArtifactState(JobArtifactSet.scala:94)
at org.apache.spark.sql.artifact.ArtifactManager.$anonfun$withResources$1(ArtifactManager.scala:112)
at org.apache.spark.sql.artifact.ArtifactManager.withClassLoaderIfNeeded(ArtifactManager.scala:106)
at org.apache.spark.sql.artifact.ArtifactManager.withResources(ArtifactManager.scala:111)
at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId0$6(SQLExecution.scala:124)
at org.apache.spark.sql.execution.SQLExecution$.withSQLConfPropagated(SQLExecution.scala:291)
at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId0$1(SQLExecution.scala:123)
at org.apache.spark.sql.SparkSession.withActive(SparkSession.scala:804)
at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId0(SQLExecution.scala:77)
at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:233)
at org.apache.spark.sql.execution.QueryExecution.$anonfun$eagerlyExecuteCommands$1(QueryExecution.scala:155)
at org.apache.spark.sql.execution.QueryExecution$.withInternalError(QueryExecution.scala:654)
at org.apache.spark.sql.execution.QueryExecution.org$apache$spark$sql$execution$QueryExecution$$eagerlyExecute$1(QueryExecution.scala:154)
at org.apache.spark.sql.execution.QueryExecution$$anonfun$eagerlyExecuteCommands$3.applyOrElse(QueryExecution.scala:169)
at org.apache.spark.sql.execution.QueryExecution$$anonfun$eagerlyExecuteCommands$3.applyOrElse(QueryExecution.scala:164)
at org.apache.spark.sql.catalyst.trees.TreeNode.$anonfun$transformDownWithPruning$1(TreeNode.scala:470)
at org.apache.spark.sql.catalyst.trees.CurrentOrigin$.withOrigin(origin.scala:86)
at org.apache.spark.sql.catalyst.trees.TreeNode.transformDownWithPruning(TreeNode.scala:470)
at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.org$apache$spark$sql$catalyst$plans$logical$AnalysisHelper$$super$transformDownWithPruning(LogicalPlan.scala:37)
at org.apache.spark.sql.catalyst.plans.logical.AnalysisHelper.transformDownWithPruning(AnalysisHelper.scala:360)
at org.apache.spark.sql.catalyst.plans.logical.AnalysisHelper.transformDownWithPruning$(AnalysisHelper.scala:356)
at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.transformDownWithPruning(LogicalPlan.scala:37)
at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.transformDownWithPruning(LogicalPlan.scala:37)
at org.apache.spark.sql.catalyst.trees.TreeNode.transformDown(TreeNode.scala:446)
at org.apache.spark.sql.execution.QueryExecution.eagerlyExecuteCommands(QueryExecution.scala:164)
at org.apache.spark.sql.execution.QueryExecution.$anonfun$lazyCommandExecuted$1(QueryExecution.scala:126)
at scala.util.Try$.apply(Try.scala:217)
at org.apache.spark.util.Utils$.doTryWithCallerStacktrace(Utils.scala:1378)
at org.apache.spark.util.Utils$.getTryWithCallerStacktrace(Utils.scala:1439)
at org.apache.spark.util.LazyTry.get(LazyTry.scala:58)
at org.apache.spark.sql.execution.QueryExecution.commandExecuted(QueryExecution.scala:131)
at org.apache.spark.sql.execution.QueryExecution.assertCommandExecuted(QueryExecution.scala:192)
at org.apache.spark.sql.classic.DataFrameWriter.runCommand(DataFrameWriter.scala:622)
at org.apache.spark.sql.classic.DataFrameWriter.saveToV1Source(DataFrameWriter.scala:273)
at org.apache.spark.sql.classic.DataFrameWriter.saveInternal(DataFrameWriter.scala:235)
at org.apache.spark.sql.classic.DataFrameWriter.save(DataFrameWriter.scala:118)
... 42 elided
When you provide the Delta Spark JAR using the --jars option (for example, when testing a locally-built JAR), Spark does not automatically fetch transitive dependencies. In this case, delta-spark may be present, but delta-storage is not.
As a result, operations that need to initialize the Delta log (for example, writing a Delta table) can fail when Delta attempts to load the LogStore API:
df.write.format("delta").save("/tmp/delta")
When using the --jars option, you can do either of the following:
delta-spark and delta-storage JARs:spark-shell \
--jars delta-spark_<scala-version>-<delta-version>.jar,delta-storage-<delta-version>.jar \
--conf "spark.sql.extensions=io.delta.sql.DeltaSparkSessionExtension" \
--conf "spark.sql.catalog.spark_catalog=org.apache.spark.sql.delta.catalog.DeltaCatalog"
build/sbt "spark/assembly"):spark-shell \
--jars delta-spark-assembly-<delta-version>.jar \
--conf "spark.sql.extensions=io.delta.sql.DeltaSparkSessionExtension" \
--conf "spark.sql.catalog.spark_catalog=org.apache.spark.sql.delta.catalog.DeltaCatalog"
Delta Lake supports reads and writes to S3 in two different modes: Single-cluster and Multi-cluster.
| Single-cluster | Multi-cluster | |
|---|---|---|
| Configuration | Comes with Delta Lake out-of-the-box | Is experimental and requires extra configuration |
| Reads | Supports concurrent reads from multiple clusters | Supports concurrent reads from multiple clusters |
| Writes | Supports concurrent writes from a single Spark driver | Supports multi-cluster writes |
| Permissions | S3 credentials | S3 and DynamoDB operating permissions |
In this default mode, Delta Lake supports concurrent reads from multiple clusters, but concurrent writes to S3 must originate from a single Spark driver in order for Delta Lake to provide transactional guarantees. This is because S3 currently does not provide mutual exclusion, that is, there is no way to ensure that only one writer is able to create a file.
<Aside type="caution"> Concurrent writes to the same Delta table on S3 storage from multiple Spark drivers can lead to data loss. For a multi-cluster solution, please see the [Multi-cluster setup](#multi-cluster-setup) section below. </Aside>This section explains how to quickly start reading and writing Delta tables on S3 using single-cluster mode. For a detailed explanation of the configuration, see Setup Configuration (S3 multi-cluster).
```bash
bin/spark-shell \
--packages io.delta:delta-spark_2.13:4.0.0,org.apache.hadoop:hadoop-aws:3.4.0 \
--conf spark.hadoop.fs.s3a.access.key=<your-s3-access-key> \
--conf spark.hadoop.fs.s3a.secret.key=<your-s3-secret-key>
```
// Read a Delta table on S3:
spark.read.format("delta").load("s3a://<your-s3-bucket>/<path-to-delta-table>").show()
```
For other languages and more examples of Delta table operations, see the Quickstart page.
For efficient listing of Delta Lake metadata files on S3, set the configuration delta.enableFastS3AListFrom=true. This performance optimization is in experimental support mode. It will only work on S3A filesystems and will not work on Amazon's EMR default filesystem S3.
This mode supports concurrent writes to S3 from multiple clusters and has to be explicitly enabled by configuring Delta Lake to use the right LogStore implementation. This implementation uses DynamoDB to provide the mutual exclusion that S3 is lacking.
This section explains how to quickly start reading and writing Delta tables on S3 using multi-cluster mode.
// Read a Delta table on S3:
spark.read.format("delta").load("s3a://<your-s3-bucket>/<path-to-delta-table>").show()
```
For other languages and more examples of Delta table operations, see the Quickstart page.
Create the DynamoDB table.
You have the choice of creating the DynamoDB table yourself (recommended) or having it created for you automatically.
Creating the DynamoDB table yourself
This DynamoDB table will maintain commit metadata for multiple Delta tables, and it is important that it is configured with the Read/Write Capacity Mode (for example, on-demand or provisioned) that is right for your use cases. As such, we strongly recommend that you create your DynamoDB table yourself. The following example uses the AWS CLI. To learn more, see the create-table command reference.
Follow the configuration steps listed in Configuration (S3 single-cluster) section.
Include the delta-storage-s3-dynamodb JAR in the classpath.
Configure the LogStore implementation in your Spark session.
First, configure this LogStore implementation for the scheme s3. You can replicate this command for schemes s3a and s3n as well.
Next, specify additional information necessary to instantiate the DynamoDB client. You must instantiate the DynamoDB client with the same tableName and region each Spark session for this multi-cluster mode to work correctly. A list of per-session configurations and their defaults is given below:
By this point, this multi-cluster setup is fully operational. However, there is extra configuration you may do to improve performance and optimize storage when running in production.
Adjust your Read and Write Capacity Mode.
If you are using the default DynamoDB table created for you by this LogStore implementation, its default RCU and WCU might not be enough for your workloads. You can adjust the provisioned throughput or update to On-Demand Mode.
Cleanup old DynamoDB entries using Time to Live (TTL).
Once a DynamoDB metadata entry is marked as complete, and after sufficient time such that we can now rely on S3 alone to prevent accidental overwrites on its corresponding Delta file, it is safe to delete that entry from DynamoDB. The cheapest way to do this is using DynamoDB's TTL feature which is a free, automated means to delete items from your DynamoDB table.
Run the following command on your given DynamoDB table to enable TTL:
The default expireTime will be one day after the DynamoDB entry was marked as completed.
Cleanup old AWS S3 temp files using S3 Lifecycle Expiration.
In this LogStore implementation, a temp file is created containing a copy of the metadata to be committed into the Delta log. Once that commit to the Delta log is complete, and after the corresponding DynamoDB entry has been removed, it is safe to delete this temp file. In practice, only the latest temp file will ever be used during recovery of a failed commit.
Here are two simple options for deleting these temp files:
Delete manually using S3 CLI.
This is the safest option. The following command will delete all but the latest temp file in your given <bucket> and <table>:
Delete using an S3 Lifecycle Expiration Rule
A more automated option is to use an S3 Lifecycle Expiration rule, with filter prefix pointing to the <delta_table_path>/_delta_log/.tmp/ folder located in your table path, and an expiration value of 30 days.
There are a variety of ways to configuring a bucket lifecycle configuration, described in AWS docs [here](https://docs.aws.amazon.com/AmazonS3/latest/userguide/how-to-set-lifecycle-configuration-intro.html).
One way to do this is using S3's `put-bucket-lifecycle-configuration` command. See [S3 Lifecycle Configuration](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-lifecycle-configuration.html) for details. An example rule and command invocation is given below:
In a file referenced as `file://lifecycle.json`:
Delta Lake has built-in support for the various Azure storage systems with full transactional guarantees for concurrent reads and writes from multiple clusters.
Delta Lake relies on Hadoop FileSystem APIs to access Azure storage services. Specifically, Delta Lake requires the implementation of FileSystem.rename() to be atomic, which is only supported in newer Hadoop versions (Hadoop-15156 and Hadoop-15086). For this reason, you may need to build Spark with newer Hadoop versions and use them for deploying your application. See Specifying the Hadoop Version and Enabling YARN for building Spark with a specific Hadoop version and Quickstart for setting up Spark with Delta Lake.
Here is a list of requirements specific to each type of Azure storage system:
For example, a possible combination that will work is Delta 0.7.0 or above, along with Apache Spark 3.0 compiled and deployed with Hadoop 3.2.
Here are the steps to configure Delta Lake on Azure Blob storage.
Include hadoop-azure JAR in the classpath. See the requirements above for version details.
Set up credentials.
You can set up your credentials in the Spark configuration property.
We recommend that you use a SAS token. In Scala, you can use the following:
Or you can specify an account access key:
<Tabs> <TabItem label="Scala"> ```scala spark.conf.set( "fs.azure.account.key.<your-storage-account-name>.blob.core.windows.net", "<your-storage-account-access-key>") ``` </TabItem> </Tabs>Here are the steps to configure Delta Lake on Azure Data Lake Storage Gen1.
Include hadoop-azure-datalake JAR in the classpath. See the requirements above for version details.
Set up Azure Data Lake Storage Gen1 credentials.
You can set the following Hadoop configurations with your credentials (in Scala):
Here are the steps to configure Delta Lake on Azure Data Lake Storage Gen2.
Include hadoop-azure and azure-storage JARs in the classpath. See the requirements above for version details.
Set up credentials.
You can use either OAuth 2.0 with service principal or shared key authentication:
For OAuth 2.0 with service principal (recommended):
For shared key authentication:
<Tabs> <TabItem label="Scala"> ```scala spark.conf.set("fs.azure.account.key.<storage-account>.dfs.core.windows.net", "<storage-account-access-key>") ``` </TabItem> </Tabs>Delta Lake has built-in support for HDFS with full transactional guarantees for concurrent reads and writes from multiple clusters. No additional configuration is required.
Delta Lake has built-in support for Google Cloud Storage (GCS) with full transactional guarantees for concurrent reads and writes from multiple clusters.
Include the GCS connector JAR in the classpath.
Set up credentials using one of the following methods:
Delta Lake supports Oracle Cloud Infrastructure (OCI) Object Storage with full transactional guarantees for concurrent reads and writes from multiple clusters.
Include the OCI connector JAR in the classpath.
Set up credentials in Spark configuration:
Delta Lake supports IBM Cloud Object Storage with full transactional guarantees for concurrent reads and writes from multiple clusters.
Include the Stocator connector JAR in the classpath.
Set up credentials in Spark configuration: