Splitting an Event into Multiple Event Types
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
.