Monday, February 20, 2012

Talend Open Studio: Populating a date dimension


Populating a date dimension

Date dimensions are an essential part of a data warehouse. Usually they are only populated once. Scripts can be created on the database side (as outlined here), but if you are working on various projects involving a variety of databases, it is more efficient to create just one ETL job which can be used to populate any database.

In this tutorial we will have a look at creating such an ETL job with Talend Open Studio for Data Integration. We will create a basic date dimension which you can then extend even further. In order to follow this tutorial, the reader should be familiar with the basic functionality of Talend Open Studio.

Our date dimension will look like this one (partial screenshot):


The primary key of the dimension will be an integer representation of the date, which saves us the hassle of looking up the key when we transform the fact data.

Open Talend Open Studio for Data Integration and create a new job called populate_date_dimension. First we will define a variable called start date, because we will use this job in various projects and we might require a different start date each time:

Click on the Context tab and then on the + button to add a new context variable. Give it the name myStartDate of type Date and define a value for it.


Next add a tRowGenerator component to the design area and double click on it to activate the settings dialog. The idea is to create X amount of rows: The first row will hold our start date and each subsequent row will increment the date by one day.


  1. Click the + button to add a new column. Name it date and set the type to Date.
  2. Click in the Environment variables cell on the right hand side and then you will see the parameters displayed in the Function parameters tab on the bottom left hand side.
  3. Define the number of rows that should be generated in Number of Rows for RowGenerator.
  4. In the Function parameters tab set the date parameter value to context.myStartDate. This will ensure that the context variable which we defined earlier will be used.
  5. Set the nb parameter to Numeric.sequence(“s1”, 1, 1) - 1. Use the expression builder for a more convenient setup. This will create a sequence which we will use to add days to our start date. The reason why we subtract 1 at the end is because we want to keep our start date.
  6. Set the dateType parameter value to “dd”. This ensures that days will be added to our date.
  7. Click on the Preview tab and check if the result set looks as expected.
  8. Click Ok to close the component settings.


Now add a tMap component and create a row from the tRowGenerator to the tMap component. Double click the tMap component:




  1. Click the + button on the right hand side to create a new output table.
  2. Add new columns to the output table and for each of them define a specific date format using this approach: Integer.parseInt(TalendDate.formatDate("yyyyMMdd",row1.date)) for integer values and TalendDate.formatDate("MM",row1.date) for string values. Have a look at the Java SimpleDateFormat specs to get an understanding of all the formatting options. You will spend now some time setting all the various date formats up.
  3. Java SimpleDateFormat doesn’t provide a quarter format, hence we have to create our own in the form of a ceiled devision / covered quotient: (Integer.parseInt(TalendDate.formatDate("M",row1.date))+3-1) /  3   
  4. Click Ok.


Add a database output component of your choice (in my case I used one for PostgreSQL) and create a row from the tMap to the database output component. Double click the database output component and provide all the necessary settings. That’s it: Now you can run the job and examine the data in your table.

8 comments:

  1. Hi,

    I've some problem when I've done set the parameters in tRowGenerator and try to preview it, i hit errors saying that

    Run Error!
    Please check your parameters!Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Syntax error on tokens, ArgumentList expected instead

    at talenddemosjava.preview_rowgenerator2.Preview_RowGenerator2.tRowGenerator_1Process(Preview_RowGenerator2.java:411)
    at talenddemosjava.preview_rowgenerator2.Preview_RowGenerator2.runJobInTOS(Preview_RowGenerator2.java:755)
    at talenddemosjava.preview_rowgenerator2.Preview_RowGenerator2.main(Preview_RowGenerator2.java:620)
    \n

    plz, help me.

    ReplyDelete
  2. Hi,

    I've followed the steps but when i execute the job, i hit an error saying that "Error in the component's properties:Syntax error on tokens, ArgumentList expected instead" in tRowGenerator_1

    Could you help me plz.

    ReplyDelete
  3. Thanks a lot for your important help, without it i would have passed a bad week! i appreciated

    ReplyDelete
    Replies
    1. Excellent! Good to hear that this blog post was useful!

      Delete
  4. Hello!
    I have a similar error as Haikal.
    Please check your parameters!Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Syntax error on tokens, ArgumentList expected instead

    at talenddemosjava.preview_rowgenerator2.Preview_RowGenerator2.tRowGenerator_1Process(Preview_RowGenerator2.java:411)
    at talenddemosjava.preview_rowgenerator2.Preview_RowGenerator2.runJobInTOS(Preview_RowGenerator2.java:755)
    at talenddemosjava.preview_rowgenerator2.Preview_RowGenerator2.main(Preview_RowGenerator2.java:620)

    Please help me!

    ReplyDelete
    Replies
    1. hi,
      i have the same problem here ! Did you solve it ?

      Delete
  5. For the guys with the "ArgumentList" problem:

    If you directly copy the bold Numeric.sequence from the above example, the following character is not the standard character:

    "

    Make sure to delete the ones copied out of the article and just type them directly again with your keyboard.

    ReplyDelete
  6. Thankyu for the tutorial. I find it's easy to understand.

    ReplyDelete