Looking for values in a database with extreme flexibility

The Database join step that you learned to use in the previous recipe is quite powerful and has several advantages over the simple Database lookup step. There is a still more powerful step for searching in a database: The Dynamic SQL row step. This recipe explains to you its capabilities and shows you how to use it.

In order to let you compare the different options for searching in a database with ease, we will work with an example similar to that you saw in the previous two recipes: we will work with the Steel Wheels sample data. You want to look for the following products:

  • Products that contain Aston Martin in their description
  • Products that contain Ford Falcon in their name and with scale 1:18
  • Products that contain Corvette in their name and with scale 1:24

Getting ready

In order to follow this recipe, you need the Steel Wheels database.

How to do it...

Carry out the following steps:

  1. Create a new transformation.
  2. Create a stream that generates a dataset like the one shown in the following screenshot:
    How to do it...

    Tip

    You can type the data into a file and read the file, or use a Data Grid.

  3. At the end of your stream, add a User Defined Java Expression step.
  4. Use that step to add a String named statement. As Java expression, type"SELECT PRODUCTNAME, PRODUCTSCALE, BUYPRICE FROM PRODUCTS WHERE PRODUCTNAME LIKE '%"+prod+"%'"+(cond!=null?" AND "+cond:"").
  5. Do a preview on this step. You will see a new column named statement with a complete SQL statement, for example:
    SELECT PRODUCTNAME, PRODUCTSCALE, BUYPRICE FROM PRODUCTS WHERE PRODUCTDESCRIPTION LIKE '%Aston Martin%'
    
  6. Add a Dynamic SQL row step. You will find it in the Lookup category of steps.
  7. Double-click on the step. As Connection, select (or create if it doesn't exist) the connection to the sampledata database.
  8. As SQL field name, type or select statement.
  9. Check the Outer join? option.
  10. In the Template SQL (to retrieve Meta data) frame, type the following:
    SELECT 'NAME', 'SCALE', 1 as BUYPRICE
    
  11. Close the Dynamic SQL row configuration window and do a preview on this step. You will see the following screen (note that the statement field is hidden):
How to do it...

How it works...

The Dynamic SQL row step is a very powerful step for looking for data in a database.

If we take, for example, the first row in the dataset, the SQL statement (the one that you build with the Java expression) is similar to the following:

SELECT PRODUCTNAME, PRODUCTSCALE, BUYPRICE FROM PRODUCTS WHERE PRODUCTDESCRIPTION LIKE '%Aston Martin%'

That's exactly the search you wanted to perform for that row: look for the records where the column PRODUCTDESCRIPTION contained Aston Martin.

Now look at the following SQL statement for the last row:

SELECT PRODUCTNAME, PRODUCTSCALE, BUYPRICE FROM PRODUCTS WHERE PRODUCTNAME LIKE '%Corvette%' AND PRODUCTSCALE="1:24"

In this case, you are filtering both by the name and by the scale.

As you see, what you are doing is dynamically creating an SQL statement. Then, in the Dynamic SQL row configuration window, you just use the SQL field name to indicate which field contains the SQL statement to execute.

As the output of the Dynamic SQL row step, you can retrieve any number of columns. Each database column in the SELECT statement will become a new field in your dataset.

In order to tell Kettle the metadata of the new fields being added with this dynamic statement, you must fill in the Template SQL (to retrieve Meta data) frame. Here, you have to type any statement that returns the same structure as the new fields. Kettle will take from here both the names and the types for the new fields.

In the recipe, you typed SELECT 'NAME', 'SCALE', 1 as BUYPRICE. With this statement, you are telling Kettle that you are adding three fields: two strings named NAME and SCALE and an integer field named BUYPRICE.

Note

Both the statement and this template are written using MySQL syntax. It's mandatory that you restate them to match the syntax of the database engine you are using.

In the recipe, you checked the Outer join? option. The effect of this is the same as in the Database join step: for the rows where the lookup fails the new fields are retrieved with null values. That was the case for the second row. There were no products with Ford Falcon as part of its name and scale 1:18. Therefore, that row shows empty values for the PRODUCTNAME, PRODUCTSCALE, and BUYPRICE fields.

For the third row, the Corvette product, you can see two results. This means that the Dynamic SQL row found two matching rows in the database and retrieved them both. This also resembles the behavior of the Database join step.

Finally, note that in the recipe, the statement was different for each row. It may happen however, that your statements do not change a lot. If this is the case, you can reduce the number of physical database queries by checking the Query only on parameters change option.

There's more...

The Dynamic SQL row step is the most flexible step for looking up data in a database. As you saw, there are a couple of similarities between this and the Database join step. What really makes a difference between them is that with the Dynamic SQL row step, any part of the statement can be built dynamically based on the fields in your stream. For example, the columns used for comparison in the recipe were the product description in the first row and the product name in the others. What you did in the recipe is not possible to achieve by using the Database join, at least in a simple fashion.

Note that you could also have had the statement already built, for example, in a property file or saved in a column in a database table. In that case, you also could have used the Dynamic SQL row step to execute the statement and that is definitely impossible to do with any other step.

See also

The recipe named Looking for values in a database (with complex conditions or multiples tables involved) in this chapter. Look at this recipe for understanding how to look in a database by using the Database join step.

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

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