Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Pig Wiki" for change 
notification.

The following page has been changed by OlgaN:
http://wiki.apache.org/pig/ParameterSubstitution

------------------------------------------------------------------------------
  
  === Parameter Specification ===
  
+ Parameters in pig script will be of the for `$<identifier>`. 
- Parameters in the script will be enclosed in `%`. The content within `%` can 
be either a parameter name in which case it starts with `$` or a command to run 
to generate the value in which case it will be enclosed in backticks.
- 
- '''Example 1''': parameter name specification
  
  {{{
  A = load '/data/mydata/%$date%';
@@ -31, +29 @@

  
  In this case, pig would expect the program to pass a value for parameter 
named `date` and will place it in the path before executing the load statement.
  
- '''Example 2''': command specification
+ In addition to parameters, a user can supply a command to execute to get the 
substitution value. This can be done using `declare` command.
  
  {{{
- A = load '/data/mydata/%`generate_date`%';
+ declare CMD `generate_date`
+ A = load '/data/mydata/$CMD';
  B = filter A by $0>'5';
  .....
  }}}
  
- In this case, pig would execute the program called `generate_date` and place 
its stdout into the path before executing the load statement.
+ In this case, pig would execute the program called `generate_date` when it 
encounters `declare` statement and place its stdout into the path before 
executing the load statement.
  
- '''Example 3''': command taking a parameter
+ A command can take parameters which need to be substituted as well.
  
  {{{
- A = load '/data/mydata/%`generate_name $date`%';
+ declare CMD `generate_date $date`
+ A = load '/data/mydata/$CMD';
  B = filter A by $0>'5';
  .....
  }}}
  
  In this case, parameter `date` is substituted first, then `generate_name` 
command ran passing value of `date` as command line parameter and the stdout of 
the command is placed into the path prior to executing the load statement.
  
- '''Example 4''': command is passed as a parameter:
+ Note that the variables passed on the command line must be resolved prior to 
the declare statement. This is wrong:
  
  {{{
+ declare A `cmd1 $B`
+ declare $B `cmd2`
+ }}}
+ 
+ Command name itself can be a parameter.
+ 
+ {{{
+ declare CMD `$mycmd $date`
- A = load '/data/mydata/%`$cmd $date`%';
+ A = load '/data/mydata/$CMD';
  B = filter A by $0>'5';
  .....
  }}}
  
  In this case, parameters `cmd` and `date` are substituted first. The the 
resulting command is executed and its stdout is placed into the path prior to 
running the load statement.
+ 
+ Note that parameter names are case insesitive and $cmd and $CMD means the 
same thing. This is to match the rest of Pig Latin.
  
  === Parameter Passing ===
  

Reply via email to