在猪中逃脱美元符号? [英] Escaping a Dollar Sign in Pig?
问题描述
在0.9.2中这不是问题,但在0.10中,当我尝试访问其中带有美元符号的映射中的键时,我为自己未定义变量的错误而感到困惑.具体来说:
This wasn't a problem in 0.9.2, but in 0.10, when I try to access a key in a map that has a dollar sign in it, I get hammered with errors that I haven't defined the variable. Specifically:
blah = FOREACH meh GENERATE source, json_post_id#'$id' AS post_id;
返回
Undefined parameter : id
这很好并且很有意义,但是当我将其修改为:
That's fine and makes sense, but when I amend it to:
blah = FOREACH meh GENERATE source, json_post_id#'\$id' AS post_id;
我得到:
Unexpected character '$'
想法?
忘了提及:尝试过2个反斜杠和3个反斜杠.没有骰子. [/编辑]
Forgot to mention: have tried with 2 backslashes and 3 backslashes as well. No dice. [/Edit]
推荐答案
-
基于对邮件存档的回复发布,看起来行为在使用Grunt shell并将其作为脚本运行时会有所不同."
Based on a response to your Mail Archive Posting, it looks like the behavior will be "different when using Grunt shell and running it as a script."
输入文件
cheolsoo@localhost:~/workspace/pig-svn $cat 1.txt $id,a
硬壳
没有反斜杠的$
起作用:
grunt> A = LOAD '1.txt' USING PigStorage(',') AS (k:chararray,
v:chararray); grunt> B = FOREACH A GENERATE TOMAP(k, v) AS M; grunt> C
= FOREACH B GENERATE M#'$id'; grunt> DUMP C; (a)
脚本
带有单个反斜杠的$
起作用:
The $
with a single backslash works:
cheolsoo@localhost:~/workspace/pig-svn $cat test.pig A = LOAD '1.txt'
USING PigStorage(',') AS (k:chararray, v:chararray); B = FOREACH A
GENERATE TOMAP(k, v) AS M; C = FOREACH B GENERATE M#'\$id'; DUMP C;
cheolsoo@localhost:~/workspace/pig-svn $./bin/pig -x local test.pig
(a)
此外,来自>带分割字符串(STRSPLIT)的猪问题,您是否尝试过以下任何一种方法?
Also, from Pig problem with split string(STRSPLIT), have you tried either of the following.
-
正确地转义了字符
u0024
.使用单引号或双引号测试单引号,以查看是否有区别. 此答案表明单引号会有所不同,但值得一提.
Correctly escaping the character
u0024
. Test single using single or double quotes to see if that makes a difference. This answer shows that the single quote makes a difference which you have but it's worth mentioning.
或者,尽管相关,但将循环分成一个块.
Alternatively, although related, break the loop into a block.
blah = FOREACH meh {
GENERATE source, json_post_id#'$id' AS post_id;
}
您似乎正在连接id
.确保您不打算使用CONCAT()
. http://pig.apache.org/docs/r0.10.0/func.html#concat
It looks like you are concatenating an id
. Make sure you are not suppose to be using CONCAT()
. http://pig.apache.org/docs/r0.10.0/func.html#concat
根据Class PigStorage
文档(Pig 0.10.0 API):
According to the Class PigStorage
documentation (Pig 0.10.0 API):
一个加载函数,它使用字符定界符将输入的行解析为字段.默认的分隔符是制表符.您可以将任何字符指定为文字("a"),已知的转义字符("\ t")或dec或hex值("\ u001","\ x0A").
A load function that parses a line of input into fields using a character delimiter. The default delimiter is a tab. You can specify any character as a literal ("a"), a known escape character ("\t"), or a dec or hex value ("\u001", "\x0A").
这篇关于在猪中逃脱美元符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!