Steps to perform Pentoho Advanced Operations with Databases
Populating the Jigsaw database:
- From Packt’s website download the js_data.sql script file.
- Launch the MySQL query browser.
- From the File menu select Open Script….
- Locate the downloaded file and open it.
- At the beginning of the script file you will see this line:
USE js;
If you created a new database, replace the name js by the name of your new database.
- Click on the Execute button.
- At the bottom of the screen, you’ll see a progress message.
- When the script execution ends, verify that the database has been populated. Execute some SELECT statements such as:
SELECT * FROM cities
All tables must have records.
Using a Database lookup step to create a list of products to buy:
- Create a new transformation.
- From the Input category of steps, drag a Get data from XML step to the canvas.
- Use it to read the orders.xml file. In the Content tab, fill the Loop XPath option with the /orders/order string. In the Fields tab get the fields.
- Do a preview. You will see the following:
- Add a Sort rows step and use it to sort the data by man_code, prod_code.
- Add a Group by step and double-click it.
- Use the upper grid for grouping by man_code and prod_code.
- Use the lower grid for adding a field with the number of orders in each group. As Name write quantity, as Subject ordernumber, and as Type write Number of Values (N). Expand the Lookup category of steps.
- Drag a Database lookup step to the canvas and create a hop from the Group by step toward this step.
- Double-click the Database lookup step.
- As Connection, select js and in Lookup table, browse the database and select products or just type its name.
- Fill the grids as follows:
- Click on OK.
- Add a filter step to pass only the rows where pro_stock<quantity.
- Add a Text file output step to send the manufacturer code, the product code, the product name, and the ordered quantity to a file named products_to_buy.txt.
- Run the transformation.
- The file should have the following content:
man_code;prod_code;pro_name;quantity
EDU;ED13_93;Times Square;1
RAV;RVZ50031;Disney World Map;2
RAV;RVZ50106;Star Wars Clone Wars;1
Using a Database join step to create a list of suggested products to buy:
- Open the transformation of the previous tutorial and save it under a new name.
- Delete the Text file output step.
- Double-click the Group by step and add an aggregated field named customers with the list of customers separated by (,). Under Subject, select idcus and as Type, select Concatenate strings separated by ,.
- Double-click the Database lookup step. In the Values to return from the lookup table grid, add pro_theme as value in the String field.
- Add a Select values step. Use it to select the fields customers, quantity, pro_theme, and pro_name. Also rename quantity as quantity_param and pro_theme as theme_param. From the Lookup category, drag a Database join step to the canvas. Create a hop from the Select values step to this step.
- Double-click the Database join step.
- Select js as Connection.
- In the SQL frame type the following statement:
SELECT man_code
, pro_code
, pro_name
FROM products
WHERE pro_theme like ?
AND pro_stock>=?
- In the Number of rows to return textbox, type 4.
- Fill the grid as shown:
- Click on OK. The transformation looks like this:
- With the last step selected, do a Preview.
- You should see this:
- In the Step Metrics you should see this:
Using a Database join step to create a list of suggested products to buy:
- Open the transformation of the previous tutorial and save it under a new name.
- Delete the Text file output step.
- Double-click the Group by step and add an aggregated field named customers with the list of customers separated by (,). Under Subject, select idcus and as Type, select Concatenate strings separated by ,.
- Double-click the Database lookup step. In the Values to return from the lookup table grid, add pro_theme as value in the String field.
- Add a Select values step. Use it to select the fields customers, quantity, pro_theme, and pro_name. Also rename quantity as quantity_param and pro_theme as theme_param. From the Lookup category, drag a Database join step to the canvas. Create a hop from the Select values step to this step.
- Double-click the Database join step.
- Select js as Connection.
- In the SQL frame type the following statement:
SELECT man_code
, pro_code
, pro_name
FROM products
WHERE pro_theme like ?
AND pro_stock>=?
- In the Number of rows to return textbox, type 4.
- Fill the grid as shown:
- Click on OK. The transformation looks like this:
- With the last step selected, do a Preview.
- You should see this:
- In the Step Metrics you should see this:
Testing the transformation that keeps a history of product changes:
- In the previous tutorial you loaded a dimension with products by using a Dimension lookup/update step. You ran the transformation once, causing the insertion of one record for each product and a special record with values n/a for the descriptive fields. Let’s apply some changes in the operational database, and run the transformation again to see how the Dimension lookup/update step keeps history.
- In MySQL Query Browser, open the script update_jumbo_products.sql and run it.
- Switch to Spoon.
- If the transformation created in the last tutorial is not open, open it again.
- Run the transformation. Explore the js_dw database again. Press Open SQL for [lk_puzzles] and type the following sentence:
SELECT *
FROM lk_puzzles
WHERE id_js_man = 'JUM'
ORDER BY id_js_prod, version
- You will see this: