是否可以将hcatalog与xml一起使用?--在cloudera虚拟机上进行etl

hzbexzde  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(271)

我正在为一个大数据类开发一个项目,我在本地安装了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

这个文件的绝对大小在虚拟机中会是一个问题吗?最后,我们将在aws中重复一些etl任务,但目前我正在努力避免在不知道如何正确使用某些工具的情况下产生一大笔账单。

xzabzqsa

xzabzqsa1#

xml使用了相当标准化的结构,所以我想看看您的数据格式以及什么分隔符不起作用。
在不了解更多数据/结构等的情况下。。。我可能会这么做:
决定我的模式并手动创建hcatalog(或者脚本化,以最简单的为准)。
使用piggybank xmlloader通过pig加载数据。
使用regex将数据解析到我为hcat决定的模式中
使用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>

相关问题