简体   繁体   English

Oracle PL-SQL:将多个定界文件导入表

[英]Oracle PL-SQL : Import multiple delimited files into table

I have multiple files (f1.log, f2.log, f3.log etc) Each file has the data in ; 我有多个文件(f1.log,f2.log,f3.log等),每个文件中都有数据; & = delimited format. =分隔格式。 (new lines are delimited by ; and fields are delimited by = ) eg (新行由;分隔,字段由=分隔)例如

data of f1: f1的数据:

1=a;2=b;3=c

data of f2: f2的数据:

1=p;2=q;3=r

I need to read all these files and import data into table in format: 我需要读取所有这些文件并将数据以以下格式导入表:

filename  number  data

f1        1       a

f1        2       b

f1        3       c

f2        1       p
[...]

I am new to SQL. 我是SQL新手。 Can you please guide me, how can do it? 您能指导我,怎么做?

Use SQL*Loader to get the files into a table. 使用SQL * Loader将文件放入表中。 Assuming you have a table created a bit like: 假设您创建了一个表,如下所示:

create table FLOG
(
  FILENAME   varchar2(1000)
 ,NUM        varchar2(1000)
 ,DATA       varchar2(1000)
);

Then you can use the following control file: 然后,您可以使用以下控制文件:

LOAD DATA
INFILE 'f1.log' "str ';'"
truncate INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
   filename constant 'f1'
   ,num  char 
   ,data char 
)

However, you will need a different control file for each file. 但是,每个文件都需要一个不同的控制文件。 This can be done by making the control file dynamically using a shell script. 这可以通过使用Shell脚本动态制作控制文件来完成。 A sample shell script can be: 一个示例shell脚本可以是:

cat >flog.ctl <<_EOF
LOAD DATA
INFILE '$1.log' "str ';'"
APPEND INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
filename constant '$1'
,num  char
,data char
)
_EOF

sqlldr <username>/<password>@<instance> control=flog.ctl data=$1.log

Saved as flog.sh it can then be run like: flog.shflog.sh即可运行:

./flog.sh f1
./flog.sh f2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM