Serverless data processing is a computing model that allows us to execute the code and process data without managing the underlying infrastructure. It’s usually used for data processing tasks like ETL pipelines, data analytics, or batch processing.
Here are some points that describe the working of serverless data processing:
import jsondef process_data(event, context):# Data retrieve by using eventsdata = json.loads(event['body'])# Data processing taskprocessed_data = process(data)# Return responseresponse = {'statusCode': 200,'body': json.dumps(processed_data)}return responsedef process(data):# Converting each value to uppercaseprocessed_data = {}for key, value in data.items():processed_data[key] = value.upper()return processed_datadef main():# Eventsevent = {'body': '{"1": "abc", "2": "bdc", "3": "xyz", "4": "mno", "5": "educative" }' # JSON data to process}# Set contextcontext = None# Call the functionresult = process_data(event, context)# Print resultprint(result['body'])# Main functionif __name__ == '__main__':main()
Line 1: We import the json
library.
Lines 3 - 5: The process_data
function retrieves the data from the event by using json.loads()
to analyze the JSON data.
Line 8: We call the process
function and give the data
in which values are stored and created from the above event.
Lines 11 - 15: We generate the response to return.
Lines 17- 22: We define the process
function which converts the data from lower to upper case.
Lines 24 - 40: We define the main function to run the above code.
Free Resources