Class Rank

  • All Implemented Interfaces:
    LogicalOperator, PipelineOperator<RecordPort>, RecordPipelineOperator

    public class Rank
    extends AbstractExecutableRecordPipeline
    Rank data using the given rank mode. The data is grouped by the given partition field(s) and is sorted within the grouping by the ranking field(s). An example is to rank employees by salary per department. To rank the highest to lowest salary within department: partition by the department and rank by the salary in descending sort order.

    Three different rank modes are supported:

    • STANDARD: also known as competition ranking, items with the same ranking values have the same rank and then a gap is left in the ranking numbers; for example: 1224
    • DENSE: items that compare equal receive the same ranking, items following receive the next ordinal ranking (i.e. ranks are not skipped); for example: 1223
    • ORDINAL: each item receives a distinct ranking, starting at 1 and increasing by one producing essentially a row number within the partition; for example: 1234

    A new output field is created to contain the result of the ranking. The field is named "rank" by default. The name of the rank field can be set using setOutputField(String).

    • Constructor Detail

      • Rank

        public Rank()
        Default constructor. The partitionBy and rankBy properties are required and must be set when using this operator.
    • Method Detail

      • setPartitionBy

        public void setPartitionBy​(List<String> partitionKeys)
        Set the fields used for partitioning the input data.
        Parameters:
        partitionKeys - list of fields to use for partitioning
      • setPartitionBy

        public void setPartitionBy​(String... partitionKeys)
        Set the fields used for partitioning the input data.
        Parameters:
        partitionKeys - list of fields to use for partitioning
      • getPartitionBy

        public List<String> getPartitionBy()
        Get the list of fields used to partition the input data.
        Returns:
        list of fields to use for partitioning
      • setRankBy

        public void setRankBy​(SortKey... rankBy)
        Set the list of fields to use for ranking. The data within each partition is sorted by the specified order. This specifies the set of fields used to calculate the rating within each partitioned group.
        Parameters:
        rankBy - list of fields used to rank the data
      • setRankBy

        public void setRankBy​(String... rankBy)
        Set the list of fields to use for ranking. The data within each partition is sorted by the specified order. This specifies the set of fields used to calculate the rating within each partitioned group. The rank order can be a list of fields with the sort order specified (optional) as 'asc' or 'desc'.
        Parameters:
        rankBy - list of fields used to rank the data along with sort order
      • setRankBy

        public void setRankBy​(List<SortKey> rankBy)
        Set the list of fields to use for ranking. The data within each partition is sorted by the specified order. This specifies the set of fields used to calculate the rating within each partitioned group.
        Parameters:
        rankBy - list of fields used to rank the data
      • getRankBy

        public List<SortKey> getRankBy()
        Get the list of fields used for ranking within each partition.
        Returns:
        list of ranking fields
      • setMode

        public void setMode​(Rank.RankMode mode)
        Set the ranking mode. Ordinal ranking is used by default.
        Parameters:
        mode - rank mode
      • getMode

        public Rank.RankMode getMode()
        Get the mode used for ranking.
        Returns:
        rank mode
      • setOutputField

        public void setOutputField​(String outputField)
        Set the name of the output field that will contain the ranking order for each record.
        Parameters:
        outputField - name of the ranking results field
      • getOutputField

        public String getOutputField()
        Get the name of the output ranking field.
        Returns:
        name of the ranking results field
      • computeMetadata

        protected void computeMetadata​(StreamingMetadataContext ctx)
        Description copied from class: StreamingOperator
        Implementations must adhere to the following contracts

        General

        Regardless of input ports/output port types, all implementations must do the following:

        1. Validation. Validation of configuration should always be performed first.
        2. Declare parallelizability.. Implementations must declare parallelizability by calling StreamingMetadataContext.parallelize(ParallelismStrategy).

        Input record ports

        Implementations with input record ports must declare the following:
        1. Required data ordering:
        2. Implementations that have data ordering requirements must declare them by calling RecordPort#setRequiredDataOrdering, otherwise data may arrive in any order.
        3. Required data distribution (only applies to parallelizable operators):
        4. Implementations that have data distribution requirements must declare them by calling RecordPort#setRequiredDataDistribution, otherwise data will arrive in an unspecified partial distribution.
        Note that if the upstream operator's output distribution/ordering is compatible with those required, we avoid a re-sort/re-distribution which is generally a very large savings from a performance standpoint. In addition, some operators may chose to query the upstream output distribution/ordering by calling RecordPort#getSourceDataDistribution and RecordPort#getSourceDataOrdering. These should be viewed as a hints to help chose a more efficient algorithm. In such cases, though, operators must still declare data ordering and data distribution requirements; otherwise there is no guarantee that data will arrive sorted/distributed as required.

        Output record ports

        Implementations with output record ports must declare the following:
        1. Type: Implementations must declare their output type by calling RecordPort#setType.
        Implementations with output record ports may declare the following:
        1. Output data ordering: Implementations that can make guarantees as to their output ordering may do so by calling RecordPort#setOutputDataOrdering
        2. Output data distribution (only applies to parallelizable operators): Implementations that can make guarantees as to their output distribution may do so by calling RecordPort#setOutputDataDistribution
        Note that both of these properties are optional; if unspecified, performance may suffer since the framework may unnecessarily re-sort/re-distributed the data.

        Input model ports

        In general, there is nothing special to declare for input model ports. Models are implicitly duplicated to all partitions when going from non-parallel to parallel operators. The case of a model going from a parallel to a non-parallel node is a special case of a "model reducer" operator. In the case of a model reducer, the downstream operator, must declare the following:
        1. Merge handler: Model reducers must declare a merge handler by calling AbstractModelPort#setMergeHandler.
        Note that MergeModel is a convenient, re-usable model reducer, parameterized with a merge-handler.

        Output model ports

        SimpleModelPort's have no associated metadata and therefore there is never any output metadata to declare. PMMLPort's, on the other hand, do have associated metadata. For all PMMLPorts, implementations must declare the following:
        1. pmmlModelSpec: Implementations must declare the PMML model spec by calling PMMLPort.setPMMLModelSpec.
        Specified by:
        computeMetadata in class StreamingOperator
        Parameters:
        ctx - the context
      • execute

        protected void execute​(ExecutionContext ctx)
        Description copied from class: ExecutableOperator
        Executes the operator. Implementations should adhere to the following contracts:
        1. Following execution, all input ports must be at end-of-data.
        2. Following execution, all output ports must be at end-of-data.
        Specified by:
        execute in class ExecutableOperator
        Parameters:
        ctx - context in which to lookup physical ports bound to logical ports