hadoop - 用 Pig 分割字符串

标签 hadoop apache-pig

我有一个以下格式的字符串:

2011 年 7 月 9 日星期六 05:38:24 GMT

我会得到这样的输出:

2011 年 7 月 9 日 05:38:24

谢谢。

[编辑] 我尝试了很多解决方案,但都遇到了错误。我将重新解释一下问题。我有一个 XML 文件,其中有一个节点:Tue, 05 Jul 2011 10:10:30 GMT,我想从中提取两个单独的字符串,如上所示。 我已经尝试过这段代码:

register /usr/lib/pig/piggybank.jar; 
items = LOAD ' depeche/2011_7_10_12_30_rss.txt' USING org.apache.pig.piggybank.storage.XMLLoader('item') AS  (item:chararray); 
source_name = FOREACH items GENERATE REGEX_EXTRACT(item, '<link>(.*)</link>', 1) AS  link:chararray, 
REGEX_EXTRACT(item, '<title>(.*)</title>', 1) AS  title:chararray, 
REGEX_EXTRACT(item, '<description>(.*)</description>',  1) AS description:chararray, 
REGEX_EXTRACT(item, '<pubDate>(.*)</pubDate>', 1) AS  pubdate:chararray, 
sortie = FOREACH pubdate GENERATE SUBSTRING((chararray)$0, 4, 25);
illustrate sortie;

错误:

[main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: <line 21, column 333>         mismatched input '=' expecting SEMI_COLON

最佳答案

编辑答案:

这个例子更清楚一点......我捕获了一个 RSS feed 例子,并做了一个快速测试。下面的代码使用包含上面示例中的所有元素的示例。不过,我使用 REGEX_EXTRACT 而不是 SUBSTRING 来获取发布日期。

--rss.pig

REGISTER piggybank.jar

items = LOAD 'rss.txt' USING org.apache.pig.piggybank.storage.XMLLoader('item') AS  (item:chararray);

data = FOREACH items GENERATE REGEX_EXTRACT(item, '<link>(.*)</link>', 1) AS  link:chararray, 
REGEX_EXTRACT(item, '<title>(.*)</title>', 1) AS  title:chararray,
REGEX_EXTRACT(item, '<description>(.*)</description>',  1) AS description:chararray,
REGEX_EXTRACT(item, '<pubDate>.*(\\d{2}\\s[a-zA-Z]{3}\\s\\d{4}\\s\\d{2}:\\d{2}:\\d{2}).*</pubDate>', 1) AS  pubdate:chararray;

dump data;

--rss.txt

<rss version="2.0">
   <channel>
      <title>News</title>
      <link>http://www.hannonhill.com</link>
      <description>Hannon Hill News</description>
      <language>en-us</language>
      <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
      <generator>Cascade Server</generator>
      <webMaster><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f3e1e6e9e5f7f0e1f6c4ece5eaeaebeaecede8e8aae7ebe9" rel="noreferrer noopener nofollow">[email protected]</a></webMaster>
      <item>
         <title>News Item 1</title>
         <link>http://www.hannonhill.com/news/item1.html</link>
         <description>Description of news item 1 here.</description>
         <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
         <guid>http://www.hannonhill.com/news/item1.html</guid>
      </item>
      <item>
         <title>News Item 2</title>
         <link>http://www.hannonhill.com/news/item2.html</link>
         <description>Description of news item 2 here.</description>
         <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
         <guid>http://www.hannonhill.com/news/item2.html</guid>
      </item>
      <item>
         <title>News Item 3</title>
         <link>http://www.hannonhill.com/news/item3.html</link>
         <description>Description of news item 3 here.</description>
         <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
         <guid>http://www.hannonhill.com/news/item3.html</guid>
      </item>
   </channel>
</rss>

rss.pig 的结果:

(http://www.hannonhill.com/news/item1.html,News Item 1,Description of news item 1 here.,03 Jun 2003 09:39:21)
(http://www.hannonhill.com/news/item2.html,News Item 2,Description of news item 2 here.,30 May 2003 11:06:42)
(http://www.hannonhill.com/news/item3.html,News Item 3,Description of news item 3 here.,20 May 2003 08:56:02)




原始答案:

有多种方法可以在这里使用,因此我将介绍两种:SUBSTRINGREGEX_EXTRACT

如果您的字符串长度是恒定的,那么您可以使用内置 SUBSTRING功能。可以将其想象为 Linux 中的 cut 命令。

OUTPUT = FOREACH INPUT GENERATE SUBSTRING((chararray)$0, 4, 25);

否则,您可以使用内置 REGEX_EXTRACT拉动您正在寻找的绳子。根据示例,我想到的最简单的正则表达式匹配是以第一个数字开始字符串,以最后一个数字结束,捕获中间的所有字符。

输出 = FOREACH 输入生成 REGEX_EXTRACT((chararray)$0, '([\d].*[\d])', 1);

关于hadoop - 用 Pig 分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22337820/

相关文章:

hadoop - 如何查看hadoop上安装的pig版本

Hadoop Pig 作业未运行

hadoop - 运行 PIG 脚本时出错

user-defined-functions - 使用分布式缓存将文件名从 Pig 传递给 Java UDF

hadoop - 从表中创建 json

java - 对值进行两次迭代 (MapReduce)

java - 从 Hadoop YARN 启动一个 java 应用程序

java - 分布式3D渲染

hadoop - 无法从 hive 加载 hbase 表中的数据

java - Oozie Java操作错误java.lang.NoClassDefFoundError:org/apache/poi/xwpf/usermodel/XWPFDocument