Sitemap

Member-only story

Databricks Magic Commands

2 min readSep 23, 2024

--

Databricks magic commands are special commands available within Databricks notebooks to simplify tasks like configuring environments, interacting with databases, and managing data.

%python

You can use this to switch the notebook’s execution language to Python (if it’s not the default).

%python
print("Hello from Python") #Hello from Python

%sql

This allows you to run SQL queries within the notebook.

%sql
SELECT * FROM table_name LIMIT 10

%scala

You can use this to write Scala code in the notebook.

%scala
val x = 10
println(x)
#10
# x: Int = 10

%r

For R users, this allows executing R code in the notebook.

%r
x <- c(1, 2, 3, 4)
mean(x)
# [1] 2.5

%md

This is used to render Markdown cells.

%md
# This is a heading
Data is everything

%run

Allows you to run another notebook inside the current notebook.

%run ./Demo_Folder_2/Test

%fs

Used for interacting with the Databricks File System (DBFS).

List files in a directory:

%fs ls '/databricks-datasets'

--

--