docs/manual/source/templates/recommendation/reading-custom-events.html.md
You can modify the default DataSource to read
You can find the complete modified source code here.
To read custom events, modify the function call PEventStore.find() in MyRecommendation/src/main/scala/DataSource.scala:
eventNames parametersentityType and targetEntityType parameters accordinglyIn this example below, we modify DataSource to read custom like and dislike events where a customer likes or dislikes a product. The event has new entityType customer and targetEntityType product:
val eventsRDD: RDD[Event] = PEventStore.find(
appName = dsp.appName,
entityType = Some("customer"), // MODIFIED
eventNames = Some(List("like", "dislike")), // MODIFIED
// targetEntityType is optional field of an event.
targetEntityType = Some(Some("product")))(sc) // MODIFIED
The ALS algorithm uses Rating object as input, so it is necessary to specify the mapping of your custom event to the Rating object. You can do so in MyRecommendation/src/main/scala/DataSource.scala.
To map like and dislike event to a Rating object with value of 4 and 1, respectively :
val ratingsRDD: RDD[Rating] = eventsRDD.map { event =>
val rating = try {
val ratingValue: Double = event.event match {
// MODIFIED
case "like" => 4.0 // map a like event to a rating of 4.0
case "dislike" => 1.0 // map a like event to a rating of 1.0
case _ => throw new Exception(s"Unexpected event ${event} is read.")
}
// entityId and targetEntityId is String
Rating(event.entityId,
event.targetEntityId.get,
ratingValue)
} catch {
case e: Exception => {
logger.error(s"Cannot convert ${event} to Rating. Exception: ${e}.")
throw e
}
}
rating
}.cache()
That's it! Your engine can read custom like and dislike event.