Generating Date Dimension
Create a new job
called date_dim. First we will define a variable for 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.
A. Now add a tMap
component and create a row from the tRowGenerator
to the tMap component.
Double click the tMap component:
- Click the + button on the
right hand side to create a new output table.
- For year column extract the year
using TalendDate.formatDate("yyyy",row1.date)
- For Quater details: (Integer.parseInt(TalendDate.formatDate("M",row1.date))+3-1)
/ 3
- For Month number : TalendDate.formatDate("MM",row1.date)
- For Month name : TalendDate.formatDate("MMM",row1.date)
- For Weekday number (1-5) : row1.date.getDay()
- For day part of the date (1-31) : row1.date.getDate()
- For week of the year(1-52):
TalendDate.formatDate("ww",row1.date)
- For day of the year(1-366)
: TalendDate.formatDate("DDD",row1.date)
- 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.
Comments
Post a Comment