Sub topic 9
Reading shared tables from Python and Spark
This chapter assumes you have a profile file from a provider (see the previous chapter on profile files) and know basic pandas and Spark DataFrames. You will list the tables you can see, then load one into pandas and into Spark.
The working model
A recipient never connects to the provider's storage account directly. The connector asks the sharing server which files make up a table. The server answers with a list of pre-signed URLs. The connector then downloads those Parquet files straight from cloud storage and assembles them into a DataFrame.
Two consequences follow. The sharing server never carries the table data, and the pre-signed URLs expire, so a connector must request fresh ones for each read.
Installing the Python connector
Install the connector with pip. The pyspark extra is only needed if you also plan to use Spark.
pip install delta-sharingListing available tables
SharingClient reads the profile file and asks the server what you have been granted.
import delta_sharing
profile = "config.share"
client = delta_sharing.SharingClient(profile)
for table in client.list_all_tables():
print(table)Each result carries a share, a schema and a table name. Those three parts build the table URL.
Loading into pandas
A table URL is the profile path, a #, then share.schema.table.
import delta_sharing
table_url = "config.share#my_share.my_schema.my_table"
df = delta_sharing.load_as_pandas(table_url)
print(df.head())load_as_pandas downloads every data file of the table into the memory of one machine. That is fine for small tables. For a large table it can exhaust memory, so use Spark or limit the rows.
import delta_sharing
table_url = "config.share#my_share.my_schema.my_table"
df = delta_sharing.load_as_pandas(table_url, limit=1000)
print(len(df))The limit argument is passed to the server as a hint, so fewer files are downloaded.
Loading into Spark
With the Delta Sharing Spark connector installed, use the deltaSharing format.
df = spark.read.format("deltaSharing").load("config.share#my_share.my_schema.my_table")
df.show()Spark spreads the file downloads across executors. Every executor must be able to read the profile file, so on a cluster put it on a shared path such as DBFS or a distributed file system, not only on your laptop.
If you work outside a managed Spark platform, the connector is a package you add when starting the session, and its version must match your Spark and Scala versions. A mismatch is a common cause of class-not-found errors.
Choosing pandas or Spark
| Situation | Better choice |
|---|---|
| Table fits in memory, quick exploration | `load_as_pandas` |
| Large table or joins with other data | Spark `deltaSharing` format |
| Need only a sample | `load_as_pandas` with `limit` |
Common errors
- Table not found. The table URL uses the wrong share, schema or table name. Compare it with the output of
list_all_tables(). - Authentication failure. The token is wrong or past the
expirationTimein the profile. Ask the provider for a new profile. - Profile not found on a cluster. The path is valid only on your local machine. Use a path every node can read.
- Download errors on old data. A pre-signed URL expired between the request and the download. Rerun the read to get new URLs.
Exercise
A colleague runs loadaspandas on a 200 GB shared table and the process crashes. What went wrong, and what are two ways to fix it?
Show answer
loadaspandas downloads all data files into the memory of a single machine, which a 200 GB table exceeds. Either read the table with Spark using spark.read.format("deltaSharing") so the work is spread across executors, or pass a limit to loadaspandas if only a sample is needed.
Exercise
Write the table URL for the table orders in schema sales of share retail_share, using the profile file prod.share.
Show answer
prod.share#retail_share.sales.orders