How to Fetch API Data and Implement Incremental Loading in PySpark with Delta Lake | Databricks

Опубликовано: 30 Май 2026
на канале: Data Engineering Toolbox
2,115
35

In this tutorial, I'll guide you step-by-step on how to fetch data from a free API and implement incremental loading using PySpark in Databricks with Delta Lake. We'll use the JSONPlaceholder API as an example and demonstrate how to ensure that only new data is appended to the Delta Lake table. This method helps in avoiding redundant records and improves data processing efficiency.

This video covers:

Fetching data from a REST API using Python's requests library.
Loading the data into a Spark DataFrame.
Implementing incremental data loading based on maximum record IDs.
Storing and querying data in Delta Lake using Databricks.

Step 1-2: We start by importing the necessary libraries and initializing a SparkSession if not already running in Databricks. We use the requests library to fetch data from a free API (https://jsonplaceholder.typicode.com/....

Step 3-4: The data from the API is converted into a Spark DataFrame by reading the JSON response. This makes the data suitable for transformations and processing in PySpark.

Step 5-6: We define the Delta Lake table path and check if the Delta table already exists. If the table exists, we proceed with the incremental loading process by identifying the maximum ID in the Delta table and loading only the new records.

Step 7: This is the core of the incremental loading process. We filter the DataFrame for records that have an ID greater than the current maximum ID in the Delta Lake table. If new data is found, it is appended to the table. If no new records exist, the code simply reports that no records were appended.

Step 8: If the Delta table does not exist, we initialize it by saving the entire DataFrame. This sets up the foundation for future incremental loads.

Step 9: Finally, we use Spark SQL to query and verify the data stored in the Delta Lake table, showing only specific columns of interest (like id, address, username, company, and phone).