Azure Databricks repository is a set of blogposts as a Advent of 2020 present to readers for easier onboarding to Azure Databricks!
Series of Azure Databricks posts:
- Dec 01: What is Azure Databricks
- Dec 02: How to get started with Azure Databricks
- Dec 03: Getting to know the workspace and Azure Databricks platform
- Dec 04: Creating your first Azure Databricks cluster
- Dec 05: Understanding Azure Databricks cluster architecture, workers, drivers and jobs
- Dec 06: Importing and storing data to Azure Databricks
- Dec 07: Starting with Databricks notebooks and loading data to DBFS
- Dec 08: Using Databricks CLI and DBFS CLI for file upload
- Dec 09: Connect to Azure Blob storage using Notebooks in Azure Databricks
- Dec 10: Using Azure Databricks Notebooks with SQL for Data engineering tasks
- Dec 11: Using Azure Databricks Notebooks with R Language for data analytics
- Dec 12: Using Azure Databricks Notebooks with Python Language for data analytics
- Dec 13: Using Python Databricks Koalas with Azure Databricks
- Dec 14: From configuration to execution of Databricks jobs
- Dec 15: Databricks Spark UI, Event Logs, Driver logs and Metrics
- Dec 16: Databricks experiments, models and MLFlow
- Dec 17: End-to-End Machine learning project in Azure Databricks
- Dec 18: Using Azure Data Factory with Azure Databricks
- Dec 19: Using Azure Data Factory with Azure Databricks for merging CSV files
- Dec 20: Orchestrating multiple notebooks with Azure Databricks
- Dec 21: Using Scala with Spark Core API in Azure Databricks
- Dec 22: Using Spark SQL and DataFrames in Azure Databricks
- Dec 23: Using Spark Streaming in Azure Databricks
- Dec 24: Using Spark MLlib for Machine Learning in Azure Databricks
- Dec 25: Using Spark GraphFrames in Azure Databricks
- Dec 26: Connecting Azure Machine Learning Services Workspace and Azure Databricks
Yesterday we connected the Azure Machine Learning services and Azure Databricks workspace for tracking and monitoring experiments and models in Azure Machine Learning.
Today we will connect on premise development environment (R or Python) with resources in Azure Databricks. With other words, we will have running code on a client / on-prem machine and pushing all the workload to the cloud.
Launch your Azure Databricks workspace from Azure portal. Once the Azure Databricks is launched, head to Clusters.
Start the cluster you will be using to connect to R Studio. Go to "Apps" tab under clusters:
Before you click the "Set up RStudio" button, you will need to disable the auto termination option. By default is enabled, making a cluster terminate itself after period of time of inactivity. Under configuration, select Edit and disable the termination. Cluster will restart. And then click the set up Rstudio. Beware, you should not stop the cluster yourself after finishing work (!).
Click Set up RStudio and you will get the following credentials:
And click the "Open RStudio" and you will get redirected to web portal with RStudio opening.
In order to get the Databricks cluster objects into R Studio, you must also run the spark_connect:
SparkR::sparkR.session() library(sparklyr) sc <- spark_connect(method = "databricks")
And you will see all the DataFrames or CSV Files from previous days:
Please note, you can also connect to RStudio desktop version (!). If so, you would, there are the following steps:
Open your RStudio Desktop and install:
install.packages("devtools") devtools::install_github("sparklyr/sparklyr")
Install Databricks-connect in CLI (it is a 250Mb Package):
pip uninstall pyspark pip install -U databricks-connect
Now set the connections to Azure Databricks:
databricks-connect get-jar-dir
And after that run the command in CLI:
databricks-connect configure
CLI will look like a text input:
And all the information you need to fill in the the CLI can find in URL:
Databrick host: adb-860xxxxxxxxxx95.15.azuredatabricks.net
Cluster Name: 1128-xxxx-yaws18/
Organization: 860xxxxxxxxxx95
Port: 15001
Token: /////
2.Connecting with Python
Go to your Cluster in Azure Databricks and straight to configure.
Cluster will need to have these two items added in the Advanced Options -> Spark Config section. Set these values to a cluster that you want to connect to from on-premise or client machine.
spark.databricks.service.server.enabled true spark.databricks.service.port 8787
It is a key-pair value so there must be a space between the key and it's value. Once you save this, the cluster will need to restart (click "Confirm & Restart)"!
You can create a virtual environment; it is up to you. I wil create a new environment for Databricks connections and python. In your CLI run the following command:
conda create --name databricksconnect python=3.7
And activate the environment:
conda activate databricksconnect
If you are using an existing Python environment, I strongly suggest you to first uninstall PySpark. This is due the fact that databricks-connect package will have it's own version of PySpark (same as with R).
Now install the Databricks-Connect package. You can alos specify the version of databricks-connect by adding =5.2 to have like pip install -U databricks-connect=5.2. This depends on your Datatricks cluster version
#pip uninstall pyspark pip install -U databricks-connect
After the installation, you will need to run the configurations again:
databricks-connect configure
Adding all the needed settings as explained in R section. And test your connection by using:
databricks-connect test
Once you have all the settings enabled you are ready to use Databricks on your on-premis / client machine.
You have many ways to use it:
- Anaconda notebooks / Jupyter notebooks
- PyCharm
- Atom
- Visual Studio Code
- etc.
In Visual Studio Code (lightweight, open-source, multi-platform) you can set up this connection. Make sure you have a Python extension installed.
and run the following script:
from pyspark.sql import SparkSession from pyspark.sql.functions import lit, col spark = SparkSession.builder.getOrCreate()
# Import data = spark.read.format("csv").option("header", "true").load("/databricks-datasets/samples/population-vs-price/data_geo.csv") #Display display(data)
And once you run this, you should have the results.
Make sure you also choose the correct Python interpreter in Visual Studio Code.
You can also connect Azure Databricks SQL tables using ODBC to your on-premise Excel or to Python or to R.
It will only see the SQL tables and connections. but it can also be done. This will require some ODBC installation, but I will not go into it.
Tomorrow we will look into Infrastructure as Code and how to automate, script and deploy Azure Databricks.
Complete set of code and the Notebook is available at the Github repository.
Happy Coding and Stay Healthy!