使用参数读取包的某个字段

时间:2013-02-14 10:14:29

标签: apache-pig

我正在通过Alan Gates的 Programming Pig 工作,我在脚本中遇到错误,引入了宏的力量:http://ofps.oreilly.com/titles/9781449302641/advanced_pig_latin.html#macros

--macro.pig
-- Given daily input and a particular year, analyze how
-- stock prices changed on days dividends were paid out.
define dividend_analysis (daily, year, daily_symbol, daily_open, daily_close)
returns analyzed {
divs          = load 'NYSE_dividends' as (exchange:chararray,
                    symbol:chararray, date:chararray, dividends:float);
divsthisyear  = filter divs by date matches '$year-.*';
dailythisyear = filter $daily by date matches '$year-.*';
jnd           = join divsthisyear by symbol, dailythisyear by $daily_symbol;
$analyzed     = foreach jnd generate dailythisyear::$daily_symbol,
                    $daily_close - $daily_open;
};

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');

错误是$中双重冒号后的意外dailythisyear::$daily_symbol符号。

如果不通过以下方式替换该行,您将如何解决这个问题:

$analyzed = foreach jnd generate divsthisyear::symbol, $daily_close - $daily_open;

1 个答案:

答案 0 :(得分:2)

参数替换不能在宏内部使用。应将参数显式传递给宏,并且仅在顶层使用参数替换。

您可以使用-dryrun命令行参数来查看宏如何在线扩展。

所以无论如何你不能以这种方式在Macro中使用参数。