Reader small image

You're reading from  Azure Databricks Cookbook

Product typeBook
Published inSep 2021
PublisherPackt
ISBN-139781789809718
Edition1st Edition
Right arrow
Authors (2):
Phani Raj
Phani Raj
author image
Phani Raj

Phani Raj is an experienced data architect and a product manager having 15 years of experience working with customers on building data platforms on both on-prem and on cloud. Worked on designing and implementing large scale big data solutions for customers on different verticals. His passion for continuous learning and adapting to the dynamic nature of technology underscores his role as a trusted advisor in the realm of data architecture ,data science and product management.
Read more about Phani Raj

Vinod Jaiswal
Vinod Jaiswal
author image
Vinod Jaiswal

Vinod Jaiswal is an experienced data engineer, excels in transforming raw data into valuable insights. With over 8 years in Databricks, he designs and implements data pipelines, optimizes workflows, and crafts scalable solutions for intricate data challenges. Collaborating seamlessly with diverse teams, Vinod empowers them with tools and expertise to leverage data effectively. His dedication to staying updated on the latest data engineering trends ensures cutting-edge, robust solutions. Apart from technical prowess, Vinod is a proficient educator. Through presentations and mentoring, he shares his expertise, enabling others to harness the power of data within the Databricks ecosystem.
Read more about Vinod Jaiswal

View More author details
Right arrow

Chapter 8: Databricks SQL

Databricks SQL provides a great experience for SQL developers, BI developers, analysts, and data scientists to run ad hoc queries on large volumes of data in a data lake, creating various visualizations and rich dashboards.

Databricks SQL provides the following features:

  • A fully managed SQL endpoint for running all SQL queries
  • A query editor for writing SQL queries
  • Visualizations and dashboards for providing various insights into the data
  • Integration with Azure Active Directory, providing enterprise-level security for data by controlling the access to tables using role-based access controls
  • Integration with Power BI for creating rich visualizations and sharing meaningful insights from the data in a data lake
  • The ability to create alerts on a field returned by a query on meeting a threshold value and notifying users

By the end of this chapter, you will have learned how you can use Databricks SQL to write ad hoc queries...

Technical requirements

To follow along with the examples shown in the recipes, you will need to have the following:

  • An Azure subscription and the required permissions on the subscription that was mentioned in the Technical requirements section in Chapter 1, Creating an Azure Databricks Service.
  • An Azure Databricks premium workspace with a Spark 3.x cluster.
  • Databricks SQL is in public preview.

    Important Note

    The UI and some features of Databricks SQL may change in the future when it goes to General Availability (GA) as it is still in public preview.

In the next section, you will learn how to create a user in Databricks SQL.

How to create a user in Databricks SQL

In this recipe, you will learn how to create a user for running queries, creating dashboards, or performing data analysis on top of the data available in Delta Lake.

Getting ready

Before starting with this recipe, you need to ensure that you have the resources mentioned in the Technical requirements section of this chapter.

How to do it…

Let's go through the steps for creating a Databricks SQL user:

  1. Open your Databricks workspace with Databricks SQL access:

    Figure 8.1 – Databricks SQL workspace

  2. Click on Admin Console and go to the Users tab to add a user:

    Figure 8.2 – Databricks workspace Admin Console

  3. Click on the Add User button and enter the email ID of the user whom you want to add. You can only add users who belong to the Azure Active Directory tenant of your Azure Databricks workspace. Click on OK to add the user to the Databricks workspace:

    Figure 8.3 – Add a Databricks SQL user...

Creating SQL endpoints

SQL endpoints are computation resources using which you can run SQL queries on data objects in Azure Databricks environments. SQL endpoints are fully managed SQL-optimized compute clusters that autoscale based on user load to support a better experience for user concurrency and performance. They are computing clusters, very similar to clusters that we have already used in the Azure Databricks environment.

Getting ready

Let's go through the permissions required for creating and managing SQL endpoints in the Azure Databricks workspace.

The user must have the Allow cluster creation permission in the Azure Databricks workspace. You can check this permission from Admin Console | the Users tab or the Groups tab depending on whether access needs to be granted to an individual user or group. Refer to the following screenshot from the workspace:

Figure 8.5 – Databricks SQL cluster creation

After permission is granted...

Granting access to objects to the user

In this recipe, you will learn how to grant access to objects to users so that they can write queries for analyzing data. We have already seen how users are created in the How to create a user in Databricks SQL recipe of this chapter.

Getting ready

Before you start on this recipe, make sure you have gone through the How to create a user in Databricks SQL recipe in this chapter and that all the resources mentioned in the Technical requirements section are ready for usage.

Also, run the following notebook to create Customer and Orders external Delta tables:

https://github.com/PacktPublishing/Azure-Databricks-Cookbook/blob/main/Chapter06/6_1.Reading%20Writing%20to%20Delta%20Tables.ipynb.

How to do it…

Let's run through the following commands to grant access to a user:

  1. Execute the following command to grant access to a user or principal to the default database:
    GRANT USAGE ON DATABASE default TO `user@xyz.com...

Running SQL queries in Databricks SQL

SQL queries in Databricks SQL allows BI users or data analysts to create and run ad hoc SQL queries on data in a data lake and schedule the queries to run at regular intervals. BI users or analysts can create reports based on business requirements and it's easy for traditional BI users to be onboarded to Databricks SQL to write SQL queries and get a similar experience they are used to in on-prem databases.

Getting ready

Before starting, execute the following notebooks:

You need to create the SQL endpoints...

Using query parameters and filters

Query parameters and filters are ways to filter the data that is returned to the end user. A query parameter will substitute the values in a query at runtime before getting executed, whereas a query filter will limit the data after it has been loaded into the browser. Query filters should be used only for small datasets and not for large volumes of data since it does not filter the data at runtime. In this recipe, you will learn how to use query filters and parameters in SQL queries.

Getting ready

Before starting, you need to ensure you execute the following notebooks:

Introduction to visualizations in Databricks SQL

In this recipe, we will learn how to create different visualizations in Databricks SQL queries and how to change certain properties of visualizations.

Getting ready

Before starting, you need to ensure you execute the following notebook so that the tables used in the queries are created:

Running the preceding notebooks will create Customer, Orders, and VehicleSensor related Delta tables. Ensure you complete the Running SQL queries in Databricks SQL and Using query parameters and filters recipes as you will be using the queries created in those recipes.

How to do it…

In this section, you will learn how to create visualizations by running through the following...

Creating dashboards in Databricks SQL

Azure Databricks Databricks SQL allows users to create various dashboards based on the queries and visualizations that are already built. This helps businesses to get a visual representation of data for various KPIs.

In this recipe, you will learn how to create visualizations in Databricks SQL and how to pin a visualization from various queries to the dashboard.

Getting ready

Before starting, we need to ensure we have executed the following notebook. The following notebook creates the required tables on which we can build our visualizations and dashboard:

https://github.com/PacktPublishing/Azure-Databricks-Cookbook/blob/main/Chapter07/7.1-End-to-End%20Data%20Pipeline.ipynb

Also create the visualization as mentioned in the Introduction to visualizations in Databricks SQL recipe.

How to do it…

Let's learn how to create a dashboard by running through the following steps:

  1. From the Databricks SQL homepage go...

Connecting Power BI to Databricks SQL

Databricks SQL provides built-in connectors for Power BI users to connect to objects in Databricks SQL. Power BI users can now connect to a SQL endpoint to get a list of all tables and can connect using the import or direct query mode.

In this recipe, you will learn how to connect to a SQL endpoint from Power BI.

Getting ready

Before starting the recipe, ensure you have executed both of the following notebooks. The following notebook creates the required tables on which we can build our visualizations and dashboard. You need to ensure you download the latest version of Power BI Desktop:

You can go through the following link which has the requirements for connecting Power...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Azure Databricks Cookbook
Published in: Sep 2021Publisher: PacktISBN-13: 9781789809718
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Phani Raj

Phani Raj is an experienced data architect and a product manager having 15 years of experience working with customers on building data platforms on both on-prem and on cloud. Worked on designing and implementing large scale big data solutions for customers on different verticals. His passion for continuous learning and adapting to the dynamic nature of technology underscores his role as a trusted advisor in the realm of data architecture ,data science and product management.
Read more about Phani Raj

author image
Vinod Jaiswal

Vinod Jaiswal is an experienced data engineer, excels in transforming raw data into valuable insights. With over 8 years in Databricks, he designs and implements data pipelines, optimizes workflows, and crafts scalable solutions for intricate data challenges. Collaborating seamlessly with diverse teams, Vinod empowers them with tools and expertise to leverage data effectively. His dedication to staying updated on the latest data engineering trends ensures cutting-edge, robust solutions. Apart from technical prowess, Vinod is a proficient educator. Through presentations and mentoring, he shares his expertise, enabling others to harness the power of data within the Databricks ecosystem.
Read more about Vinod Jaiswal