Checking Spark configurations (like the number of executors and cores) in a Databricks notebook is straightforward using the SparkSession API. Databricks runs on Apache Spark, so you can query runtime configurations programmatically in Python, Scala, or even via SQL. Below, I'll explain the methods step by step, with examples. These work in both Python and Scala notebooks (I'll provide code for both).
Important Notes
- Cluster Configuration: Many Spark configs (e.g., executor count and cores) are set at the cluster level in Databricks (via the cluster UI under "Advanced Options" > "Spark"). You can read them in the notebook, but changing them requires cluster edits or restarts.
- Dynamic Allocation/Autoscaling: If your cluster has autoscaling enabled, the actual number of executors can vary at runtime. The config values might reflect min/max settings, but you'll need runtime queries for the current state.
- Accessing SparkSession: In Databricks notebooks,
spark is pre-initialized as the SparkSession. In Scala, you can also use sc (SparkContext).
- Permissions: You need to be attached to a running cluster to run these commands.
1. Check Specific Spark Configs
Use spark.conf.get("key") to retrieve a specific configuration value.
Python Example
# Number of executors (spark.executor.instances)
num_executors = spark.conf.get("spark.executor.instances", "Not set") # Default if not found
print(f"Number of executors: {num_executors}")
# Cores per executor (spark.executor.cores)
cores_per_executor = spark.conf.get("spark.executor.cores", "Not set")
print(f"Cores per executor: {cores_per_executor}")
# Total cores (calculated: executors * cores per executor)
if num_executors != "Not set" and cores_per_executor != "Not set":
total_cores = int(num_executors) * int(cores_per_executor)
print(f"Total cores: {total_cores}")
Scala Example
// Number of executors (spark.executor.instances)
val numExecutors = spark.conf.get("spark.executor.instances", "Not set")
println(s"Number of executors: $numExecutors")
// Cores per executor (spark.executor.cores)
val coresPerExecutor = spark.conf.get("spark.executor.cores", "Not set")
println(s"Cores per executor: $coresPerExecutor")
// Total cores (calculated: executors * cores per executor)
if (numExecutors != "Not set" && coresPerExecutor != "Not set") {
val totalCores = numExecutors.toInt * coresPerExecutor.toInt
println(s"Total cores: $totalCores")
}
2. Get All Spark Configs
To see everything (useful for debugging), retrieve all configs as a dictionary or list.
Python Example
all_configs = dict(spark.conf.getAll())
print(all_configs) # Prints a dict of all key-value pairs
# Filter for executor-related configs
executor_configs = {k: v for k, v in all_configs.items() if 'executor' in k}
print(executor_configs)
Scala Example
val allConfigs = spark.conf.getAll
allConfigs.foreach { case (k, v) => println(s"$k: $v") }
// Filter for executor-related configs
val executorConfigs = allConfigs.filter(_._1.contains("executor"))
executorConfigs.foreach { case (k, v) => println(s"$k: $v") }
- This will include keys like
spark.executor.memory, spark.dynamicAllocation.enabled, etc.
3. Get Runtime Executor Information (Current Active Executors)
Configs like spark.executor.instances show the configured value, but for the current number of active executors (especially with autoscaling), query the Spark runtime.
Python Example
from pyspark import SparkContext
sc = SparkContext.getOrCreate()
# Get active executors (subtract 1 for the driver)
active_executors = len(sc._jsc.statusTracker().getExecutorInfos()) - 1
print(f"Current active executors: {active_executors}")
# Get total cores across all executors
total_cores = sc._jsc.sc().getExecutorMemoryStatus().size() * int(spark.conf.get("spark.executor.cores", "1")) # Approximate
print(f"Approximate total cores: {total_cores}")
Scala Example
val activeExecutors = sc.statusTracker.getExecutorInfos.length - 1 // Subtract driver
println(s"Current active executors: $activeExecutors")
// Get total cores (approximate)
val coresPerExecutor = spark.conf.get("spark.executor.cores", "1").toInt
val totalCores = activeExecutors * coresPerExecutor
println(s"Approximate total cores: $totalCores")
4. Using SQL or Databricks Utilities
-
SQL Query for Configs: You can query Spark configs via SQL.
-- In a %sql cell
SELECT * FROM spark_config;
This shows a table of all configs (similar to getAll()).
-
Databricks-Specific: Use dbutils to get cluster info (not directly Spark configs, but related).
# Cluster ID and name
print(dbutils.notebook.getContext().clusterId())
print(dbutils.notebook.getContext().clusterName())
For more details, check the Databricks UI (Clusters > Your Cluster > Spark UI tab) for live metrics.
5. Viewing in Spark UI
For a visual check:
- Go to your Databricks workspace > Clusters > Select your cluster > Spark UI.
- Under the "Executors" tab, you'll see live details on executors, cores, memory, etc.
- This is great for monitoring but not programmatic.
Troubleshooting
- Key Not Found: If
spark.conf.get returns nothing, the config might not be set (common in autoscaling). Check the cluster settings or enable dynamic allocation configs.
- Autoscaling Clusters: Configs like
spark.dynamicAllocation.minExecutors and spark.dynamicAllocation.maxExecutors define ranges. Use the runtime query above for current values.
- Errors: Ensure your notebook is attached to a cluster. If using an all-purpose cluster, configs are shared across notebooks.
- Setting Configs: To change them temporarily in the notebook (if allowed), use
spark.conf.set("key", "value"), but cluster-level settings take precedence.
If you provide more details about your cluster setup (e.g., autoscaling enabled?), I can refine this further! For full Spark config docs, see the Apache Spark Configuration page or Databricks docs.