Mohammad Gufran Jahangir June 19, 2025 0

๐Ÿง  How to Run Your First Notebook in Databricks: From Beginner to Advanced

Whether you’re a data analyst, engineer, scientist, or just a curious tech enthusiast, Databricks offers a powerful platform to explore big data and AI effortlessly using notebooks. In this guide, we’ll walk you through the complete journey โ€” from setting up your environment to mastering advanced notebook workflows in Databricks.


๐Ÿ”ฐ Table of Contents

  1. What is a Databricks Notebook?
  2. Step 1: Set Up Your Databricks Workspace
  3. Step 2: Create a New Cluster
  4. Step 3: Create and Open a Notebook
  5. Step 4: Write Your First Code (Beginner)
  6. Step 5: Attach Notebook to Cluster
  7. Step 6: Execute Code Cells
  8. Step 7: Use Magic Commands and Shortcuts
  9. Step 8: Import Data into Your Notebook
  10. Step 9: Visualize Data (Charts/Graphs)
  11. Step 10: Schedule Notebooks with Jobs
  12. Step 11: Version Control with Git
  13. Step 12: Best Practices for Notebook Development
  14. Final Thoughts

๐Ÿ”Ž What is a Databricks Notebook?

A Databricks notebook is a collaborative, interactive environment where you can write code in Python, SQL, R, Scala, or Java, and view results immediately. Itโ€™s like Jupyter Notebook but powered by Apache Spark under the hood โ€” perfect for data analytics and machine learning workflows.


โœ… Step 1: Set Up Your Databricks Workspace

You can get started with either:

Create an Account:

  • Sign up โ†’ Verify Email โ†’ Log in

โš™๏ธ Step 2: Create a New Cluster

A cluster is a collection of computation resources (Spark jobs run here).

How to create:

  • Go to Compute tab
  • Click Create Cluster
  • Choose:
    • Cluster name
    • Runtime version (e.g., 14.3 LTS for latest features)
    • Autoscaling & node type (select small for testing)

๐Ÿ”„ Wait for the cluster to be in Running state before attaching a notebook.


๐Ÿ““ Step 3: Create and Open a Notebook

  • Go to Workspace
  • Click Create โ†’ Notebook
  • Name your notebook (e.g., MyFirstNotebook)
  • Select Default Language: Python / SQL / Scala / R

โœ๏ธ Step 4: Write Your First Code (Beginner)

Try running a simple Python print statement:

print("Hello, Databricks!")

Or a simple SQL query:

%sql
SELECT "Hello from SQL" AS greeting;

๐Ÿ”— Step 5: Attach Notebook to Cluster

  • At the top of the notebook, click Detached โ†’ select your running cluster
  • Now your code will execute on that cluster

โ–ถ๏ธ Step 6: Execute Code Cells

  • Press Shift + Enter or click the Run icon on each cell
  • You can add multiple cells using the + sign
  • Check output immediately below the cell

๐Ÿง™ Step 7: Use Magic Commands and Shortcuts

Databricks supports magic commands to switch between languages or shell access:

CommandPurpose
%pythonRun cell as Python
%sqlRun cell as SQL
%fsFile system commands
%shShell commands
%mdMarkdown rendering

Example:

%fs ls /databricks-datasets

Useful Shortcuts:

  • Esc + A โ†’ Insert cell above
  • Esc + B โ†’ Insert cell below
  • Ctrl + / โ†’ Comment/Uncomment

๐Ÿ“ Step 8: Import Data into Your Notebook

Option 1: Upload file manually

  • Click Data tab โ†’ Add Data โ†’ Upload File
  • Access it using:
df = spark.read.csv("/FileStore/tables/mydata.csv", header=True)

Option 2: Use built-in datasets

df = spark.read.csv("/databricks-datasets/airlines/part-00000", header=True)

๐Ÿ“Š Step 9: Visualize Data (Charts/Graphs)

  • Run a DataFrame cell
  • Click on the “+Visualization” icon
  • Choose from:
    • Bar Chart
    • Line Chart
    • Pie Chart
    • Scatter Plot
    • Map

You can also use:

display(df.groupBy("Year").count())

โฐ Step 10: Schedule Notebooks with Jobs

  1. Go to Workflows โ†’ Jobs
  2. Click Create Job
  3. Select:
    • Notebook path
    • Cluster
    • Schedule (e.g., Daily at 8 AM)
  4. Monitor job history from dashboard

This is great for ETL or automated reports.


๐Ÿงฌ Step 11: Version Control with Git

Integrate your notebook with GitHub or Azure DevOps.

Steps:

  1. Click on Revision History
  2. Link to a Git provider
  3. Clone repo and commit directly from the notebook interface

๐Ÿ’ก Step 12: Best Practices for Notebook Development

PracticeDescription
Use markdown cellsDocument your code clearly
Modularize codeUse functions or %run sub-notebooks
Avoid hardcodingUse widgets or config files
Use checkpointsExport versions or use Git
Cache dataframesFor repeated heavy queries
Clean up resourcesStop unused clusters

๐Ÿงพ Final Thoughts

Databricks notebooks combine the power of big data with the simplicity of notebooks โ€” making them a go-to tool for everyone from analysts to AI engineers. Once you’ve mastered your first notebook, explore Delta Lake, MLflow, and Unity Catalog to expand your skills even further.


๐Ÿ“Œ Bonus: Sample Notebook Flow

StepCodeDescription
Load CSVspark.read.csv()Load data from FileStore
Clean Datadf.dropna()Remove nulls
Transformdf.withColumn()Add calculated columns
Analyzedf.groupBy().agg()Perform aggregation
Visualizedisplay()Generate charts
ScheduleJobsAutomate daily run

๐Ÿš€ Ready to launch your first project?

Try building a notebook that:

  • Loads a dataset
  • Cleans & filters it
  • Displays visualizations
  • Runs on a schedule

Happy Notebooks! ๐Ÿงช


Category: 
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments