Time for action—deleting data about discontinued items

Suppose a manufacturer informs you about the categories of products that will no longer be available. You don't want to have in your database products something that you will not sell. Then you use PDI to delete them.

  1. From the Packt website, download the LUX_discontinued.txt file.
  2. Create a new transformation.
  3. With a Text file input step, read the file.
  4. Preview the file. You will see the following:
    Time for action—deleting data about discontinued items
  5. After the Text file input step, add an Add constants step to add a String constant named man_code with value LUX.
  6. Expand the Output category of steps and drag a Delete step to the canvas.
  7. Create a hop from the Add constants step to the Delete step.
  8. Double-click the Delete step. Select js as Connection and, as Target table, browse and select products. In the grid add the conditions man_code = man_code and pro_theme LIKE category. After the Delete step, add a Write to log step.
  9. Right-click the Delete step and define the error handling just like you did in each of the previous tutorials in this chapter.
  10. Save the transformation.
  11. Before running the transformation, open the Database Explorer.
  12. Under the js connection, locate the products table and click Open SQL for [products].
  13. In the simple SQL editor type:
    SELECT pro_theme, pro_name FROM js.products p
    ORDER BY pro_theme, pro_name;
    
  14. Click on Execute. You will see the following result set:
    Time for action—deleting data about discontinued items
  15. Close the preview data window and the results of the SQL window.
  16. Minimize the database explorer window.
  17. The database explorer is collapsed at the bottom of the Spoon window.
  18. Run the transformation.
  19. Look at the Step Metrics. The following is what you should see:
    Time for action—deleting data about discontinued items
  20. Maximize the database explorer window.
  21. In the SQL editor window click Execute again. This time you will see this:
    Time for action—deleting data about discontinued items

What just happened?

You deleted from the products table all products belonging to the categories found in the LUX_discontinued.txt file.

Note that to query the list of products, you used the PDI Database explorer. You could have done the same by using MySQL Query Browser.

Deleting records of a database table with the Delete step

The Delete step allows you to delete records of a database table based on a given condition. For each row coming to the step, PDI deletes the records that match the condition set in its configuration window.

Let's see how it worked in the tutorial. The following is the dataset coming to the Delete step:

Deleting records of a database table with the Delete step

For each of these two rows PDI performs a new delete operation.

For the first row, the records deleted from the products table are those where man_code is equal to LUX and pro_theme is like FAMOUS LANDMARKS.

For the second row, the records deleted from the products table are those where man_code is equal to LUX and pro_theme is like COUNTRYSIDE.

You can verify the performed operations by comparing the result sets you got in the database explorer before and after running the transformation.

Just for your information, you could have done the same task with the following DELETE statements:

DELETE FROM products
WHERE man_code = 'LUX' and pro_theme LIKE 'FAMOUS LANDMARKS'
DELETE FROM products
WHERE man_code = 'LUX' and pro_theme LIKE 'COUNTRYSIDE'

In the Step Metrics result, you may notice that the updated column for the Delete step has value 2. This number is the number of delete operations, not the number of deleted records, which was actually a bigger number.

Have a go hero—deleting old orders

Create a transformation that asks for a date from the command line and deletes all orders from the Steel Wheels database whose order dates are before the given date.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.103.27