Time for action—getting variables for setting the default starting date

Let's modify the transformation so that the starting date depends on a parameter.

  1. Press Ctrl+T to open the transformation settings window.
  2. Add a parameter named START_DATE with default value 01/12/1999.
  3. Add a Get variables step between the Calculator step and the Filter rows step .
  4. Edit the Get variables step and a new field named start_date. Under Variable write ${START_DATE}. As Type select Date, and under Format select or type dd/MM/yyyy.
  5. Modify the filter step so the condition is now: date>=start_date and date<=31/12/2020.
  6. Modify the Select values step to remove the start_date field.
  7. With the Select values step selected do a preview. You will see this:
    Time for action—getting variables for setting the default starting date

What just happened?

You added a starting date as a named parameter. Then you read that variable into a new field and used it to keep only the dates that are greater or equal to its value.

Using the Get Variables step

As you just saw, the Get Variables step allows you to get the value of one or more variables. In the main tutorial you saw how to use the step at the beginning of a stream. Now you saw how to use it in the middle. The following is the dataset after the Get Variables step for this last exercise:

Using the Get Variables step

With the Get Variables step, you can read any Kettle variable—variables defined in the kettle.properties file, internal variables as for example ${user.dir}, named parameters as in this tutorial, or variables defined in another transformation (you haven't yet learned about these variables but you will soon).

As you know, the type of Kettle variables is String by default. However, at the time you get a variable, you can change its metadata. As an example of that, in this last exercise you converted ${START_DATE} to a Date by using the mask dd/MM/yyyy.

Note that you specified the variables as ${name of the variable}. You could have used %%name of the variable%% also. The full specification of the name of a variable allows you to mix variables with plain text.

Suppose that instead of a date you create a parameter named YEAR with default value 1950.

In the Get variables step you may specify 01/01/${YEAR} as the value.

When you execute the transformation, this text will be expanded to 01/01/1950 or to 01/01/ plus the year you enter if you overwrite the default value.

Note

Note that the purpose of using the Get Variable step is to have the values of variables as fields in the dataset. Otherwise, you don't need to use this step for using a variable. You just use it wherever you see a dollar sign icon.

Have a go hero—enhancing the time dimension

Modify the time dimension generation by doing the following:

  • Add the following fields to the dataset, taking as model the generation of weeks: Name of month, Short name of month, and Quarter.
  • Add two more parameters: start_year and end_year. Modify the transformation so that it generates dates only between those years. In other words, you have to discard dates out of that range. You may assume that the parameters will be between 1900 and 2020.

Pop quiz—using Kettle variables inside transformations

There are some Kettle predefined variables that hold information about the logged in user: user.country, user.language, etc. The following tasks involve the use of some of those variables. Which of the tasks can be accomplished without using a Get Variables step or a JavaScript step (Remember from the previous chapter that you can also get the value for a Kettle variable with a Javascript step):

a. Create a file named hello_<user>.txt, where<user> is the name of the logged user.

b. Create a file named hello.txt that contains a single line with the text Hello, <user>!, <user> being is the name of the logged user.

c. Write to the log (by using the Write to log step) a greeting message like Hello, user!. The message has to be written in a different language depending on the language of the logged user.

d. All of the above

e. None of the above

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

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