Class ColumnsToRows

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

    public class ColumnsToRows
    extends AbstractExecutableRecordPipeline
    Normalize records by transposing values from row columns into multiple rows. Users define one or more pivot value families to describe the output field and the source field(s) that will be mapped to these fields in the output. Target field datatypes are determined by finding the widest common scalar data type of the pivot elements. An exception can occur if no common type is possible. The number of elements defined in a keyFamily and/or valueFamily must be equal. Users may optionally define up to one pivot key family. This allows users to provide a context label when transposing rows defined in value families. Users set a list of Strings that correspond positionally to the fields defined in the list portion of defined value families. The size of all lists across both value families and key family must be the same. Users may optionally define groupKeyFields which will be the fixed or repeating value portion of the multiple rows. If this property is unset, group key fields will be determined by the remainder of the source fields not specified as pivot elements.
    • Constructor Detail

      • ColumnsToRows

        public ColumnsToRows()
    • Method Detail

      • definePivotValueFamily

        public void definePivotValueFamily​(String valueFieldName,
                                           String... sourceUnpivotFields)
        Define a pivotValueFamily consisting of the target field and the source fields to be transposed. Target field output is determined by widest source type. A new pivotValueFamily is created per call.
        Parameters:
        valueFieldName - the name of the field to appear in the output
        sourceUnpivotFields - the source fields to be pivoted
      • definePivotKeyFamily

        public void definePivotKeyFamily​(String keyFieldName,
                                         String... labels)
        Define up to one pivotKeyFamily consisting of the target field and the constant String values. Calling this method repeatedly, overwrites the previous call's settings.
        Parameters:
        keyFieldName - the name of the field to appear in the output
        labels - the String values that will be added to each defined pivot value family
      • 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
      • 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
      • setGroupKeyFields

        public void setGroupKeyFields​(String... groupKeyFields)
        Optional parameter to specify the fixed or repeating key fields. If this is left unset, the group key fields will automatically be made up of source fields not specified in any pivot family
        Parameters:
        groupKeyFields -
      • getGroupKeyFields

        public List<String> getGroupKeyFields()
      • getValueOrder

        public List<String> getValueOrder()
      • setValueOrder

        public void setValueOrder​(List<String> valueOrder)
        Indicates the order of the fields defined in the valueMap to appear in the output. If this parameter is unset, no order is gauranteed.
        Parameters:
        valueOrder - list of the fields in the valueMap in the order you wish for them to appear in the output.
      • setKeyMap

        public void setKeyMap​(Map<String,​List<String>> keyMap)
        Sets the name of the output field and the constant values (labels) that will appear with their corresponding valueMap entry based on position. Only one keyMap value should be defined. If more that one is set an exception will occur.
        Parameters:
        keyMap - output field name and list of label fields to appear in this column in the output.
      • setValueMap

        public void setValueMap​(Map<String,​List<String>> valueMap)
        Sets the name of the output field and the list of input fields which will be transposed in the output. The number of elements defined in the list portion must be consistent throughout all valueMap entries as well as the number of elements defined in the list portion of the keyMap if one keyMap entry is defined.
        Parameters:
        valueMap - output field name and lsit of input fields to be transposed to this column in the output.