Featured image of post Databricks in a Nutshell

Databricks in a Nutshell

Intro to Databricks with code examples.

Introduction

Databricks is a cloud-based data analytics platform built on Apache Spark, designed to simplify big data processing, machine learning, and real-time analytics.

It provides an interactive workspace that allows data engineers, analysts, and scientists to collaborate efficiently.

In this blog post, I’ll go over what Databricks is, how to set up your first workspace, and explore basic code examples to get you started.


1. What is Databricks?

Databricks is an enterprise-level cloud platform that integrates Apache Spark with cloud storage solutions like AWS S3, Azure Blob, and Google Cloud Storage. It provides:

  • Scalability – Run large-scale analytics on distributed data.
  • Ease of Use – Web-based notebooks with a collaborative environment.
  • Optimized Performance – Managed clusters and auto-scaling.
  • Built-in Security – Enterprise-grade security and compliance features.

Databricks is used for ETL (Extract, Transform, Load) pipelines, real-time analytics, AI/ML model training, and more.

IMPORTANT There is no On-Prem version of Databricks. That may be a deal killer for you.. (it actually was for me- but it didnt stop me from playing with it)


2. Setting Up Databricks

Step 1: Create a Databricks Account

  1. Sign up at Databricks.
  2. Choose your preferred cloud provider (AWS, Azure, or GCP).
  3. Set up your workspace by following the guided instructions.

Step 2: Launching a Cluster

  1. Go to the Databricks workspace.
  2. Navigate to Compute → Click Create Cluster.
  3. Choose a cluster name, select a runtime version, and configure autoscaling options.
  4. Click Create Cluster and wait for it to start.

3. Basic Databricks Code Examples

Running a Simple Spark Job

Databricks supports Python (PySpark), Scala, SQL, and R. Below is an example of running a Spark job using PySpark.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from pyspark.sql import SparkSession

# Create a Spark session
spark = SparkSession.builder.appName("DatabricksExample").getOrCreate()

# Create a simple DataFrame
data = [(1, "Alice"), (2, "Bob"), (3, "Charlie")]
df = spark.createDataFrame(data, ["ID", "Name"])

# Show the DataFrame
df.show()

Expected Output:

1
2
3
4
5
6
7
+---+-------+
| ID|  Name |
+---+-------+
|  1| Alice |
|  2|   Bob |
|  3|Charlie|
+---+-------+

Reading and Writing Data

You can read and write CSV, Parquet, JSON, and Delta Lake files effortlessly.

Reading a CSV File

1
2
df = spark.read.csv("/mnt/data/sample.csv", header=True, inferSchema=True)
df.show()

Writing Data to Parquet

1
df.write.mode("overwrite").parquet("/mnt/data/output.parquet")

Using SQL in Databricks

You can run SQL queries directly in Databricks notebooks.

1
SELECT * FROM my_table WHERE age > 30;

Or in PySpark:

1
spark.sql("SELECT * FROM my_table WHERE age > 30").show()

Machine Learning with Databricks

Databricks integrates with MLflow for tracking experiments and model management.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from pyspark.ml.regression import LinearRegression
from pyspark.ml.feature import VectorAssembler

# Load sample dataset
data = spark.read.csv("/mnt/data/housing.csv", header=True, inferSchema=True)
assembler = VectorAssembler(inputCols=["sqft", "bedrooms"], outputCol="features")
data = assembler.transform(data)

# Train a simple Linear Regression model
lr = LinearRegression(featuresCol="features", labelCol="price")
model = lr.fit(data)
print("Model Coefficients:", model.coefficients)

4. Monitoring & Optimizing Performance

Databricks provides a built-in performance monitoring UI:

  • Go to Clusters → Select your cluster → Click Spark UI.
  • View details about executors, jobs, and stages.
  • Use Auto-Scaling to dynamically allocate resources.

For further optimization:

  • Use Delta Lake for faster queries.
  • Cache frequently used DataFrames using .cache().
  • Optimize queries with .repartition() to control parallelism.

5. Conclusion

Databricks simplifies big data processing and enables AI/ML at scale. Whether you’re working with structured or unstructured data, Databricks provides a powerful, scalable, and collaborative environment.

Key Takeaways:

  • Databricks is a cloud-based platform for Apache Spark.
  • It supports Python, Scala, SQL, and R.
  • You can run ETL, data analytics, and ML workloads.
  • Monitoring and performance tuning are built-in.

Start experimenting today and unlock the power of Databricks for big data processing! 🚀


Key Ideas Table

ConceptSummary
What is Databricks?A cloud-based analytics and ML platform.
Running Spark JobsSupports PySpark, Scala, SQL, and R.
Data OperationsRead/write CSV, JSON, Parquet, and Delta.
Machine LearningIntegrated MLflow for model tracking.
Performance TuningAuto-scaling, caching, and optimizations.