本文介绍 OBLOADER 的 --pos 参数的使用方法和场景。
适用版本
OBLOADER and OBDUMPER 所有版本。
操作步骤
比如有导入固定长度的数据文件入库的需求。

OBLOADER 的
--pos参数可以导入 POS 字节偏移格式的数据文件,文件中存放按照固定长度的字节偏移位置组织的数据。注意这里指的是字节数,不是字符数,一个字符在不同的编码格式下对应着不同的字节数。 字节位置要显式指定来让程序解析。
比如原来的导入脚本:
#!/bin/sh
USER_NAME=root
PORT=3339
HOST_NAME=127.x.x.x
DB_NAME=test
mysql_sit="mysql -u${USER_NAME} -P${PORT} -h${HOST_NAME} -D${DB_NAME} -pxxxxxx --local-infile=1 --default-character-set=utf8mb4 -A -N"export
MYSQL_PWD=${csp_password}
sql="LOAD DATA LOCAL INFILE 'cs2-20220303-kficrpst.bin' INTO TABLE csp_imp_fova_kficrpst (@row) SET batch_date='20220304', zoneno=TRIM(SUBSTR(@row,1,5)), brno=TRIM(SUBSTR(@row,6,5)), currtype=TRIM(SUBSTR(@row,11,3)), dsubcode=TRIM(SUBSTR(@row,14,7)), csubcode=TRIM(SUBSTR(@row,21,7)), corperf=TRIM(SUBSTR(@row,28,1)), drcrf=TRIM(SUBSTR(@row,29,1)), cardno=TRIM(SUBSTR(@row,30,19)), trxcode=TRIM(SUBSTR(@row,49,5)), trxamt=TRIM(SUBSTR(@row,54,17)), trxserno=TRIM(SUBSTR(@row,71,23)), merno=TRIM(SUBSTR(@row,94,15)), authno=TRIM(SUBSTR(@row,109,6)), trxdisp=TRIM(SUBSTR(@row,115,120)), succflag=TRIM(SUBSTR(@row,235,1)), notes=TRIM(SUBSTR(@row,236,120)), trxzone=TRIM(SUBSTR(@row,356,5)), kmsg=TRIM(SUBSTR(@row,361,9)), trxdate=TRIM(SUBSTR(@row,370,10)), trxsrc=TRIM(SUBSTR(@row,380,10)), mflag=TRIM(SUBSTR(@row,390,3)), bakdec=TRIM(SUBSTR(@row,393,17)), bakdate=TRIM(SUBSTR(@row,410,10)), bakchar=TRIM(SUBSTR(@row,420,100)); select count(*) from csp_imp_fova_kficrpst where batch_date='20220304';"
echo ${sql}
execute_result=$($mysql_sit -s -e "${sql}")
echo $execute_result
可以直接使用 OBLOADER 带上 --pos 参数来简化导入脚本。
参考导入命令。
./obloader -h 192.xxx.x.xxx -P xxxx -u oracle_xxxx -p 'xxxxxxxx!' -D TPCC --table TPCCTEST --pos -f /root/data/20220518 --public-cloud --ctl-path '/root/ctl/TPCCTEST.CTRL' --external-data
参考控制文件。
lang=java (
batch_date position(1:0) "systimestamp",
zoneno position(1:5) "trim(zoneno)",
brno position(6:10) "trim(brno)",
currtype position(11:13) "trim(currtype)",
dsubcode position(14:20) "trim(dsubcode)",
csubcode position(21:27) "trim(csubcode)",
corperf position(28:28) "trim(corperf)",
drcrf position(29:29) "trim(drcrf)",
cardno position(30:46) "trim(cardno)",
trxcode position(49:53) "trim(trxcode)",
trxamt position(54:70) "trim(trxamt)",
trxserno position(71:93) "trim(trxserno)",
merno position(94:108) "trim(merno)",
authno position(109:114) "trim(authno)",
trxdisp position(115:234) "trim(trxdisp)",
succflag position(235:235) "trim(succflag)",
notes position(236:355) "trim(notes)",
trxzone position(356:360) "trim(trxzone)",
kmsg position(361:369) "trim(kmsg)",
trxdate position(370:379) "trim(trxdate)",
trxsrc position(380:389) "trim(trxsrc)",
mflag position(390:392) "trim(mflag)",
bakdec position(393:409) "trim(bakdec)",
bakdate position(410:409) "trim(bakdate)",
bakchar position(420:520) "trim(bakchar)"
);
说明
position(x:y) position 的意思是(起始位置:长度)。
对应的建表语句。
CREATE TABLE `csp_imp_fova_kficrpst` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵自增',
`batch_date` varchar(10) DEFAULT NULL,
`zoneno` varchar(5) DEFAULT NULL,
`brno` varchar(5) DEFAULT NULL,
`currtype` varchar(3) DEFAULT NULL,
`dsubcode` varchar(7) DEFAULT NULL,
`csubcode` varchar(7) DEFAULT NULL,
`corperf` varchar(1) DEFAULT NULL,
`drcrf` varchar(1) DEFAULT NULL,
`cardno` varchar(19) DEFAULT NULL,
`trxcode` varchar(5) DEFAULT NULL,
`trxamt` varchar(17) DEFAULT NULL,
`trxserno` varchar(23) DEFAULT NULL,
`merno` varchar(15) DEFAULT NULL,
`authno` varchar(6) DEFAULT NULL,
`trxdisp` varchar(120) DEFAULT NULL,
`succflag` varchar(1) DEFAULT NULL,
`notes` varchar(120) DEFAULT NULL,
`trxzone` varchar(5) DEFAULT NULL,
`kmsg` varchar(9) DEFAULT NULL,
`trxdate` varchar(10) DEFAULT NULL,
`trxsrc` varchar(10) DEFAULT NULL,
`mflag` varchar(3) DEFAULT NULL,
`bakdec` varchar(17) DEFAULT NULL,
`bakdate` varchar(10) DEFAULT NULL,
`bakchar` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8