public class RowsToColumns extends CompositeOperator implements RecordPipelineOperator
The table below provides an example set of data that we want to pivot by Region. There are only four regions: North, South, East and West. For each item, we'd like to compute the total sales per region. Items can show up multiple times in a region, as the data is also organized by store.
ItemID | StoreID | Region | Sales |
---|---|---|---|
1 | 10 | North | 1000 |
1 | 15 | North | 800 |
1 | 20 | South | 500 |
1 | 30 | East | 700 |
2 | 40 | West | 1200 |
2 | 10 | North | 500 |
2 | 15 | North | 200 |
To accomplish this pivot, the ItemID will be used as the group key. The Region will be used as the pivot key. And the Sales column will be the pivot value, aggregating by summing the values. The pivot key values are: "North", "South", "East" and "West". The result of the pivot is shown in the table below. Note that the sales total for the West region for item 1 is empty. Scanning the input data shows that no sales were present in the West region for item 1. Item 1 did have two sales values for the North region. Those values (1000 and 800) are summed and the total (1800) appears in the North region column for item 1. Column values of ? indicate a null (non-existing) value.
ItemID | North | South | East | West |
---|---|---|---|---|
1 | 1800 | 500 | 700 | ? |
2 | 700 | ? | ? | 1200 |
The key concepts to understand usage of the RowsToColumns operator are:
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULTCOLUMNPATTERN |
Constructor and Description |
---|
RowsToColumns() |
Modifier and Type | Method and Description |
---|---|
protected void |
compose(CompositionContext ctx)
Compose the body of this operator.
|
List<Aggregation> |
getAggregations()
Get the aggregation(s) applied to each pivot group.
|
List<String> |
getGroupKeys()
Get the list of fields used to segment the input data into groups.
|
RecordPort |
getInput()
Returns the input port
|
RecordPort |
getOutput()
Returns the output port
|
String |
getPivotKey()
Get the name of the field used as the pivot key.
|
List<String> |
getPivotKeyValues()
Get the list of distinct values of the pivot key field.
|
void |
setAggregations(Aggregation... aggregations)
Set the aggregation(s) to apply to each pivot grouping.
|
void |
setAggregations(List<Aggregation> aggregations)
Set the aggregation(s) to apply to each pivot grouping.
|
void |
setAggregations(String aggregationExpression)
Set the aggregation(s) to apply to each pivot grouping using an aggregation expression.
|
void |
setGroupKeys(List<String> groupKeys)
Set the list of fields used to segment the input data into groups for pivoting.
|
void |
setGroupKeys(String... groupKeys)
Set the list of fields used to segment the input data into groups for pivoting.
|
void |
setPivotColumnPattern(String pattern)
Sets the naming pattern used for new pivot columns.
|
void |
setPivotKey(String pivotKey)
Set the name of the field used as the pivot key.
|
void |
setPivotKeyValues(List<String> pivotValues)
Set the list of distinct pivot key values.
|
void |
setPivotKeyValues(String... pivotValues)
Set the list of distinct pivot key values.
|
disableParallelism, getInputPorts, getOutputPorts, newInput, newInput, newOutput, newRecordInput, newRecordInput, newRecordOutput, notifyError
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
disableParallelism, getInputPorts, getOutputPorts
public static final String DEFAULTCOLUMNPATTERN
public RecordPort getInput()
PipelineOperator
getInput
in interface PipelineOperator<RecordPort>
public RecordPort getOutput()
PipelineOperator
getOutput
in interface PipelineOperator<RecordPort>
public List<String> getGroupKeys()
public void setGroupKeys(List<String> groupKeys)
groupKeys
- ordered list of grouping field namespublic void setGroupKeys(String... groupKeys)
groupKeys
- ordered list of grouping field namespublic String getPivotKey()
public void setPivotKey(String pivotKey)
pivotKey
- name of the pivot key fieldpublic List<Aggregation> getAggregations()
public void setAggregations(List<Aggregation> aggregations)
aggregations
- aggregation(s) to apply to each pivot data grouppublic void setAggregations(Aggregation... aggregations)
aggregations
- aggregation(s) to apply to each pivot data grouppublic void setAggregations(String aggregationExpression)
aggregations
- aggregation(s) to apply to each pivot data grouppublic List<String> getPivotKeyValues()
public void setPivotKeyValues(List<String> pivotValues)
pivotValues
- list of distinct pivot key valuespublic void setPivotKeyValues(String... pivotValues)
pivotValues
- list of distinct pivot key valuespublic void setPivotColumnPattern(String pattern)
pattern
- name pattern for pivot columnsprotected void compose(CompositionContext ctx)
CompositeOperator
OperatorComposable.add(O)
OperatorComposable.connect(P, P)
. This includes
connections from the composite's input ports to sub-operators, connections between sub-operators, and
connections from sub-operators output ports to the composite's output portscompose
in class CompositeOperator
ctx
- the contextCopyright © 2019 Actian Corporation. All rights reserved.