We all have been in situations where we have to read data from API and load the same in Spark Data Frames for further operations.
Following is a small snippet of code which reads data from API and generates a Spark Data Frame.
Lets create a Python function to read API data.
# Create Python function to read data from API import requests, jsondef read_api(url: str): normalized_data = dict() data = requests.get(api_url).json() normalized_data["_data"] = data # Normalize payload to handle array situtations return json.dumps(normalized_data)
Following code generates Spark Data Frame from the json payload of the API response