暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 将CSV加载到表中并将列拆分为不同的行

askTom 2017-07-22
339

问题描述

我必须将csv文件数据加载到oracle中的表中。csv文件的一列具有4000多个字符的数据,该字符具有许多值。这些值可以通过关键字 “http” 或 “https” 来区分。我需要将它们拆分为oracle表中的不同行。

csv文件中一行的示例:
abc,123,http://test1 https://test2 http://test3 https://test4

注意: csv中的最后一列值可以有4000多个字符

当我把这个文件加载到一个表中,它应该存储如下:

col1 col2 col3
abc 123http://test1
abc 123https://test2
abc 123http://test3
abc 123 https://test4


专家解答

外部表非常适合此。这是我的平面文件

col1a,col2a,http://link10 http://link20 http://link30 http://link40
col1b,col2b,http://link11 http://link21 http://link31 http://link41
col1c,col2c,http://link12 http://link22 http://link32 http://link42
col1d,col2d,http://link13 http://link23 http://link33 http://link43
复制


这是我们使用外部表通过SQL向我们公开的方式,之后,一个简单的横向将解决问题

SQL> CREATE TABLE links (
  2    col1 VARCHAR2(20),
  3    col2 varchar2(20),
  4    col3 varchar2(32767)
  5  )
  6  ORGANIZATION EXTERNAL (
  7    TYPE ORACLE_LOADER
  8    DEFAULT DIRECTORY temp
  9    ACCESS PARAMETERS (
 10      RECORDS DELIMITED BY NEWLINE
 11      FIELDS TERMINATED BY ','
 12      MISSING FIELD VALUES ARE NULL
 13      (
 14    col1 char(20),
 15    col2 char(20),
 16    col3 char(32767)
 17      )
 18    )
 19    LOCATION ('links.csv')
 20  )
 21  REJECT LIMIT UNLIMITED;

Table created.

SQL>
SQL> col col3 format a70
SQL> select * from links;

COL1                 COL2                 COL3
-------------------- -------------------- ----------------------------------------------------------
col1a                col2a                http://link10 http://link20 http://link30 http://link40
col1b                col2b                http://link11 http://link21 http://link31 http://link41
col1c                col2c                http://link12 http://link22 http://link32 http://link42
col1d                col2d                http://link13 http://link23 http://link33 http://link43


5 rows selected.

SQL>
SQL> select col1,
  2         col2,
  3       substr( ' '||col3||' ' ,
  4                instr( ' '||col3||' ' , ' ' ,1,x)+ length(' ') ,
  5                instr( ' '||col3||' ' , ' ' ,1,x+1) -
  6                instr( ' '||col3||' ' , ' ' ,1,x) -length(' ')) link
  7     from links,
  8        lateral( select level x
  9                from dual
 10                connect by level <= (length(col3) - length(replace(col3,' ')))/length(' ') +1)
 11   /

COL1                 COL2                 LINK
-------------------- -------------------- ------------------------------
col1a                col2a                http://link10
col1a                col2a                http://link20
col1a                col2a                http://link30
col1a                col2a                http://link40
col1b                col2b                http://link11
col1b                col2b                http://link21
col1b                col2b                http://link31
col1b                col2b                http://link41
col1c                col2c                http://link12
col1c                col2c                http://link22
col1c                col2c                http://link32
col1c                col2c                http://link42
col1d                col2d                http://link13
col1d                col2d                http://link23
col1d                col2d                http://link33
col1d                col2d                http://link43
复制

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论