如何在pig中使用xpath提取xml属性?

xwmevbvl  于 2021-06-24  发布在  Pig
关注(0)|答案(1)|浏览(328)

我想用pig拉丁语从xml中提取属性。
这是xml文件的一个示例

<CATALOG>
<BOOK>
<TITLE test="test1">Hadoop Defnitive Guide</TITLE>
<AUTHOR>Tom White</AUTHOR>
<COUNTRY>US</COUNTRY>
<COMPANY>CLOUDERA</COMPANY>
<PRICE>24.90</PRICE>
<YEAR>2012</YEAR>
</BOOK>
</CATALOG>

我用了这个脚本,但没用:

REGISTER ./piggybank.jar
DEFINE XPath org.apache.pig.piggybank.evaluation.xml.XPath();

A =  LOAD './books.xml' using org.apache.pig.piggybank.storage.XMLLoader('BOOK') as (x:chararray);

B = FOREACH A GENERATE XPath(x, 'BOOK/TITLE/@test'), XPath(x, 'BOOK/PRICE');
dump B;

输出为:

(,24.90)

我希望有人能帮我。谢谢。

thigvfpy

thigvfpy1#

piggybank的xpath类中有两个错误:
ignorenamespace逻辑中断了对xml属性的搜索https://issues.apache.org/jira/browse/pig-4751
IgnoreNamespace参数默认为true,无法覆盖https://issues.apache.org/jira/browse/pig-4752
下面是我使用xpathall的解决方法:

XPathAll(x, 'BOOK/TITLE/@test', true, false).$0 as (test:chararray)

如果仍然需要忽略名称空间:

XPathAll(x, '//*[local-name()=\'BOOK\']//*[local-name()=\'TITLE\']/@test', true, false).$0 as (test:chararray)

相关问题