[Solved-1 Solution] Importing a common constant declared pig file in other pig files ?



Problem:

How to import a common constant declared file in other pig files ?

Solution 1:

  • The IMPORT keyword is used to import macros, not constants. %declare and %default are preprocessor statements, and its scope is all the remaining lines in the script.
  • If we declare it in a script, but import it from a different one, it will not work because it is out of scope.
  • Both statements are valid in a macro, as long as we use the declared variable inside the macro. If we need to define constants outside the script for modularity.

we need to use a parameter file like below:

ACTIVE_VALUES = 'UK'
  • Then we have to run the pig script
pig -param_file your_params_file.properties -f your_script.pig
  • If we really want to use IMPORT, we could create a macro which takes care of the filtering with that constant value:
%declare ACTIVE_VALUES 'UK';

DEFINE my_custom_filter(A) RETURNS B {
   $B = FILTER $A BY $0 == '$ACTIVE_VALUES ';
};
  • We can use macro instead of calling filter function
IMPORT 'macro.pig';
A = LOAD 'a.csv' using PigStorage(',') AS (country_code:chararray, country_name:chararray);
B = my_custom_filter(A);
dump B;

Related Searches to Importing a common constant declared pig file in other pig files