Use pyarrow with Azure Data Lake gen2
pip install pyarrowfs-adlgen2pyarrowfs-adlgen2
pyarrowfs-adlgen2 is an implementation of a pyarrow filesystem for Azure Data Lake Gen2.
It allows you to use pyarrow and pandas to read parquet datasets directly from Azure without the need to copy files to local storage first.
Compared with adlfs, you may see better performance when reading datasets with many files, as pyarrowfs-adlgen2 uses the datalake gen2 sdk, which has fast directory listing, unlike the blob sdk used by adlfs.
pyarrowfs-adlgen2 is stable software with a small API, and no major features are planned.
Installation
pip install pyarrowfs-adlgen2
Reading datasets
Example usage with pandas dataframe:
import azure.identity
import pandas as pd
import pyarrow.fs
import pyarrowfs_adlgen2
handler = pyarrowfs_adlgen2.AccountHandler.from_account_name(
'YOUR_ACCOUNT_NAME', azure.identity.DefaultAzureCredential())
fs = pyarrow.fs.PyFileSystem(handler)
df = pd.read_parquet('container/dataset.parq', filesystem=fs)
Example usage with arrow tables:
import azure.identity
import pyarrow.dataset
import pyarrow.fs
import pyarrowfs_adlgen2
handler = pyarrowfs_adlgen2.AccountHandler.from_account_name(
'YOUR_ACCOUNT_NAME', azure.identity.DefaultAzureCredential())
fs = pyarrow.fs.PyFileSystem(handler)
ds = pyarrow.dataset.dataset('container/dataset.parq', filesystem=fs)
table = ds.to_table()
Configuring timeouts
Timeouts are passed to azure-storage-file-datalake SDK methods. The timeout unit is in seconds.
import azure.identity
import pyarrowfs_adlgen2
handler = pyarrowfs_adlgen2.AccountHandler.from_account_name(
'YOUR_ACCOUNT_NAME',
azure.identity.DefaultAzureCredential(),
timeouts=pyarrowfs_adlgen2.Timeouts(file_system_timeout=10)
)
# or mutate it:
handler.timeouts.file_client_timeout = 20
Writing datasets
With pyarrow version 3 or greater, you can write datasets from arrow tables:
import pyarrow as pa
import pyarrow.dataset
pyarrow.dataset.write_dataset(
table,
'name.pq',
format='parquet',
partitioning=pyarrow.dataset.partitioning(
schema=pyarrow.schema([('year', pa.int32())]), flavor='hive'
),
filesystem=pyarrow.fs.PyFileSystem(handler)
)