Step-by-Step Guide: Copy Data from Azure Blob Storage to SQL Database using Azure Data Factory
Azure provides a robust set of services for building end-to-end data pipelines in the cloud. Three key components are Azure Blob Storage for raw data storage, Azure SQL Database for structured data storage, and Azure Data Factory for orchestrating data movement and transformation.
In this post, we‘ll walk through how to use these services together to build an automated pipeline that copies data from Blob Storage to SQL Database. Whether you‘re just learning Azure or looking to optimize your data flows, this guide will teach you the fundamentals and best practices. Let‘s get started!
Overview of Azure Storage and Data Services
Before we dive into the step-by-step, let‘s review the core Azure services involved and their roles:
Azure Blob Storage is a massively scalable object store for unstructured data like logs, images, and documents. It‘s commonly used as a "data lake" to land raw data that will later be processed and loaded into other data stores. Blob Storage is cheap, durable, and highly available, making it ideal for big data workloads.
Azure SQL Database is a fully managed relational database-as-a-service based on the latest stable version of Microsoft SQL Server Database Engine. It offers high performance, easy scalability, automatic backups and patching, and advanced security and compliance features. SQL DB is often used as the structured "data warehouse" in an analytics pipeline.
Azure Data Factory is a serverless data integration service for creating, scheduling, and orchestrating ETL/ELT workflows in the cloud. It provides a code-free UI for building data pipelines between 90+ data sources and destinations, including on-premises and SaaS apps. ADF takes care of infrastructure management, scaling, and monitoring.
Now that you have the basics down, we‘re ready to build our pipeline!
Step 1: Set Up Azure Blob Storage
First we need to set up our data lake with Azure Blob Storage. Here‘s how:
-
In the Azure portal, click "Create a resource" and search for "storage account". Fill in the required fields like subscription, resource group, location, and account name. For account kind, choose "StorageV2". For replication, choose "Locally-redundant storage (LRS)" to optimize for cost.
-
Once your storage account is deployed, navigate to it in the portal. Go to the "Containers" tab and click "+ Container" to create a new container to house your data. Give it a name, choose the Public access level, and hit "Create".
-
Now click into your new container. Click "Upload" to upload files from your local machine, or use one of the tools like Azure Storage Explorer, AzCopy, or PowerShell to mass upload data.
Tip: Organize your blobs into a clean folder hierarchy for easier management, and consider using a naming convention for your containers like "raw", "processed", "tmp" to denote different stages of data.
Step 2: Provision Azure SQL Database
Next we set up our data warehouse target with Azure SQL Database:
-
In the Azure portal, click "Create a resource" and search for "sql database". Select your subscription, resource group and give your DB a name.
-
Choose your desired Server. You can reuse an existing server or create a new one. If creating new, set an admin login and password. Choose your location close to your other resources.
-
Under "Compute + storage", select your service tier and size. You can start small and scale up later if needed. Generally the cheapest option is a Serverless tier and the minimum Gen 5 compute hardware.
-
Finally, choose a backup redundancy (LRS suffices for non-prod), and click "Review + create".
-
Once deployed, open your new DB and go to the Query editor. From here you can create your destination tables using SQL commands. For example:
CREATE TABLE MyTable (
ID int IDENTITY(1,1) PRIMARY KEY,
FirstName varchar(50),
LastName varchar(50),
Salary int
)
Tip: Leverage features like auto-scale, performance recommendations, geo-replication, and advanced data security to optimize your database, and watch your costs carefully with the Azure Cost Management tool.
Step 3: Create Azure Data Factory
Finally, we provision our orchestrator Azure Data Factory:
-
In Azure portal, click "Create a resource" and search for "data factory". Select your subscription, resource group and region. Give your factory a globally unique name.
-
Choose your Git configuration and decide whether to configure a repo now or later. Having your pipelines version controlled in Git is strongly recommended.
-
Select your networking preferences and hit "Create". Once deployed, open your factory and click "Author & Monitor" to open the ADF UI.
-
First create Linked Services for your Blob Storage account and SQL DB. These store the connection info for ADF to access your data stores.
- For Blob, choose "Azure Blob Storage", select your account, and input the container name.
- For SQL DB, choose "Azure SQL Database", select your server/DB, and input auth details.
- Now create Datasets referencing your specific source file(s) and destination table(s). These define the schema and format of your data.
- For Blob, choose "Azure Blob Storage" and "DelimitedText", and specify your container/folder/file names.
- For SQL, choose "Azure SQL Database" and select your target table.
Tip: Parameterize your linked services and dataset properties to make them reusable across environments. And leverage the Integration Runtime auto-resolve for accessing data within your VNet.
Step 4: Create and Run Your Copy Pipeline
Now for the fun part – building the pipeline:
-
In the ADF UI, go to the "Author" tab and click the plus icon to create a new Pipeline. Give it a name like "BlobToSqlPipeline".
-
From the Activities sidebar, drag a "Copy data" activity onto the canvas. In its Source tab, select your Blob dataset. You can optionally click "Preview data" to verify your file contents.
-
In the Sink tab, select your SQL dataset. Optionally, check "Enable staging" to use Polybase for faster loading into SQL. Map your source to destination columns or choose "Auto mapping" if the names match.
-
Back in the pipeline canvas, click "Debug" to do a test run and monitor it to completion. Once it succeeds, click "Publish all" to save your work.
-
To operationalize your pipeline, associate it to a trigger. Click "Add trigger" and choose a schedule or event to kick off your pipeline. For example, run it every night at 1am when source data arrives.
-
Congrats, your pipeline is now live! You can monitor its runs in the "Monitor" tab and set up alerts for failures. Your data will automatically flow from Blob to SQL on your defined cadence.
Designing Data Pipelines in Azure – Best Practices
While you now know how to build a basic Blob-to-SQL pipeline, here are some tips to optimize it:
-
Leverage ADF‘s built-in connectors for data stores and services across Azure, AWS, GCP, on-prem and SaaS. No need to write custom code!
-
Build your copy activities in "Dataflow" mode for code-free transformations. You can filter rows, join tables, add columns, and more.
-
For more advanced transformations, add other activities like Notebook (Databricks), HDInsight (Hadoop/Spark), Stored Procedure, and more.
-
Manage data consistency with transaction support and idempotent writes. Make your pipelines rerunnable without duplicate data.
-
Secure your data and activities with column and row level security, data masking, MSI auth, and more.
-
Capture lineage and data quality metrics to maintain trust and compliance in your data.
-
For unstructured data, consider options like Cognitive Search and Form Recognizer to extract insights from images, videos and documents.
Troubleshooting Common Issues
Even with the best design, issues will inevitably arise. Here are some common ones and how to fix them:
-
Linked service connection fails: Double check your endpoint URLs, firewall rules, and credentials. Try toggling "Interactive authoring" in the ADF UI to test.
-
Copy activity is stuck in "Queued": Ensure you have available Concurrent jobs in ADF and DTUs in SQL DB. Scale up if needed.
-
Copy throughput is low: For Blob, ensure your files are large enough for parallelism. For SQL, try using Polybase and/or increase your DWU allocation.
-
Data is incomplete or inaccurate: Check your source and sink field mappings and data types. Filter out bad records before loading.
-
Pipeline is timing out: Reduce your retry interval and timeout settings in the pipeline parameters. Break up large pipelines into smaller chunks.
Hopefully with these tips you can keep your pipelines humming along! When in doubt, leverage Azure documentation, community forums, and Microsoft support resources to unblock yourself.
Next Steps
Congrats on mastering the basics of Blob-to-SQL pipelines in ADF! But this is just the tip of the iceberg. Here are some next steps to level up:
-
Explore other data sources and sinks supported by ADF like CosmosDB, Data Lake Gen2, Snowflake, and more. Mix and match to build your ideal data platform.
-
Graduate to more complex control flows with activities like ForEach, If Condition, Wait, Web, and more. Parameterize your datasets and pipelines to make them dynamic.
-
Build your own custom activities using the ADF extensibility SDK. Reuse your existing scripts and leverage open source.
-
Integrate other Azure services like Databricks, Machine Learning, Stream Analytics, and more to enrich your data pipelines with advanced analytics.
-
Implement DevOps best practices like Git integration, CI/CD, and automated testing. Manage your ADF assets like code.
The journey to data engineering mastery never ends, but you‘re well on your way! For further learning, check out these resources:
What data challenge will you solve with ADF? Go build something great!