Splitting an Event into Multiple Event Types

Last updated on May 30, 2023
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

Note: You cannot set the primary key while creating an Event Type through Transformations. However, you can use the Schema Mapper page of your Pipeline and set the primary key for that Event Type.

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
Apr-10-2023 NA Updated the page to add a note regarding setting the primary key through the Schema Mapper page.
Oct-25-2022 NA New document.

Tell us what went wrong