At Netflix's algorithm team, we work on ranking problems to find the great content to fulfill the unique tastes of our members. Before building a recommendation algorithms, we need to prepare the training, testing, and validation datasets in Apache Spark. Due to the nature of ranking problems, we have a nested list of items to be ranked in one column, and the top level is the contexts describing the setting for where a model is to be used (e.g. profiles, country, time, device, etc.) Here is a blog post describing the details, Distributed Time Travel for Feature Generation.
To be more concrete, for the ranks of videos for a given profile_id at a given country, our data schema can be looked like this,
We oftentimes need to work on the nested list of structs by applying some functions on them. Sometimes, we're dropping or adding new columns in the nested list of structs. Currently, there is no easy solution in open source Apache Spark to perform those operations using SQL primitives; many people just convert the data into RDD to work on the nested level of data, and then reconstruct the new dataframe as workaround. This is extremely inefficient because all the optimizations like predicate pushdown in SQL can not be performed, we can not leverage on the columnar format, and the serialization and deserialization cost becomes really huge even we just want to add a new column in the nested level.
We built a solution internally at Netflix which we're very happy with. We plan to make it open source in Spark upstream. We would like to socialize the API design to see if we miss any use-case.
The first API we added is mapItems on dataframe which take a function from Column to Column, and then apply the function on nested dataframe. Here is an example,
Now, with the ability of applying a function in the nested dataframe, we can add a new function, withColumn in Column to add or replace the existing column that has the same name in the nested list of struct. Here is two examples demonstrating the API together with mapItems; the first one replaces the existing column,
and the second one adds a new column in the nested dataframe.
We also implement a filter predicate to nested list of struct, and it will return those items which matched the predicate. The following is the API example,
Dropping a column in the nested list of struct can be achieved by similar API to withColumn. We add drop method to Column to implement this. Here is an example,
Note that all of those APIs are implemented by SQL expression with codegen; as a result, those APIs are not opaque to Spark optimizers, and can fully take advantage of columnar data structure.
We're looking forward to the community feedback and suggestion! Thanks.