Splitting an Event into Multiple Event Types

On This Page

In this example, we will split an Event received as a single object, from the Source, into multiple Event Types.

Consider the following sample Event received from a REST API Source as a single object called API:

{
	"event_name": "API",
	"properties": {
		"photos_avail": "url",
		"photos_avail_download": "yes",
		"photo_names": "name",
		"low_stock_quantity": "1",
		"discount": "0.00",
		"discount_type": "amount",
		"discount_start_date": "12-21-12",
		"__hevo_id": "id"
	}
}

The following Transformation script:

  • Splits the single Event, API into two Event Types

  • Creates the Event Types:

    • photos_table with the following fields:

      • photos_avail

      • photos_avail_download

      • photo_names

    • discount_table with the following fields:

      • discount

      • discount_type

      • discount_start_date

from io.hevo.api import Event

def transform(event):
    events = []
    eventName = event.getEventName()
    properties = event.getProperties()
    
    req_fields_for_photos = ['photos_avail', 'photos_avail_download','photo_names']
    properties_photos = {} 
    
    req_fields_for_discount = ['discount', 'discount_type','discount_start_date']
    properties_discount = {}
    
    for column in properties.keys():
        if column in req_fields_for_photos:
            properties_photos[column] = properties[column]
            properties.pop(column)
            
        if column in req_fields_for_discount:
            properties_discount[column] = properties[column]
            properties.pop(column)
            
    events.append(Event('photos_table', properties_photos))
    events.append(Event('discount_table', properties_discount))

    return events

The output from the above snippet is:

 [
	{
		"event_name": "photos_table",
		"properties": {
			"photos_avail": "url",
			"photos_avail_download": "yes",
			"photo_names": "name"
		}
	},
	{
		"event_name": "discount_table",
		"properties": {
			"discount": "0.00",
			"discount_type": "amount",
			"discount_start_date": "12-21-12"
		}
	}
]

As you can see above, the Event, API is split into two Event Types, photos_table and discount_table.



Revision History

Refer to the following table for the list of key updates made to this page:

Date Release Description of Change
Oct-25-2022 NA New document.
Last updated on 21 Oct 2022

Tell us what went wrong