Splitting rows based on a column value. The input data comprises of ticket booking records defining number of seats booked at event, section and row level. It also contains the starting and last seat number.
Objective
It was required to split ticket blocks within event_name + section_name + row_name as follows:
-
- convert the record where num_seats > 1 into as many records as num_seats
- assign values as follow in split records
- num_seats = 1 for each record
- seat_num = individual seat within the block
- original seat_num + i where “i” is counter from 0 to num_seats – 1
-
-
-
- last_seat = new value of the seat_num as above
- population logic of rest of the column remains unchanged
-
-
Sample Data Input:
Expected Output:
So based on the above screenshots, we need to split the incoming input rows based on the num_seats field .So for first input row where num_seats=4 we need to generate 4 records as per the rules defined above.
Solution:
Pentaho provides a clone row step that can clone objects or rows in the same way as the main row based on a column value.
Refer to the below screenshot for the solution:
- Table input: This step will load the input data.
- Clone row: This step will create the clone objects or rows similar to the main row
-
-
- Nr clone in field: will specify column value to be used for cloning
- Add clone flag to output: will put the flag=N for the original row and Flag=Y for clone rows
- Clone num field (seat_index_rownum): will add the index value (0,1,2,..).
-
- Filter rows: Remove the original (non-cloned) row (where clone?=N).
- Calculator and Select Values: Calculate the seat number and replace the original fields (num_seats, seat_num, last_seat, etc.) with the new values.
- Table Output: Loading the data into the target table.