In order to search for data in a database, Kettle offers several options. The simplest situation is the one in which you need to get one or more columns from a single database table. In this recipe, you will learn how to do this by using the Database lookup step.
We will work with the Steel Wheels sample data. If you don't know about it, refer to Chapter 1, Working with Databases. Suppose that you want to look for products that match a given search term and whose prices are below a given value. This recipe shows you how to do this.
Carry out the following steps:
String
items: prod
and max_price
. Then, complete the Data tab, as shown in the following screenshot: like_statement
. As Java expression, type"%"+prod+"%"
. sampledata
database. As Lookup table type or browse for PRODUCTS
.The Database lookup step allows you to look for values in a database table. To perform that search, you need to specify at least the following:
The upper grid is where you specify the conditions. Each row in the grid represents a comparison between a column in the table and a field in your stream, by using one of the provided comparators.
Take a look at the upper grid in the recipe. The conditions you entered were:
BUYPRICE < max_price
and PRODUCTNAME LIKE like_statement
If we take, for example, the first row in the dataset, the condition can be restated as:
BUYPRICE<90
and PRODUCTNAME LIKE %Aston Martin%
That's exactly the search you wanted to perform for that row: look for the records where the column BUYPRICE
was less than 90
and the column PRODUCTNAME
contained Aston Martin
.
As the result of the database lookup, you can retrieve any number of columns. Each database column you enter in the lower grid will become a new field in your dataset. You can rename them (this is particularly useful if you already have a field with the same name) and supply a default value if no record is found in the search.
In the recipe, you added three fields: PRODUCTNAME, PRODUCTSCALE
, and BUYPRICE
. By default, for PRODUCTNAME
you typed not available
. In the final preview, you can see that description in the second row. This means that there were no products with Ford Falcon
as part of their description and price lower than 70
.
The recipe showed the minimal setting of the Database lookup step. The step has more options that can be useful as explained in the following subsections.
When you perform a search with the Database lookup step, you expect that the search returns a row. If the data you are looking for doesn't exist in the table then the lookup fails. If this happens, the fields you added in the lower grid are added to the dataset anyway with null values or with the default values, if you provided them. That is the default behavior. However, you have two more options if you don't like that, which are as follows:
Remember that you can capture errors by right-clicking on the Database lookup step, selecting the Define error handling... option and configuring the Step error handling settings window properly. At least, you have to check the Enable the error handling? option and select the Write to log step as the Target step.
The Database lookup step is meant to retrieve just one row of the table for each row in your dataset. If the search finds more than one row, the following two things may happen:
... - Database lookup (fail on multiple res.).0 ERROR... Because of an error, this step can't continue: ... - Database lookup (fail on multiple res.).0 ERROR... : Only 1 row was expected as a result of a lookup, and at least 2 were found!
sampledata
database, there are three products that meet the conditions for the Corvette
row. If, for Order by, you type PRODUCTSCALE DESC, PRODUCTNAME
, then you will get 1958 Chevy Corvette Limited Edition
, which is the first product after ordering the three found products by the specified criterion.If instead of taking some of those actions, you realize that you need all the resulting rows, you should take another approach: replace the Database lookup step with a Database join or a Dynamic SQL row step. For recipes explaining these steps, see the following See also section.
If instead of looking for a row you want to determine if the row doesn't exist, the procedure is much the same. You configure the Database lookup step to look for those rows. Then you capture the error, as depicted in the following diagram:
In this case, the stream that you use for capturing the error becomes your main stream. The rows that didn't fail will be discarded and the rows for which the lookup failed go directly to the main stream for further treatment.
3.12.76.164