Azure Blob Storage with Pyspark

Image for post
Image for post

Azure Blob Storage is a service for storing large amounts of data stored in any format or binary data. This is a good service for creating data warehouses or data lakes around it to store preprocessed or raw data for future analytics.

In this post, I’ll explain how to access Azure Blob Storage using Spark framework on Python.

Azure blob requires to install additional libraries for accessing data from it because it uses wasb/wasbs protocol, not de facto standard hdfs protocol. Wasbs protocol is just an extension built on top of the HDFS APIs. In order to access resources from azure blob you need to add built jar files, named hadoop-azure.jar and azure-storage.jar to spark-submit when you submitting a job.

Also if you’re using Docker or deploying the application to a cluster, there is a tip for you as well. You don’t need to pass additional jars if they are already in the right place — where pyspark can find it(it need to be done on every cluster node of course). Use the commands below with caution — the versions/links may be wrong!

On the application level, first of all as always in spark applications, you need to grab a Spark Session. Spark Session is the entry point for the cluster resources — for reading data and execute SQL queries over data and getting the results.

Then set up an account key to your blob container:

or SAS token:

Once an account access key or SAS token is set up you’re ready to read/write to Azure blob:

Thank you for reading!

Any questions? Leave your comment below to start fantastic discussions!

Check out my blog or come to say hi 👋 on Twitter or subscribe to my telegram channel.
Plan your best!

Written by

helping robots conquer the earth and trying not to increase entropy using Python, Big Data, Machine Learning. Check out my blog —

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store