我重新启动了GreenPlum DBA指南。它讨论了使用STX转换处理XML消息。
如果我们有XSD,我们需要处理符合XSD的XML消息。
我们如何在GreenPlum中处理这种情况?
最好的方法是什么?
在Greenplum中是否还有其他方法可以处理基于XSD的XML?
感谢您的见解。
答案 0 :(得分:0)
通常,在Greenplum中没有用于处理XML文件的内置功能。您正在谈论的是gpfdist客户端转换,它允许您将XML转换为CSV,然后在数据库内处理它。
处理Greenplum内部XML的最佳选择是将它们分解为关系模型并作为一组表加载。如果只需处理一般的XML文件,可以编写一个PL / Java或PL / Python函数,为您解析XML并提取所需的信息。
以下是该功能的一个示例:
create or replace function parse (inxml varchar, node varchar) returns varchar as $BODY$
from xml.etree import ElementTree
def getText (node, subnode):
res = None
if node is not None:
sn = node.find(subnode)
if sn is not None:
res = sn.text
else:
for el in node:
res2 = getText(el, subnode)
if res2 is not None:
res = res2
break
return res
et = ElementTree.fromstring(inxml.strip())
return getText(et, node)
$BODY$
language plpythonu
volatile;
这是一个调用的例子:
select parse (
'<?xml version="1.0" encoding="utf-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Dont forget me this weekend!</body>
<attachments>
<attachment>file1</attachment>
<attachment>file2</attachment>
<attachment>file3</attachment>
</attachments>
</note>',
'attachment');