Разбор XML данных в Hive, когда в теге мало элементов, а нет в другом теге - PullRequest
0 голосов
/ 06 августа 2020

Ниже приведен фрагмент XML:

<employment source="file">
<employer>
    <unparsed>EMPLOYER-2</unparsed>
</employer>
<fileDate>2020-07-21</fileDate>
<effDate>2020-07-21</effDate>
</employment>
<employment source="file">
<employer>
    <unparsed>EMPLOYER-1</unparsed>
</employer>
<occupation>NURSE</occupation>
<hiredDate>2006-09-01</hiredDate>
<fileDate>2015-08-07</fileDate>
<effDate>2015-08-07</effDate>
</employment>

В приведенном выше коде XML содержит информацию о занятости. Информация указана для двух работодателей, но теги по занятости не совпадают. Пример: у сотрудника 2 нет информации о дате найма и профессии, а у работодателя 1 есть. Требуется logi c для извлечения данных из файла XML в следующем формате:

Я пробовал использовать Explode и Posexplode, но безуспешно.

1 Ответ

0 голосов
/ 06 августа 2020

приведенные ниже шаги могут быть полезны,

  1. используйте hivexmlserde из Maven https://mvnrepository.com/artifact/com.ibm.spss.hive.serde2.xml/hivexmlserde

  2. скопируйте ввод xml файл для куста внешней таблицы, указывающей на hdfs

hadoop fs -copyFromLocal emp.xml /stackoverflow/data/hive/dwh/employee

добавьте xml serde jar в терминал улья, создайте ddl и действуйте, как показано ниже.
add jars file:///home/sathya/Downloads/hivexmlserde-1.0.5.3.jar;
Added [file:///home/sathya/Downloads/hivexmlserde-1.0.5.3.jar] to class path
Added resources: [file:///home/sathya/Downloads/hivexmlserde-1.0.5.3.jar]


CREATE EXTERNAL TABLE employee (
`employer_name` string,
`occupation` string,
`hiredDate` string,
`fileDate` string,
`effDate` string
)
ROW FORMAT SERDE 'com.ibm.spss.hive.serde2.xml.XmlSerDe'
WITH SERDEPROPERTIES (
"column.xpath.employer_name"="/employment/employer/unparsed/text()",
"column.xpath.occupation"="/employment/occupation/text()",
"column.xpath.hiredDate"="/employment/hiredDate/TillNo/text()",
"column.xpath.fileDate"="/employment/fileDate/text()",
"column.xpath.effDate"="/employment/effDate/text()"
)
STORED AS INPUTFORMAT 'com.ibm.spss.hive.serde2.xml.XmlInputFormat'
    OUTPUTFORMAT 
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat' 
    LOCATION 'hdfs://localhost:9000/stackoverflow/data/hive/dwh/employee'
    TBLPROPERTIES (
    "xmlinput.start"="<employment","xmlinput.end"="</employment>"
);

select * from employee;

EMPLOYER-2  NULL    NULL    2020-07-21  2020-07-21
EMPLOYER-1  NURSE   NULL    2015-08-07  2015-08-07

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...