[英]Issue loading data using SQL*Loader in staging tables then into main database tables
I am trying to load data into our main database table using SQL*Loader. 我正在尝试使用SQL * Loader将数据加载到我们的主数据库表中。 Data will be provided into pipe separated CSV files.
数据将提供到管道分隔的CSV文件中。
I have developed a shell script to load data and it is working fine except one thing. 我已经开发了一个Shell脚本来加载数据,除了一件事之外,它运行良好。
Here are the details and data to re-create the problem. 这是重新创建问题的详细信息和数据。 Staging table structure in which data will be populated using SQL/*Loader:
使用SQL / * Loader填充数据的登台表结构:
create table stg_cmts_data (cmts_token varchar2(30), CMTS_IP varchar2(20));
create table stg_link_data (dhcp_token varchar2(30), cmts_to_add varchar2(200));
create table stg_dhcp_data (dhcp_token varchar2(30), DHCP_IP varchar2(20));
Data in CSV files; CSV文件中的数据; for
stg_cmts_data
in cmts_data.csv
: 对于
stg_cmts_data
中的cmts_data.csv
:
wnlb-cmts-01-1|10.15.0.1
wnlb-cmts-02-2|10.15.16.1
wnlb-cmts-03-3|10.15.48.1
wnlb-cmts-04-4|10.15.80.1
wnlb-cmts-05-5|10.15.96.1
For stg_dhcp_data
in dhcp_map_03092015_1.csv
: 对于
stg_dhcp_data
中的dhcp_map_03092015_1.csv
:
dhcp-1-1-1|10.25.23.10,25.26.14.01
dhcp-1-1-2|56.25.111.25,100.25.2.01
dhcp-1-1-3|25.255.3.01,89.20.147.258
dhcp-1-1-4|10.25.26.36,200.32.58.69
dhcp-1-1-5|80.25.47.369,60.258.14.10
For stg_link_data
in link_data.csv
: 对于
stg_link_data
中的link_data.csv
:
dhcp-1-1-1|wnlb-cmts-01-1,wnlb-cmts-02-2
dhcp-1-1-2|wnlb-cmts-03-3,wnlb-cmts-04-4,wnlb-cmts-05-5
dhcp-1-1-3|wnlb-cmts-01-1
dhcp-1-1-4|wnlb-cmts-05-8,wnlb-cmts-05-6,wnlb-cmts-05-0,wnlb-cmts-03-3
dhcp-1-1-5|wnlb-cmts-02-2,wnlb-cmts-04-4,wnlb-cmts-05-7
wnlb-dhcp-1-13|wnlb-cmts-02-2
Now after loading this data into the staging tables I have to populate the main database tables: 现在,将这些数据加载到暂存表中之后,我必须填充主数据库表:
create table subntwk (subntwk_nm varchar2(20), subntwk_ip varchar2(30));
create table link (link_nm varchar2(50));
SQL scripts which I have created to load data: 我创建的用于加载数据的SQL脚本:
spool load_cmts.log
set serveroutput on
DECLARE
CURSOR c_stg_cmts IS SELECT *
FROM stg_cmts_data;
TYPE t_stg_cmts IS TABLE OF stg_cmts_data%ROWTYPE INDEX BY pls_integer;
l_stg_cmts t_stg_cmts;
l_cmts_cnt NUMBER;
l_cnt NUMBER;
l_cnt_1 NUMBER;
BEGIN
OPEN c_stg_cmts;
FETCH c_stg_cmts BULK COLLECT INTO l_stg_cmts;
FOR i IN l_stg_cmts.FIRST..l_stg_cmts.LAST
LOOP
SELECT COUNT (1)
INTO l_cmts_cnt
FROM subntwk
WHERE subntwk_nm = l_stg_cmts(i).cmts_token;
IF l_cmts_cnt < 1 THEN
INSERT
INTO SUBNTWK
(
subntwk_nm
)
VALUES
(
l_stg_cmts(i).cmts_token
);
DBMS_OUTPUT.PUT_LINE('Token has been added : '||l_stg_cmts(i).cmts_token);
ELSE
DBMS_OUTPUT.PUT_LINE('Token is already present');
END IF;
EXIT WHEN l_stg_cmts.COUNT =0;
END LOOP;
commit;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('ERROR OCCURED ' || SQLERRM);
END;
/
exit
For dhcp
: 对于
dhcp
:
spool load_dhcp.log
set serveroutput on
DECLARE
CURSOR c_stg_dhcp IS SELECT *
FROM stg_dhcp_data;
TYPE t_stg_dhcp IS TABLE OF stg_dhcp_data%ROWTYPE INDEX BY pls_integer;
l_stg_dhcp t_stg_dhcp;
l_dhcp_cnt NUMBER;
l_cnt NUMBER;
l_cnt_1 NUMBER;
BEGIN
OPEN c_stg_dhcp;
FETCH c_stg_dhcp BULK COLLECT INTO l_stg_dhcp;
FOR i IN l_stg_dhcp.FIRST..l_stg_dhcp.LAST
LOOP
SELECT COUNT (1)
INTO l_dhcp_cnt
FROM subntwk
WHERE subntwk_nm = l_stg_dhcp(i).dhcp_token;
IF l_dhcp_cnt < 1 THEN
INSERT
INTO SUBNTWK
(
subntwk_nm
)
VALUES
(
l_stg_dhcp(i).dhcp_token
);
DBMS_OUTPUT.PUT_LINE('Token has been added : '||l_stg_dhcp(i).dhcp_token);
ELSE
DBMS_OUTPUT.PUT_LINE('Token is already present');
END IF;
EXIT WHEN l_stg_dhcp.COUNT =0;
END LOOP;
commit;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('ERROR OCCURED ' || SQLERRM);
END;
/
exit
For link: 对于链接:
spool load_link.log
set serveroutput on
DECLARE
l_cmts_1 VARCHAR2( 4000 CHAR );
l_cmts_add VARCHAR2( 200 CHAR );
l_dhcp_cnt NUMBER;
l_cmts_cnt NUMBER;
l_link_cnt NUMBER;
l_add_link_nm VARCHAR2( 200 CHAR );
BEGIN
FOR r IN (
SELECT dhcp_token, cmts_to_add || ',' cmts_add
FROM stg_link_data
)
LOOP
l_cmts_1 := r.cmts_add;
l_cmts_add := TRIM(SUBSTR( l_cmts_1, 1, INSTR(l_cmts_1, ',' ) - 1 ));
SELECT COUNT(1)
INTO l_dhcp_cnt
FROM subntwk
WHERE subntwk_nm = r.dhcp_token;
IF l_dhcp_cnt = 0 THEN
DBMS_OUTPUT.PUT_LINE('Device not found : '|| r.dhcp_token);
ELSE
WHILE l_cmts_add IS NOT NULL
LOOP
l_add_link_nm := r.dhcp_token||'_TO_'||l_cmts_add;
SELECT COUNT(1)
INTO l_cmts_cnt
FROM subntwk
WHERE subntwk_nm = TRIM(l_cmts_add);
SELECT COUNT(1)
INTO l_link_cnt
FROM link
WHERE link_nm = l_add_link_nm;
IF l_cmts_cnt > 0 AND l_link_cnt = 0 THEN
INSERT INTO link ( link_nm)
VALUES ( l_add_link_nm);
DBMS_OUTPUT.PUT_LINE( l_add_link_nm ||' '||'Has been added.');
ELSIF l_link_cnt > 0 THEN
DBMS_OUTPUT.PUT_LINE( 'link is already present : '|| l_add_link_nm );
ELSIF l_cmts_cnt = 0 then
DBMS_OUTPUT.PUT_LINE( 'NO CMTS FOUND for device to create link : '|| l_cmts_add );
END IF;
l_cmts_1 := TRIM(SUBSTR( l_cmts_1, INSTR( l_cmts_1, ',' ) + 1 ));
l_cmts_add := TRIM(SUBSTR( l_cmts_1, 1, INSTR( l_cmts_1, ',' ) - 1 ));
END LOOP;
END IF;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('ERROR OCCURED ' || SQLERRM);
END;
/
exit
Control files: 控制文件:
LOAD DATA
INFILE 'cmts_data.csv'
APPEND
INTO TABLE STG_CMTS_DATA
when (cmts_token != '') AND (cmts_token != 'NULL') AND (cmts_token != 'null')
and (cmts_ip != '') AND (cmts_ip != 'NULL') AND (cmts_ip != 'null')
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(cmts_token "RTRIM(LTRIM(:cmts_token))",
cmts_ip "RTRIM(LTRIM(:cmts_ip))")
For dhcp
: 对于
dhcp
:
LOAD DATA
INFILE 'dhcp_data.csv'
APPEND
INTO TABLE STG_DHCP_DATA
when (dhcp_token != '') AND (dhcp_token != 'NULL') AND (dhcp_token != 'null')
and (dhcp_ip != '') AND (dhcp_ip != 'NULL') AND (dhcp_ip != 'null')
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(dhcp_token "RTRIM(LTRIM(:dhcp_token))",
dhcp_ip "RTRIM(LTRIM(:dhcp_ip))")
For link: 对于链接:
LOAD DATA
INFILE 'link_data.csv'
APPEND
INTO TABLE STG_LINK_DATA
when (dhcp_token != '') AND (dhcp_token != 'NULL') AND (dhcp_token != 'null')
and (cmts_to_add != '') AND (cmts_to_add != 'NULL') AND (cmts_to_add != 'null')
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(dhcp_token "RTRIM(LTRIM(:dhcp_token))",
cmts_to_add CHAR(4000) "RTRIM(LTRIM(:cmts_to_add))")
Shell script: Shell脚本:
if [ ! -d ./log ]
then
mkdir log
fi
if [ ! -d ./done ]
then
mkdir done
fi
if [ ! -d ./bad ]
then
mkdir bad
fi
nohup time sqlldr username/password@SID CONTROL=load_cmts_data.ctl LOG=log/ldr_cmts_data.log BAD=log/ldr_cmts_data.bad DISCARD=log/ldr_cmts_data.reject ERRORS=100000 DIRECT=TRUE PARALLEL=TRUE &
nohup time username/password@SID @load_cmts.sql
nohup time sqlldr username/password@SID CONTROL=load_dhcp_data.ctl LOG=log/ldr_dhcp_data.log BAD=log/ldr_dhcp_data.bad DISCARD=log/ldr_dhcp_data.reject ERRORS=100000 DIRECT=TRUE PARALLEL=TRUE &
nohup time sqlplus username/password@SID @load_dhcp.sql
nohup time sqlldr username/password@SID CONTROL=load_link_data.ctl LOG=log/ldr_link_data.log BAD=log/ldr_link_data.bad DISCARD=log/ldr_link_data.reject ERRORS=100000 DIRECT=TRUE PARALLEL=TRUE &
nohup time sqlplus username/password@SID @load_link.sql
mv *.log ./log
So here the problem which I encounter is while loading data into the link
table I am checking if DHCP present in the subntwk
table then proceed further else log error. 所以在这里,我遇到的问题是在将数据加载到
link
表中时,我正在检查subntwk
表中是否存在DHCP,然后进一步处理否则出现日志错误。 If CMTS exits then create link else log error. 如果CMTS退出,则创建链接,否则记录错误。
Now as we can we here multiple CMTS are associated with single DHCP. 现在我们可以在这里将多个CMTS与单个DHCP关联。
So here in link
table it is creating link but for the last iteration of the loop where I am fetching separate comma separated CMTS from stg_link_data
table it is giving me log as CMTS not found. 因此,在
link
表中,它正在创建链接,但是对于循环的最后一次迭代,在该循环中,我从stg_link_data
表中提取了逗号分隔的CMTS,这为我提供了未找到CMTS的日志。
For example 例如
dhcp-1-1-1|wnlb-cmts-01-1,wnlb-cmts-02-2
Here I am supposed to create link for dhcp-1-1-1
with wnlb-cmts-01-1
and wnlb-cmts-02-2
在这里我应该使用
wnlb-cmts-01-1
和wnlb-cmts-02-2
wnlb-cmts-01-1
为dhcp-1-1-1
创建链接
Theses data are all present in the subntwk
table but still it is giving me log as wnlb-cmts-02-2 NOT FOUND
but we have already loaded it into subntwk
table. 这些数据都存在于
subntwk
表中,但是仍然给我显示wnlb-cmts-02-2 NOT FOUND
日志,但是我们已经将其加载到subntwk
表中。
The same is happening with all CMTS from stg_link_data
table which are in the last (I believe here you got what I am trying to explain). 最后一个来自
stg_link_data
表的所有CMTS也会发生相同的情况(我相信这里您已经得到我要解释的内容)。
But when I run the SQL scripts in the SQL Developer separately then it inserts all the valid links into the link table. 但是,当我分别在SQL Developer中运行SQL脚本时,它将所有有效链接插入到链接表中。
Here it should create 9 rows in the link table whereas now it is creating only 5 rows. 在这里,它应该在链接表中创建9行,而现在仅创建5行。
I use COMMIT in my script also but it is not helping me. 我也在脚本中使用COMMIT,但是它没有帮助。
Please run these scripts in your machine and let me know if you are also getting the same behavior as I am getting, and please provide me some solution. 请在您的计算机上运行这些脚本,并让我知道您是否也获得了与我相同的行为,请提供一些解决方案。 I have tried lots of thing from yesterday but it is still same.
从昨天开始,我尝试了很多事情,但还是一样。
Here is the log for link table: 这是链接表的日志:
link is already present : dhcp-1-1-1_TO_wnlb-cmts-01-1
NO CMTS FOUND for device to create link : wnlb-cmts-02-2
link is already present : dhcp-1-1-2_TO_wnlb-cmts-03-3
link is already present : dhcp-1-1-2_TO_wnlb-cmts-04-4
NO CMTS FOUND for device to create link : wnlb-cmts-05-5
NO CMTS FOUND for device to create link : wnlb-cmts-01-1
NO CMTS FOUND for device to create link : wnlb-cmts-05-8
NO CMTS FOUND for device to create link : wnlb-cmts-05-6
NO CMTS FOUND for device to create link : wnlb-cmts-05-0
NO CMTS FOUND for device to create link : wnlb-cmts-03-3
link is already present : dhcp-1-1-5_TO_wnlb-cmts-02-2
link is already present : dhcp-1-1-5_TO_wnlb-cmts-04-4
NO CMTS FOUND for device to create link : wnlb-cmts-05-7
Device not found : wnlb-dhcp-1-13
If more information is required then please let me know. 如果需要更多信息,请告诉我。
I figure out later in night that while loading into staging table using UNIX machine it was creating new line for each row. 我在晚上晚些时候发现,使用UNIX计算机将其加载到暂存表中时,它正在为每一行创建新行。 That is why the last CMTS was not found, For that i use DOS 2 UNIX conversion and it starts working perfectly.
这就是为什么找不到最后一个CMTS的原因,为此我使用了DOS 2 UNIX转换,并且开始正常运行。
That was dos2unix error ! 那是dos2unix错误!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.