如何使用 sqlldr 命令将 XML 数据导入 Oracle 数据库 table

How to Import XML data into Oracle database table using sqlldr command

我正在尝试将 XML 数据导入 Oracle 数据库,但出现如下所述的错误

创建了 table:

CREATE TABLE fv_xml_12 (
   id    NUMBER,
   fv  XMLTYPE)
   XMLTYPE fv STORE AS OBJECT RELATIONAL
      XMLSCHEMA "http://www.example.com/fvInteger.xsd" 
      ELEMENT "FeatureVector";

创建加载器:

options (errors=9999999, rows=5)
load data
 characterset WE8MSWIN1252
infile '/home/alucard/fv_data_cophir/loader_pbf/semiestruturado/12dim/FeatureVectors_XML.txt'
 badfile '/home/alucard/fv_data_cophir/loader_pbf/semiestruturado/12dim/FeatureVectors.bad'
 discardfile '/home/alucard/fv_data_cophir/loader_pbf/semiestruturado/12dim/FeatureVectors.dsc'
 into table fv_xml_12
 fields terminated by " "
( id, fv)

数据:

1 <FeatureVector><feature>11</feature><feature>50</feature><feature>19</feature><feature>27</feature><feature>8</feature><feature>13</feature><feature>17</feature><feature>22</feature><feature>25</feature><feature>9</feature><feature>9</feature><feature>20</feature></FeatureVector>
2 <FeatureVector><feature>13</feature><feature>34</feature><feature>31</feature><feature>7</feature><feature>18</feature><feature>8</feature><feature>20</feature><feature>15</feature><feature>12</feature><feature>10</feature><feature>19</feature><feature>19</feature></FeatureVector>

sqlldr 命令:

sqlldr user/pass control=imp_fv_12dim.ctl

但我收到错误...

Record 1: Rejected - Error on table FV_XML_12, column FV. Field in data file exceeds maximum length Record 2: Rejected - Error on table FV_XML_12, column FV. Field in data file exceeds maximum length

怎么了?

您没有为 fv 字段指定数据类型,因此它默认为 CHAR;和 from the documentation:

For a delimited CHAR field for which no length is specified, the default is 255 bytes.

您的 XML 值大约为 280 个字符,因此超出了该限制。您可以通过指定更大的尺寸来避免错误:

...
fields terminated by " "
( id, fv char(4000) )

在 11gR2 中测试;没有大小我得到相同的错误,指定大小的两行加载成功。 (虽然我创建了没有 store as 的 table,因为我没有你的 .xsd。)但是它在 12c 中应该完全一样。

另请参阅 Load XMLType Data Using SQL*Loader 了解其他方法,例如将每个 XML 放入单独的文件中。