Thursday, April 11, 2024

How to Restore a Delta Table in Databricks

Scenario.


We will make some updates and inserts on a table. After that, we restore it to its original form.



SELECT * FROM airline_passenger_csv
WHERE Age > 80



%sql
UPDATE airline_passenger_csv
SET Class="Business"
WHERE Age > 80


%sql
INSERT INTO airline_passenger_csv
VALUES(1234567,'Female','Loyal Customer',88,"Personal Travel","Eco",300,0,0, "satisfied")



%sql
DELETE FROM airline_passenger_csv
WHERE _c0=7547


So far we have updated the Class to business class. Added a user and deleted id 7547
to restore we will check the version from the DESC HISTORY airline_passenger_csv.

Below are a few examples on how to flashback queries DELTA TABLES


%sql
SELECT* FROM airline_passenger_csv
TIMESTAMP AS OF '2024-04-12T01:06:51.000+00:00'
WHERE AGE > 80

%sql
SELECT * FROM airline_passenger_csv@v2
WHERE Age > 80

Now let's say we need to restore to v2 of the table
we type
%sql
RESTORE airline_passenger_csv TO VERSION AS OF 2