hadoop - 从文本文件将多行xml数据加载到Hive表中

标签 hadoop hive newline multiline

我在MSSQL中的表具有如下架构:(id bigint, xmldata xml, other bigint)我想创建与SQL表相同的Hive表。通过使用Sqoop,我在HDFS上获得了原始数据文件,例如:

1  5678|<root><l1><l2><productid>1234</productid><description>Just to show</description></l2></l1></root>|1002020
2  5679|<root><l1><l2><productid>1239</productid><description>Just to show</description></l2></l1></root>|4213212
3  5680||112345
4  ....
8  5688|<root><l1><l2><productid>1248</productid><description>Just
9  to 
10 show
11 </description></l2></l1></root>|12391023
12 5689|<root><l1><l2><productid>1259</productid><description>Just to
13 show</description></l2></l1></root>|12391021

第一个数字1,2,3是行号。我使用|分隔列。如您所见,看到一些xml字段跨越多行。

我的问题是:如何创建配置单元表并加载此原始数据?

我已经阅读了有关SO的相关问题,但就我而言没有人问津。
我试过了:CREATE TABLE test (id Bigint, xmldata String, other Bigint) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|';Load data inpath 'path/raw' into table test
Hive表不正确,例如:
5678    <root><l1><l2><productid>1234</productid><description>Just to show</description></l2></l1></root>   1002020
5679    <root><l1><l2><productid>1239</productid><description>Just to show</description></l2></l1></root>   4213212
5680        112345
5688    <root><l1><l2><productid>1248</productid><description>Just  NULL
NULL    NULL    NULL
NULL    NULL    NULL
NULL    12391023    NULL
5689    <root><l1><l2><productid>1259</productid><description>Just to   NULL
NULL    12391021    NULL

更新:

尝试过:CREATE TABLE ts_test (id String, xmldata String, other String) ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe' WITH SERDEPROPERTIES ( "input.regex" = "^([^\|]*)[\|]([^\|]*)[\|]([^\|]*)$", "output.format.string" = "%1$s %2$s %3$s" );Load data inpath 'path/test.xml' into table ts_test;
输出表不正确,例如:
5678    <root><l1><l2><productid>1234</productid><description>Just to show</description></l2></l1></root>   1002020
5679    <root><l1><l2><productid>1239</productid><description>Just to show</description></l2></l1></root>   4213212
5680        112345
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL

最佳答案

您需要一个自定义的InputFormat。与其重复,不如看一下
Using FileFormat v Serde to read custom text files

关于hadoop - 从文本文件将多行xml数据加载到Hive表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24815464/

相关文章:

hadoop - 在哪里可以找到安装和运行 cascading.jruby 的教程?

java - 如何读取和写入多个子文件夹?

Hive 始终以本地模式运行映射作业

regex - Hive RegexSerDe多行日志匹配

python - 任何用于并行和分布式任务的 python 库?

hadoop - MapReduce不会减少?

Python 将换行符写入文件给出了 ਍一洀攀ऀ䄀爀碗碗猀琀ഀ 而不是换行符

sql - SQL Server Management Studio 中 'go' 附近的语法不正确

amazon-s3 - 为什么一个hive查询的结果会拆分成多个文件

php mail ()/r/n 和 <br> 没有效果