我有一个巨大的xml文件,除非我将它导入数据库,否则无法打开它。我要用Postgres来做这个。我有一个与这个数据文件相匹配的模式。列太多了,所以我想自动从这个模式创建一个表,然后从我的计算机上的本地驱动器导入数据文件来填充这个表。我该怎么做?我看到了很多答案,但一直未能正确理解这一点。此外,我没有超级用户的权利,所以将不得不围绕这一点。
下面是模式文件的样子:
> <?xml version="1.0" encoding="UTF-8"?> <xs:schema
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified"
> targetNamespace="http://www.drugbank.ca" xmlns="http://www.drugbank.ca">
> <xs:element name="drugbank" type="drugbank-type">
> <xs:annotation>
> <xs:documentation>This is the root element for the DrugBank database schema. DrugBank is a database on drug and
> drug-targets.</xs:documentation>
> </xs:annotation>
> </xs:element>
> <xs:complexType name="drugbank-type">
> <xs:annotation>
> <xs:documentation>This is the root element type for the DrugBank database schema.</xs:documentation>
> </xs:annotation>
> <xs:sequence>
> <xs:element name="drug" type="drug-type" maxOccurs="unbounded"/>
> </xs:sequence>
> <xs:attribute name="version" type="xs:string" use="required">
> <xs:annotation>
> <xs:documentation>The DrugBank version for the exported XML file.</xs:documentation>
> </xs:annotation>
> </xs:attribute>
> <xs:attribute name="exported-on" type="xs:date" use="required">
> <xs:annotation>
> <xs:documentation>The date the XML file was exported.</xs:documentation>
> </xs:annotation>
> </xs:attribute>
> </xs:complexType>
> <xs:complexType name="drug-type">
> <xs:sequence>
> <xs:element maxOccurs="unbounded" minOccurs="1" name="drugbank-id"
> type="drugbank-drug-salt-id-type"> </xs:element>
> <xs:element name="name" type="xs:string"/>
> <xs:element name="description" type="xs:string"/>
> <xs:element name="cas-number" type="xs:string"/>
> <xs:element name="unii" type="xs:string"/>
> <xs:element name="average-mass" type="xs:float" minOccurs="0"/>
> <xs:element name="monoisotopic-mass" type="xs:float" minOccurs="0"/>
> <xs:element name="state" type="state-type" minOccurs="0"/>
> <xs:element name="groups" type="group-list-type"/>
> <xs:element name="general-references" type="reference-list-type"/>
> <xs:element name="synthesis-reference" type="xs:string"/>
> <xs:element name="indication" type="xs:string"/>
> <xs:element name="pharmacodynamics" type="xs:string"/>
> <xs:element name="mechanism-of-action" type="xs:string"/>
> <xs:element name="toxicity" type="xs:string"/>
> <xs:element name="metabolism" type="xs:string"/>
> <xs:element name="absorption" type="xs:string"/>
> <xs:element name="half-life" type="xs:string"/>
> <xs:element name="protein-binding" type="xs:string"/>
> <xs:element name="route-of-elimination" type="xs:string"/>
> <xs:element name="volume-of-distribution" type="xs:string"/>
> <xs:element name="clearance" type="xs:string"/>
> <xs:element name="classification" type="classification-type" minOccurs="0"/>
> <xs:element name="salts" type="salt-list-type"/>
> <xs:element name="synonyms" type="synonym-list-type"/>
> <xs:element name="products" type="product-list-type"/>
> <xs:element name="international-brands" type="international-brand-list-type"/>
> <xs:element name="mixtures" type="mixture-list-type"/>
> <xs:element name="packagers" type="packager-list-type"/>
> <xs:element name="manufacturers" type="manufacturer-list-type"/>
> <xs:element name="prices" type="price-list-type"/>
> <xs:element name="categories" type="category-list-type"/>
> <xs:element name="affected-organisms" type="affected-organism-list-type"/>
> <xs:element name="dosages" type="dosage-list-type"/>
> <xs:element name="atc-codes" type="atc-code-list-type"/>
> <xs:element name="ahfs-codes" type="ahfs-code-list-type"/>
> <xs:element name="pdb-entries" type="pdb-entry-list-type"/>
> <xs:element name="fda-label" type="xs:anyURI" minOccurs="0"/>
> <xs:element name="msds" type="xs:anyURI" minOccurs="0"/>
> <xs:element name="patents" type="patent-list-type"/>
> <xs:element name="food-interactions" type="food-interaction-list-type"/>
> <xs:element name="drug-interactions" type="drug-interaction-list-type"/>
> <xs:element minOccurs="0" name="sequences" type="sequence-list-type"/>
> <xs:element minOccurs="0" name="calculated-properties" type="calculated-property-list-type"/>
> <xs:element name="experimental-properties" type="experimental-property-list-type"/>
> <xs:element name="external-identifiers" type="external-identifier-list-type"/>
> <xs:element name="external-links" type="external-link-list-type"/>
> <xs:element name="pathways" type="pathway-list-type"/>
> <xs:element name="reactions" type="reaction-list-type"/>
> <xs:element name="snp-effects" type="snp-effect-list-type"/>
> <xs:element name="snp-adverse-drug-reactions" type="snp-adverse-drug-reaction-list-type"/>
> <xs:element name="targets" type="target-list-type"/>
> <xs:element name="enzymes" type="enzyme-list-type"/>
> <xs:element name="carriers" type="carrier-list-type"/>
> <xs:element name="transporters" type="transporter-list-type"/>
> </xs:sequence>这只是其中的一部分。这是个很大的文件。任何形式的帮助/指导都是非常感谢的。
发布于 2018-04-16 17:52:10
可能有上千种将XML文件导入PostgreSQL的方法,但我发现另一种方法很容易实现,并且已经用大型xml文档(120GB+)进行了测试。
根据XML文件的大小,考虑拆分它。这样做的一个很好的工具是xml。此命令将file.xml拆分为最大为100 in的较小文件:
xml_split -n 5 -l 1 -s 100MB file.xml一旦将文件按合理的大小分割,就可以开始导入它们,而不会有耗尽内存的风险。
让我们考虑以下XML文件结构..。
<?xml version="1.0"?>
<t>
<foo>
<id j="a">1</id>
<val>bar1</val>
</foo>
<foo>
<id j="b">8</id>
<val>bar1</val>
</foo>
<foo>
<id j="c">5</id>
<val>bar1</val>
</foo>
<foo>
<id j="b">2</id>
</foo>
</t>..。以及下面的目标表,我们将在其中插入XML记录。
CREATE TABLE t (id TEXT, entry XML);下面的代码将XML文件导入临时的unlogged表,并使用节点<foo>的CTE (也称为WITH子句)将它们导入表t中。命令perl -pe 's/\n/\\n/g'用\\n替换换行符,这样就不会得到Premature end of data异常:
#!/bin/bash
psql testdb -c "CREATE UNLOGGED TABLE tmp (entry xml);"
for f in /path/to/your/files/;do
cat $f | perl -pe 's/\n/\\n/g' |psql testdb -c "COPY tmp FROM STDIN;"
psql testdb -c "
WITH j AS (
SELECT UNNEST(XPATH('//t/foo',entry)) AS entry FROM tmp
)
INSERT INTO t
SELECT XPATH('//foo/id/text()',j.entry),j.entry FROM j;
TRUNCATE TABLE tmp;"
done
psql testdb -c "DROP TABLE tmp;"这是你的数据:
testdb=# SELECT * FROM t;
id | entry
-----+--------------------------
{1} | <foo> +
| <id j="a">1</id>+
| <val>bar1</val> +
| </foo>
{8} | <foo> +
| <id j="b">8</id>+
| <val>bar1</val> +
| </foo>
{5} | <foo> +
| <id j="c">5</id>+
| <val>bar1</val> +
| </foo>
{2} | <foo> +
| <id j="b">2</id>+
| </foo>
(4 Zeilen)https://stackoverflow.com/questions/49862106
复制相似问题