1

我正在做一个大数据类的项目,我已经在本地安装了 Cloudera Quickstart VM,以便在我的数据集上运行一些基本任务并熟悉一些工具。我正在学习一个教程,其中涉及将数据集移动到 HDFS,基于数据集文件创建一个 HCatalog 表,然后在表上运行 Hive 和/或 Pig 命令。问题是我的数据是一个大型 XML 文件,HCatalog 中的标准分隔符选项不适用。

有没有办法将 XML 导入 HCatalog?如果不是,在我的 XML 数据集上使用 Hive 或 Pig 的最佳方式是什么?

编辑:我的文件来自公共 StackOverflow 数据集。我正在使用该posts.xml文件。它非常大(25GB),我无法在我的机器上打开它,但下面是根据自述文件的结构:

- **posts**.xml
   - Id
   - PostTypeId
      - 1: Question
      - 2: Answer
   - ParentID (only present if PostTypeId is 2)
   - AcceptedAnswerId (only present if PostTypeId is 1)
   - CreationDate
   - Score
   - ViewCount
   - Body
   - OwnerUserId
   - LastEditorUserId
   - LastEditorDisplayName="Jeff Atwood"
   - LastEditDate="2009-03-05T22:28:34.823"
   - LastActivityDate="2009-03-11T12:51:01.480"
   - CommunityOwnedDate="2009-03-11T12:51:01.480"
   - ClosedDate="2009-03-11T12:51:01.480"
   - Title=
   - Tags=
   - AnswerCount
   - CommentCount
   - FavoriteCount

该文件的绝对大小会成为 VM 中的问题吗?最后,我们将在 AWS 中重复其中的一些 ETL 任务,但现在我正在努力避免在不知道如何正确使用某些工具的情况下支付大笔费用。

4

1 回答 1

3

XML 使用相当标准化的结构,因此我很想看看您的数据格式以及哪些分隔符不起作用。

在不了解数据/结构等的情况下......这可能是我会做的:

  1. 决定我的架构并手动创建 HCatalog(或脚本,以最简单的为准)。
  2. 通过 pig 加载数据,使用 piggybank XMLLoader。
  3. 使用正则表达式将数据解析为我为 HCat 决定的模式
  4. 使用 HCatStore 方法存储它。



--示例代码

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;

STORE data into 'rss_items' USING org.apache.hcatalog.pig.HCatStorer();


validate = LOAD 'default.rss_items' USING org.apache.hcatalog.pig.HCatLoader();
dump validate;



--结果

(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)



-- 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>webmaster@hannonhill.com</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>
于 2014-03-16T16:32:06.977 回答