Class ProcessByGroup

  • All Implemented Interfaces:
    LogicalOperator, RecordSinkOperator, SinkOperator<RecordPort>, ScriptOptionsAware

    public class ProcessByGroup
    extends ExecutableOperator
    implements RecordSinkOperator, ScriptOptionsAware
    Executes an application graph for each distinct key group of data within the input data set. The data for each key group is fed to a spawned application graph. The application graph is composed using the provided RushScript. The source data and the key values are provided to each executed RushScript as variables. The names of these variables are specified by properties to this operator.

    This operator supports parallel execution. Each operator instance will spawn DataRush applications that are limited to running on the machine of the spawning operator. When run distributed, this implies that applications executed by the operator will not be distributed. The spawned jobs run locally.

    By default, the spawned graphs will run with parallelism of 1. Default parallelism is limited to prevent over subscribing of system resources. This default value can be changed using the setSubParallelism(int) method to set the parallelism level wanted. Caution should be used when setting this value as higher values require more system resources to process jobs. Memory consumption is especially affected by multiple jobs running with high-parallelism levels.

    • Constructor Detail

      • ProcessByGroup

        public ProcessByGroup()
        Construct an instance of the operator. The keys property is required and should be set using the setKeys(List) method.
      • ProcessByGroup

        public ProcessByGroup​(List<String> keys)
        Construct an instance of the operator with the given key fields.
        Parameters:
        keys - names of the fields to use as keys for partitioning and sorting the input data.
      • ProcessByGroup

        public ProcessByGroup​(String... keys)
        Construct an instance of the operator with the given key fields.
        Parameters:
        keys - names of the fields to use as keys for partitioning and sorting the input data.
    • Method Detail

      • setScriptOptions

        public void setScriptOptions​(ScriptOptions environment)
        Description copied from interface: ScriptOptionsAware
        Set a collection of scripting options that are interesting to operators utilizing scripting in some way.
        Specified by:
        setScriptOptions in interface ScriptOptionsAware
        Parameters:
        environment - set of scripting options
      • setKeys

        public void setKeys​(List<String> keys)
        Set the names of the key fields used to partition and sort the input data set. Order is important as it specifies the order of partitioning and sorting.
        Parameters:
        keys - list of key field names
      • setKeys

        public void setKeys​(String... keys)
        Set the names of the key fields used to partition and sort the input data set. Order is important as it specifies the order of partitioning and sorting.
        Parameters:
        keys - list of key field names
      • getKeys

        public List<String> getKeys()
        Get the names of the key fields.
        Returns:
        key field names
      • getScript

        public String getScript()
        Get the text of the script used for graph composition.
        Returns:
        script text
      • setScript

        public void setScript​(String script)
        Set the text of the RushScript that will be used to compose the subgraphs run per data grouping.
        Parameters:
        script - script text
      • setScriptFile

        public void setScriptFile​(File scriptFile)
        Set the text of the RushScript that will be used to compose the subgraphs run per data grouping. This is a convenience method that will source the text from the provided file.
        Parameters:
        scriptFile - file containing the text of the RushScript used for graph composition
      • setScriptFileName

        public void setScriptFileName​(String scriptFileName)
        Set the name of the file containing the script used to compose the application executed per key group. If this operator is invoked within an application built using RushScript, the file may be found using the include directories specified at run time. Otherwise, the file name must be an absolute path name.
        Parameters:
        fileName - name of the file containing the RushScript source
      • getContextVariableName

        public String getContextVariableName()
        Get the name of the RushScript variable containing the current context.
        Returns:
        RushScript variable name
      • setContextVariableName

        public void setContextVariableName​(String contextVariableName)
        Set the name of the RushScript variable containing the current context. The default value is "context".
        Parameters:
        contextVariableName - RushScript variable name for the context
      • setVariables

        public void setVariables​(Map<String,​Object> variables)
        Provides the JavaScript variables to set within the RushScript environment before composing applications for each group of the input data. These variables can be used for passing settings such as an output directory to the script that composes the applications.
        Parameters:
        variables - a map of valid JavaScript variable names to values
      • getVariables

        public Map<String,​Object> getVariables()
        Gets the JavaScript variables that will be set before composing applications for each data group.
        Returns:
        map of variable names to values
      • addVariable

        public void addVariable​(String name,
                                Object value)
        Adds a single JavaScript variable name and value to the map of variables. Any variables set by this method will be lost if setVariables(Map) is invoked after.
        Parameters:
        name - valid JavaScript variable name
        value - value to set for the variable
      • setSubParallelism

        public void setSubParallelism​(int value)
        Set the parallelism level wanted for the sub-graphs spawned by this operator. By default, the parallelism will be set to 0, indicating all available resources should be consumed.
        Parameters:
        value - parallelism level
      • getSubParallelism

        public int getSubParallelism()
        Get the level of parallelism for spawned sub-graphs.
        Returns:
        parallelism level
      • getFunctionName

        public String getFunctionName()
        Gets the name of the JavaScript function to invoke after source evaluation.
        Returns:
        function name
      • setFunctionName

        public void setFunctionName​(String functionName)
        Sets the name of a JavaScript function to invoke after evaluating all source files. The function can be used to further compose the DataRush application.
        Parameters:
        functionName - JavaScript function name
      • 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