[HOWTO] use the tRowGenerator

Employee

[HOWTO] use the tRowGenerator

At the moment 2006-09-29, this component is not available on the update site yet. So take this howto as a description of a components and give your suggestions of improvement

A new component has arrived just after the availability of the TOS beta 2. His name is tRowGenerator, you can get it with the "update" feature inside TOS.
Basically, tRowGenerator is used as an input stream. It can generate as many rows as you want and as many fields as you want. You can control the number of rows with the "Number of rows" property which must be an integer value. You control the number of fields per row with the number of lines in the "Values" table property. This "Values" table is what needs a deeper introduction.
In each line of the "Values" table, TOS is waiting for a Perl expression corresponding to a list. At each row creation, for each field, the Perl generated script will choose randomly between the values in the given list.
- "'foo', 'bar'" (without surroundning quotes) will result in half of the lines with 'foo' and the other half with 'bar'.
- "1, 2, 3, 4, 5, 6, 7, 8, 9, 10" (without surroundning quotes) will randomly choose a number between 1 and 10 at each field creation
- "1..10" (without surroundning quotes) is exactly the same as previous line, Perl concision added
- "'a'..'z', 'A'..'Z'" (without surroundning quotes) will randomly choose a letter between "a" and "z" lowercase or uppercase.
- "'aa'..'zz'" (without surroundning quotes) will randomly choose a two letters word in lowercase. Indeed, with this expression, Perl automatically creates a list of all two letters words existing in the given range. Be warned that asking more than a three letters word is dangerous because of required memory, as the Perl script first stores all lists in arrays. If you ask a four letters word it would make 456976 possibilities, that's a huge array.
- "sub{++$i_rowgen1}" will return an incremented integer. This is a Perl closure. Be sure to use a unique name for the incremented variable, ie if you have another tRowGenerator on the same job, name your incremented variable $i_rowgen2 for example.
- "sub{getAsciiRandomString(10)}" will return a random string made of 10 ASCII characters. getAsciiRandomString is a function available in talend::misc Perl module.
- "sub{getHexRandomString(32)}" will return a random string made of 32 hexadecimal characters (ie 0-9a-f)
- "sub{getRandomString(5, )}" will return a random string made of 5 consonants. This function looks like the two others but here you can choose the characters.
If you have any suggestion to improve this component, feel free to give your opinion.
Tags (1)

Accepted Solutions
Employee

Re: [HOWTO] use the tRowGenerator

- I need to generate for example a code between 1 and 1000, but i need to have five numbers at least. That means that the number will be between 00001 - 01000.

'00001' .. '01000'

- I'd like to generate a date also. How can i generate a day randomly with a fix year / month ? With for example '2006/11/' + day. I tried : '2006/11/' . 1..30 but it doesn't work.

For this very simple example, you can use:
sub{'2006/11/'.getRandomString(1, )}

A much better solution is to add getRandomDate in your project routine Perl module:
use Time::Local;
sub getRandomDate {
my %params = @_;
my ($year, $month, $day);
($day, $month, $year) = ($params{min} =~ m{(\d{2})/(\d{2})/(\d{4})});
my $min = timelocal(0, 0, 0, $day, $month-1, $year);
($day, $month, $year) = ($params{max} =~ m{(\d{2})/(\d{2})/(\d{4})});
my $max = timelocal(0, 0, 0, $day, $month-1, $year);
my $diff = $max - $min;
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($min + int rand $diff);
return sprintf(
'%02u/%02u/%4u',
$mday,
$mon + 1,
$year + 1900,
);
}
}

... and add getRandomDate to the @EXPORT array in your routines module.
Then in the tRowGenerator, use:
sub{getRandomDate(min => '01/11/2006', max => '30/11/2006')}

All Replies
Employee

Re: [HOWTO] use the tRowGenerator

Hi,
I need to do few things with the tRowGenerator but i don't know how to do exactly.
Can you tell me how to generate 2 things:
- I need to generate for example a code between 1 and 1000, but i need to have five numbers at least. That means that the number will be between 00001 - 01000.
- I'd like to generate a date also. How can i generate a day randomly with a fix year / month ? With for example '2006/11/' + day. I tried : '2006/11/' . 1..30 but it doesn't work.
Thanks
Employee

Re: [HOWTO] use the tRowGenerator

- I need to generate for example a code between 1 and 1000, but i need to have five numbers at least. That means that the number will be between 00001 - 01000.

'00001' .. '01000'

- I'd like to generate a date also. How can i generate a day randomly with a fix year / month ? With for example '2006/11/' + day. I tried : '2006/11/' . 1..30 but it doesn't work.

For this very simple example, you can use:
sub{'2006/11/'.getRandomString(1, )}

A much better solution is to add getRandomDate in your project routine Perl module:
use Time::Local;
sub getRandomDate {
my %params = @_;
my ($year, $month, $day);
($day, $month, $year) = ($params{min} =~ m{(\d{2})/(\d{2})/(\d{4})});
my $min = timelocal(0, 0, 0, $day, $month-1, $year);
($day, $month, $year) = ($params{max} =~ m{(\d{2})/(\d{2})/(\d{4})});
my $max = timelocal(0, 0, 0, $day, $month-1, $year);
my $diff = $max - $min;
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($min + int rand $diff);
return sprintf(
'%02u/%02u/%4u',
$mday,
$mon + 1,
$year + 1900,
);
}
}

... and add getRandomDate to the @EXPORT array in your routines module.
Then in the tRowGenerator, use:
sub{getRandomDate(min => '01/11/2006', max => '30/11/2006')}
Employee

Re: [HOWTO] use the tRowGenerator

Warning: a new version of the tRowGenerator with a dedicated GUI was added in TOS 2.0.0. You can still use the Perl expressions given in this forum topic as custom expression.