日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java调用sqluldr_java 使用 oracle sqluldr2 快速导出数据文件

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java调用sqluldr_java 使用 oracle sqluldr2 快速导出数据文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前提

1、確認本地電腦 oracle 已經安裝好

2、將工具文件在oracle下的bin目錄

以下是64位的 window\linux 文件下載地址

https://github.com/chenjunwei111/sqlLoaderFile

實體類

/**

* Description sqlLoader類

* @param

* @Author junwei

* @Date 14:56 2020/11/26

**/

public class ctlUMsg {

private String query;

private String separator;//指定字段分隔符 默認逗號

private String fileName;//導出文件名 可動態指定名 %y%m%d.txt 表示年月日; %b.txt 大多文件導出時,表示文件序號

private boolean header;//是否需列頭 Yes/No 默認No

private boolean isSql;

private String quote;//指定非數字字段值前后的引號符 如雙引號: 1,"A",3 \r=0x0d \n=0x0a |=0x7c \t=0x09 :=0x3a #=0x23 "=0x22 '=0x27 &=0x26 |=0x7c 空格=0x20

private boolean isCom2gz;//壓縮成gz 文件后綴加.gz

private Integer batch;//分多文件導出 1:50W 2:100W ... 結合fileName設置序號

private boolean isLog;//輸出log

/**

* controller parameter

* @param query sql statement or sql file(.sql)

* @param fileName File name including css/txt suffix.can naming by parameter ,ex:%y%m%d.txt(yyyymmdd), %b.txt (automatic generation of serial numbers)

*/

public ctlUMsg(String query, String fileName) {

super();

this.query=query;

this.fileName=fileName;

if (this.query.toLowerCase().endsWith(".sql")) {

this.isSql=true;

}

}

/**

* controller parameter

* @param query sql statement or sql file(.sql)

* @param fileName File name including css/txt suffix.can naming by parameter ,ex:%y%m%d.txt(yyyymmdd), %b.txt (automatic generation of serial numbers)

* @param separator data separator. can use ASCII code for separator. default is |.

* @param isLog export logger?default append to logger as same log file name.

*

* ASCII:\r=0x0d \n=0x0a |=0x7c \t=0x09 :=0x3a #=0x23 "=0x22 '=0x27 &=0x26 |=0x7c 空格=0x20.

*/

public ctlUMsg(String query, String fileName, String separator, boolean isLog) {

super();

this.query=query;

this.fileName=fileName;

this.separator=separator;

this.isLog=isLog;

if (this.query.toLowerCase().endsWith(".sql")) {

this.isSql=true;

}

}

/**

* controller parameter

* @param query query sql statement or sql file(.sql)

* @param fileName File name including css/txt suffix.can naming by parameter ,ex:%y%m%d.txt(yyyymmdd), %b.txt (automatic generation of serial numbers)

* @param separator data separator.separator can use ASCII code. default is |

* @param isLog export logger?default append to logger as same log file name.

* @param header export header? default is No

* @param batch multi-batch export.each batch is 50W.ex:1=50W 2=100W ...

* @param quote quotation marks before and after non-numeric field values.quote can use ASCII code.ex:1,"A","b",3

* @param isCom2gz compression to gz format

*

* separator and quote ASCII:\r=0x0d \n=0x0a |=0x7c \t=0x09 :=0x3a #=0x23 "=0x22 '=0x27 &=0x26 |=0x7c 空格=0x20

*/

public ctlUMsg(String query, String fileName, String separator, boolean isLog, boolean header, Integer batch, String quote, boolean isCom2gz) {

super();

this.query=query;

this.fileName=fileName;

this.separator=separator;

this.header=header;

this.batch=batch;

this.quote=quote;

this.isCom2gz=isCom2gz;

this.isLog=isLog;

if (this.query.toLowerCase().endsWith(".sql")) {

this.isSql=true;

}

}

public String getQuery() {

return query;

}

public String getSeparator() {

return separator;

}

public String getFileName() {

return fileName;

}

public boolean isHeader() {

return header;

}

public boolean isSql() {

return isSql;

}

public String getQuote() {

return quote;

}

public boolean isCom2gz() {

return isCom2gz;

}

public Integer getBatch() {

return batch;

}

public boolean isLog() {

return isLog;

}

}

實現類

import org.springframework.stereotype.Service;

import java.io.BufferedReader;

import java.io.File;

import java.io.InputStream;

import java.io.InputStreamReader;

/**

* Description SQLLdr導出類

* @param

* @Author junwei

* @Date 14:47 2020/11/26

**/

@Service

public class sqlLdrOut {

public sqlLdrOut() {

}

public boolean Loader(ctlUMsg cMsg) {

try {

this._cMsg = cMsg;

if (!this.chkKW()) {

return false;

}

if (!this.builderCmd()) {

return false;

}

return this.start();

} catch (Exception e) {

this.exError = new Throwable("sqluloader executing error. see error log.");

}

return true;

}

private boolean builderCmd() {

try {

StringBuilder sb = new StringBuilder();

if (this._sMsg.getSqlldrPath() != null) {

sb.append(this._sMsg.getSqlldrPath() + File.separator);

}

sb.append("sqluldr2 " + this._sMsg.getUserName() + "/" + this._sMsg.getPassWord() + "@"

+ this._sMsg.getAddressIp() + ":" + this._sMsg.getPort() + "/" + this._sMsg.getOrclName() + " ");

if (this._cMsg.isSql()) {

sb.append("sql=" + this._sMsg.getDataDir() + File.separator + this._cMsg.getQuery() + " ");

} else {

sb.append("query=\"" + this._cMsg.getQuery() + "\" ");

}

sb.append("file=" + this._sMsg.getDataDir() + File.separator + this._cMsg.getFileName()

+ (this._cMsg.isCom2gz() ? ".gz" : "") + " ");

if (this._cMsg.isLog()) {

String f = this._cMsg.getFileName().substring(0, this._cMsg.getFileName().lastIndexOf("."));

sb.append("log=+" + this._sMsg.getDataDir() + File.separator + f.replace("%b", "") + ".log ");

}

if (this._sMsg.getOrclEcoding() != null) {

sb.append("charset=" + this._sMsg.getOrclEcoding() + " ");

}

if (this._cMsg.isHeader()) {

sb.append("head=yes ");

}

if (this._cMsg.getSeparator() != null) {

sb.append("field=" + this._cMsg.getSeparator() + " ");

} else {

sb.append("field=0x7c ");

}

if (this._cMsg.getBatch() != null && this._cMsg.getBatch() > 0) {

sb.append("batch=" + this._cMsg.getBatch() + " ");

}

if (this._cMsg.getQuote() != null) {

sb.append("quote=" + this._cMsg.getQuote() + " ");

}

sb.append("safe=yes");

this.connectStr = sb.toString().trim();

sb.setLength(0);

return true;

} catch (Exception e) {

this.exError = new Throwable("build cmd error. ", e);

return false;

}

}

private boolean chkKW() {

if (this._sMsg == null) {

this.exError = new Throwable("sMsg is null. ");

return false;

}

if (!this._sMsg.isIsvaild()) {

this.exError = new Throwable("sMsg key parameter is null. ");

return false;

}

if (this._cMsg.getFileName() == null || this._cMsg.getQuery() == null) {

this.exError = new Throwable("cMsg key parameter is null. ");

return false;

}

return true;

}

private boolean start() {

InputStream ins = null;

Process process = null;

BufferedReader readers = null;

try {

System.out.println("CMD命令");

System.out.println(this.connectStr);

process = Runtime.getRuntime().exec(this.connectStr);

ins = process.getInputStream();

readers = new BufferedReader(new InputStreamReader(ins));

String line = null;

while ((line = readers.readLine()) != null) {

//String msg = new String(line.getBytes("ISO-8859-1"), "GBK");

//System.out.println(msg); // 輸出

}

int exitValue = process.waitFor();

if (exitValue == 0) {

return true;

} else {

this.exError = new Throwable("load data file error.see log");

return false;

}

} catch (Exception e) {

this.exError = new Throwable("start sqluder2 process error. ", e);

return false;

} finally {

try {

if (process != null) {

process.getOutputStream().close();

}

if (readers != null) {

readers.close();

readers = null;

}

if (ins != null) {

ins.close();

ins = null;

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

/**

* get error message

*

* @return

*/

public Throwable getexError() {

return this.exError;

}

private ldrMsg _sMsg = null;

private ctlUMsg _cMsg = null;

private Throwable exError = null;

private String connectStr;

public sqlLdrOut(ldrMsg sMsg) {

super();

this._sMsg = sMsg;

}

//導出例子

public static void main(String[] args) {

String getDataSql="select 't1' as t1 ,'t2' as t2 from dual \n" +

"union select 't3' as t3, 't4' as t4 from dual ";

String tableName="dual";

sqlLdrOut out=new sqlLdrOut();

out.operate(getDataSql, tableName);

}

/**

* Description 核心導出方法

* @param

* @Author junwei

* @Date 16:54 2020/11/25

**/

public void operate(String getDataSql,String tableName){

//這里導出CSV后綴文件,可以修改為txt等

String fileName=tableName+".csv";

//linux

//String originfilename=" /data/static/spmv/ftpupload/";

//String toolPath="/home/tomcat/";

//window

//導出的文件路徑

String originfilename="D:\\export\\";

//使用到的sqlLoader工具文件 父級目錄(通常是oracle安裝目錄下BIN目錄是)

String toolPath="D:\\oracle\\product\\11.2.0\\dbhome_2\\BIN";

//JDBC數據庫連接

ldrMsg lm = new ldrMsg("192.168.10.11", "account", "password", 1521, "orcl",originfilename ,

toolPath, null);

sqlLdrOut sOut = new sqlLdrOut(lm);

ctlUMsg cu = new ctlUMsg(

getDataSql,fileName, "0x2c", false, true, 1, null, false);

if (!sOut.Loader(cu)) {

System.out.println("錯誤1:" + tableName + ":"+sOut.getexError());

}else{

System.out.println("導出完成");

}

}

}

查詢SQL,寫自己想要的數據即可

本文地址:https://blog.csdn.net/qq_37203082/article/details/110188164

希望與廣大網友互動??

點此進行留言吧!

總結

以上是生活随笔為你收集整理的java调用sqluldr_java 使用 oracle sqluldr2 快速导出数据文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。