This program creates a Delta Lake table for employees, inserts two rows, then safely updates Alice's salary by 500 using ACID transactions. Finally, it shows the updated table.
Apache Spark
from pyspark.sql import SparkSession
from delta.tables import DeltaTable
spark = SparkSession.builder.appName('ACIDExample')\
.config('spark.sql.extensions', 'io.delta.sql.DeltaSparkSessionExtension')\
.config('spark.sql.catalog.spark_catalog', 'org.apache.spark.sql.delta.catalog.DeltaCatalog')\
.getOrCreate()
# Create a Delta table
spark.sql("CREATE TABLE IF NOT EXISTS employees (id INT, name STRING, salary INT) USING delta LOCATION '/tmp/delta/employees'")
# Insert initial data
spark.sql("INSERT INTO employees VALUES (1, 'Alice', 5000), (2, 'Bob', 4000)")
# Start a transaction by updating salary
employees = DeltaTable.forPath(spark, '/tmp/delta/employees')
employees.update("name = 'Alice'", {"salary": "salary + 500"})
# Read data to see the update
df = spark.sql("SELECT * FROM employees ORDER BY id")
df.show()
OutputSuccess
Important Notes
Delta Lake automatically handles ACID transactions; you don't manually write BEGIN or COMMIT commands.
Using Delta Lake ensures your data lake behaves more like a database with safe updates.
Make sure to configure Spark with Delta Lake extensions to use these features.
Summary
ACID transactions keep data safe and consistent in data lakes.
Delta Lake adds ACID support to Apache Spark data lakes.
You can update and read data safely even with many users working at once.
Practice
(1/5)
1. What does ACID stand for in the context of data lakes?
easy
A. Aggregation, Calculation, Insertion, Deletion
B. Accuracy, Clarity, Integrity, Data
C. Access, Control, Indexing, Distribution
D. Atomicity, Consistency, Isolation, Durability
Solution
Step 1: Understand ACID properties
ACID is a set of properties that guarantee reliable processing of database transactions.
Step 2: Match each letter to its meaning
Atomicity means all or nothing, Consistency means data stays correct, Isolation means transactions don't interfere, Durability means changes are permanent.
Final Answer:
Atomicity, Consistency, Isolation, Durability -> Option D