Package: delta-lake-reader
Delta Lake ReaderTheDeltaformat, developed by Databricks, is often used to build data lakes or lakehouses.While it has many benefits, one of the downsides of delta tables is that they rely on Spark to read the data. This might be infeasible, or atleast introduce a lot of overhead, if you want to build data applications likeStreamlitapps or ML APIs ontop of the data in your Delta tables.This package tries to fix this, by providing a lightweight python wrapper around the delta file format,withoutany Spark dependenciesInstallationInstall the package using pippip install delta-lake-readerThis will only install the minimal dependencies for working with local file system.
To access Delta Tables stored in popular cloud storages, use one of the following commands, to include the cloud specific dependenciesAzurepip install delta-lake-reader[azure]Amazon Web Services (AWS)pip install delta-lake-reader[aws]Google Cloud Platform (GCP)pip install delta-lake-reader[gcp]UsagePackage is build onPyArrowandFSSpec.This means that you get all the features of PyArrow, like predicate pushdown, partition pruning and easy interoperability with Pandas.Meanwhile, FSSpec serves as a FileSystem agnostic backend, that lets you read files from many places, including popular cloud providers.To read a DeltaTable, first create aDeltaTableobject.
This will read the delta transaction log to find the current files, and get the schema. This will, however,notread any data.
To read the content of the table, callto_table()to get apyarrow.Tableobject, orto_pandas()to get apandas.DataFrameLocal file systemfromdeltalakeimportDeltaTable# native file path. Can be relative or absolutetable_path="somepath/mytable"# Get table as pyarrow tabledf=DeltaTable(table_path).to_table()# Get table as pandas dataframedf=DeltaTable(table_path).to_pandas()AzureThe Azure integration is based onadlfspackage, developed by theDaskcommunity.credentialused to authenticate against the storage account, can be either a SAS token, Access Keys or one of theazure.identityclasses. Seeauthentication using Azure SDKfor more information.fromdeltalakeimportDeltaTablefromadlfsimportAzureBlobFileSystem#example url  'abfss://myContainer@myStorageAccount.dfs.core.windows.net/somepath/mytable'fs=AzureBlobFileSystem(account_name="myStorageAccount",credential='...')df=DeltaTable("mycontainer/somepath/mytable",file_system=fs).to_pandas()Amazon Web Service (AWS)The AWS integration is based ons3fspackage, developed by theDaskcommunity.To authenticate you can either specify the access key and secret, or since it's build onboto, use one of their methods for authentication. Seeauthentication using AWS SDKfor more information.fromdeltalakeimportDeltaTablefroms3fsimportS3FileSystem#example url  's3://myBucket/somepath/mytable'fs=S3FileSystem()#authenticate using environment variables, in this exampledf=DeltaTable("myBucket/somepath/mytable",file_system=fs).to_pandas()Google Cloud Platform (GCP)The GCP integration is based on thegcsfs, developed by theDaskcommunity.For more information about authentication with GCP see thegcsfs documentationor theGCP documentationfromdeltalakeimportDeltaTablefromgcsfsimportGCSFileSystem#example url  'gs://myBucket/somepath/mytable'fs=GCSFileSystem()#authenticate using environment variables, in this exampledf=DeltaTable("myBucket/somepath/mytable",file_system=fs).to_pandas()Time travelOne of the features of the Delta format, is the ability to dotimetravel.This can be done using theas_versionmethod. Note that this currenly only support specific version, and not timestamp.fromdeltalakeimportDeltaTabledf=DeltaTable("somepath/mytable").as_version(5).to_pandas()Timetraveling to a version that has been vacuumed, currently results in undefined behaviorPredicate Pushdown, Partition Pruning & Columnar file formatsSince the resultingDeltaTableis based on thepyarrow.DataSet, you get many cool features for free.TheDeltaTable.to_tableis inherited frompyarrow.Dataset.to_table. This means that you can include arguments likefilter, which will do partition pruning and predicate pushdown. If you have a partitioned dataset, partition pruning can potentially reduce the data needed to be downloaded substantially. The predicate pushdown will not have any effect on the amount of data downloaded, but will reduce the dataset size when loaded into memory.Further more, since the underlying parquet file format is columnar, you can select a subset of columns to be read from the files. This can be done by passing a list of column names toto_table.See documentation ofto_pandas, orto_tablefor documentation of all argumentsimportpyarrow.datasetasds#Predicate pushdown.#If the table is partitioned on age, it will also to partition pruningdf=DeltaTable("...").to_table(filter=ds.field("age")>=18).to_pandas()#Only load a subset of columnsdf=DeltaTable("...").to_table(columns=["age","name"]).to_pandas()Read more about filtering data using PyArrowBring Your Own FilesystemSince the implementation is using the FSSpec for filesystem abstraction, you can in principle use any FSSpec filesystem.See more about available FSSpec interfaces.fs=SomeFSSpecFilesystem()df=DeltaTable(path,file_system=fs).to_pandas()Performance comparison with PySparkIt is possible to runPySparkin local mode, which means you can run spark code without having to spin up an entire cluster. This, however, still involves a big performance and resource usage overhead. To investigate if this module is actually faster than usingPySparki made a small experiment.The time to read a table into a pandas dataframe was measured for a table with 3 columns, and various number of rows.
The tables were stored locally on a VM (8 vCPUs, 32GB ram). This might be a synthetic test case since you normally would store your table in a remote blob store, where network latency would even out the results a bit.PySparkwas, however, still given an advantage by first being timed after starting the Spark session, which can take several seconds. Furthermore the resource usage byPySparkshould be significantly higher, both in terms of CPU and RAM, which can be another limiting factor. Finally, reading data from remote blob storage often requires adding cloud specific JARs to the runtime, which may or may not be tedious to get to work.The results can be seen below, wheredelta-lake-readeris about 100x faster thanPySparkon averageDisclaimer (2021-01-15)Databricks recently announced a stand alone reader for Delta tables in ablogpostThe stand alone reader is JVM based, but an "official" Rust implementation with python bindings also exists. Back then the python bindings couldn't be pip installed, which was a major inconvenience for python developers, but this has since been added.
While there is a lot of overlap between these two project, this projects still supports a few additional features, compared to the Rust implemtation, like more alternatives for authenticating to azure (identity based, instead of only relying on account key) and support for more file systems like GCP buckets.
If you, however, are interested in a more actively maintained package, if would recommend taking a look atthe Delta Rust implemtation.
Although the idea for this library was made independently, some inspirations has been taken from the Rust library.Read moreDelta Table paperDelta transaction logPyArrow DocumentationFSSpec Documentation