[Solved-2 Solutions] How to call a pig script within another pig script ?



Problem:

How to call a pig script within another pig script ?

Solution 1:

  • Use the exec command to run a Pig script with no interaction between the scriptand the Grunt shell (batch mode). Aliases defined in the script are not available to the shell; however, the files produced as the output of the script and stored on the system are visible after the script is run
  • Pig gives run and exec commands to tackle your requirement.
  • exec command is there for calling a pig script that is inependent and a standalone run. run command is there for running a pigscipt and preserve its variables and aliases.

Solution 2:

  • For a long time in Pig Latin, the entire script needed to be in one file. This produced some rather unpleasant multithousand-line Pig Latin scripts.
  • Starting in 0.9, the preprocessor can be used to include one Pig Latin script in another. Taken together with the macros, it is now possible to write modular Pig Latin that is easier to debug and reuse: import is used to include one Pig Latin script in another:

Example:

import '../examples/ch6/dividend_analysis.pig';
daily = load 'NYSE_daily' as (exchange:chararray, symbol:chararray,
date:chararray, open:float, high:float, low:float, close:float,
volume:int, adj_close:float);
results = dividend_analysis(daily, '2009', 'symbol', 'open', 'close');
  • Import writes the imported file directly into your Pig Latin script in place of the import statement.
  • In the preceding example, the contents of dividend_analysis.pig will be placed immediately before the load statement. Note that a file cannot be imported twice.
  • If we wish to use the same functionality multiple times, you should write it as a macro and import the file with that macro.

Related Searches to How to call a pig script within another pig script