Loading...

Mount ADLS Gen2 or Blob Storage in Azure Databricks

Mount ADLS Gen2 or Blob Storage in Azure Databricks

Scenario:
Azure Databricks offers many of the same features as the open-source Databricks platform, such as a web-based workspace for managing Spark clusters, notebooks, and data pipelines, along with Spark-based analytics and machine learning tools. It is fully integrated with Azure cloud services, providing native access to Azure Blob Storage, Azure Data Lake Storage, Azure SQL Database, and other Azure services. This blog shows example of mounting Azure Blob Storage or Azure Data Lake Storage in the Databricks File System (DBFS), with two authentication methods for mount: Access Key and SAS token.

 

Objective:

To become acquainted with Databricks storage mount with ABFS/WASB driver and various authentication methods.

Pre-requisites:

For this example, you would need:

  1. An Azure Databricks Service.
  2. A Databricks Cluster (compute).
  3. A Databricks Notebook.
  4. An Azure Data Lake Storage or Blob Storage.

 

Steps to mount storage container on Databricks File System (DBFS):

  1. Create storage container and blobs.
  2. Mount with dbutils.fs.mount().
  3. Verify mount point with dbutils.fs.mounts().
  4. List the contents with dbutils.fs.ls().
  5. Unmount with dbutils.fs.unmount().

 

[STEP 1]: Create storage container and blobs

Below is the storage structure used in this example. I have created a container “aaa”, a virtual folder “bbb”, in which has 5 PNG files. The storage “charlesdatabricksadlsno” is a blob storage with no hierarchical namespace.

 

databricks-test1.png

databricks-test.png

 

 

[STEP 2]: Mount with dbutils.fs.mount()

We can use below code snippet to mount container "aaa" with Azure Databricks.

 

 

 

storageAccountName = "charlesdatabricksadlsno" storageAccountAccessKey = <access-key> sasToken = <sas-token> blobContainerName = "aaa" mountPoint = "/mnt/data/" if not any(mount.mountPoint == mountPoint for mount in dbutils.fs.mounts()): try: dbutils.fs.mount( source = "wasbs://{}@{}.blob.core.windows.net".format(blobContainerName, storageAccountName), mount_point = mountPoint, #extra_configs = {'fs.azure.account.key.' + storageAccountName + '.blob.core.windows.net': storageAccountAccessKey} extra_configs = {'fs.azure.sas.' + blobContainerName + '.' + storageAccountName + '.blob.core.windows.net': sasToken} ) print("mount succeeded!") except Exception as e: print("mount exception", e)

 

 

 

Some keypoints to note:

  1. I provide two authentication methods for mount: Access Key and SAS token. You may use either (by choosing the 1st or 2nd line that starts with "extra_configs". Instructions for getting the Access Key and SAS token are in the next section.
  2. This mount example does not re-mount an existing mount point. To re-mount, you have to unmount (mentioned in later section) and then mount again.

To get the Access Key, you would go to Azure portal/Access Keys and copy either key1 or key2.

 

charleswang_4-1682178395217.png

To get a SAS token, you can generate in two ways:

  • Generate an account level SAS with all Allowed Resource Types enabled.

charleswang_0-1685953446828.png

 

  • Generate a container level SAS with read and list permissions. For this example, I generate a SAS for container “aaa” which I would later mount on the Databricks cluster.

charleswang_0-1685953796024.png

 

 [STEP 3]: Verify mount point (/mnt/data) with dbutils.fs.mounts()

 

 

 

dbutils.fs.mounts()

 

 

charleswang_5-1682179093078.png

 

[STEP 4]: List the contents with dbutils.fs.ls()

 

 

 

dbutils.fs.ls("/mnt/data/bbb")

 

 

charleswang_0-1682180048076.png

 

[STEP 5]: Unmount with dbutils.fs.unmount()

 

 

 

dbutils.fs.unmount('/mnt/data')

 

 

charleswang_2-1682180294442.png

 

Others:

  • To use ADLS Gen2 storage as mount source, just replace the storage account name, Access Key, and SAS token in the mount step. You may reuse the BLOB endpoint (blob.core.windows.net).
  • If you want to take advantage of the hierarchical namespace feature of ADLS Gen2, such as ACL on the files and folders, you can switch to use ABFS, which stands for Azure Blob File System, and the DFS endpoint (dfs.core.windows.net), from the previous WASBS (Windows Azure Storage Blob) used with BLOB endpoint. The mount source would become: 
abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/
  • from
wasbs://<container-name>@<storage-account-name>.blob.core.windows.net/
  • To prevent mount point authentication error in case an Access Key or SAS token is rotated, you can modify the mount condition such that if there is an existing mount point, it will first unmount before mounting.

 

 

 

storageAccountName = "charlesdatabricksadlsno" storageAccountAccessKey = <access-key> sasToken = <sas-token> blobContainerName = "aaa" mountPoint = "/mnt/data/" if any(mount.mountPoint == mountPoint for mount in dbutils.fs.mounts()): dbutils.fs.unmount(mountPoint) try: dbutils.fs.mount( source = "wasbs://{}@{}.blob.core.windows.net".format(blobContainerName, storageAccountName), mount_point = mountPoint, #extra_configs = {'fs.azure.account.key.' + storageAccountName + '.blob.core.windows.net': storageAccountAccessKey} extra_configs = {'fs.azure.sas.' + blobContainerName + '.' + storageAccountName + '.blob.core.windows.net': sasToken} ) print("mount succeeded!") except Exception as e: print("mount exception", e)

 

 

 

 

References:

Published on:

Learn more
Need help with this product?

We can help you with Mount ADLS Gen2 or Blob Storage in Azure Databricks

If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.

Azure PaaS Blog articles
Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related posts

Azure Storage - TLS 1.0 and 1.1 retirement

Overview TLS 1.0 and 1.1 retirement on Azure Storage was previously announced for Nov 1st, 2024, and it was postponed recently to 1 year later...

1 year ago

Efficient Management of Append and Page Blobs Using Azure Storage Actions

  Overview In Azure Storage, Blob Lifecycle Management (BLM) allows you to automate the management of your data based on rules defined by...

1 year ago

[Azure AI Search] Internal Server Error when creating CMK encrypted objects

Scenario Customers follow the Microsoft doc to create CMK encrypted objects (data source, index etc.), but get the 500 Internal Serv...

2 years ago

Optimizing Azure Table Storage: Automated Data Cleanup using a PowerShell script with Azure Automate

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

2 years ago

Optimizing Azure Table Storage: Automated Data Clean-up using a PowerShell script with Azure Automat

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

2 years ago

Restoring Soft-Deleted Blobs with multithreading in Azure Storage Using C#

Blob soft delete is an essential feature that safeguards your data against accidental deletions or overwrites. By retaining deleted data for a...

2 years ago

Performing simple Azure Table Storage REST API operations using curl command.

The blog provides guidance to perform simple Table Storage REST API operations such as Create table, Delete Table, Insert entity, Delete entit...

2 years ago

Bulk delete all the old jobs from the batch account

Deleting a Job also deletes all Tasks that are part of that Job, and all Job statistics. This also overrides the retention period for Task dat...

2 years ago

Utilizing Azure Storage and Runbooks for scheduled automated backups of Azure SQL Databases

In this article, we are going to provide detailed steps to create a scheduled Azure SQL Database backup to storage account using automation. T...

2 years ago

[Azure Service Bus] JMS messages getting dead-lettered

The article discusses a problem where numerous messages end up in the dead letter queue (DLQ) when the JMS service bus consumer connects to th...

2 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.