1.java卸载数据(ociuldr user=anysql/anysql query="select * from tab" field="|")
我们通常会采用jdbc操作数据库然后用io写入文件,而在大数据的情况下用io写入文件是比较慢的,所以在工作中,我们会采用oracle的ociuldr.exe命令进行卸载数据下面是部分代码
<span style="font-size:14px;">strCmd=ociuldr.exe user="query/oracle@dw" file="e:\interface\jxjk\20130630\in_biz_ba.txt" query="select data_date, barg_no, ba_no, area_no, org_no, currency, cust_no, cust_type, cust_name, draw_date, mature_date, face_amt, bail_acct, assure_mode, ead_rate, cash_flag, cash_date, status, manager_no from in_biz_ba where data_date= date'2013-06-30'" field="|"
Runtime rt = Runtime.getRuntime();
Process pro = rt.exec(strCmd);
if (pro.waitFor() == 0) {}</span>
2.ftp上传数据到目标位置
我们将第一步生成的txt文件,通过ftp上传到指定服务器下面是部分逻辑实现的代码
<span style="font-size:14px;">package com.sunline.pds;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import com.sunline.pds.util.Para;
public class FTP {
Para p = null;
public FTP(Para p) {
this.p = p;
}
private FTPClient ftp;
/**
*
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password){
boolean result = false;
ftp = new FTPClient();
int reply;
try {
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
*
* @param file 上传的文件或文件夹
* @throws Exception
*/
private void upload(File file){
try {
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i]);
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
}else{
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
private void disConnect() {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
public void upFtp(String date) {
String etlDate = date.replace("-", "");
this.connect(p.getUpDir(), p.getUpip(), 21, p.getFtpName(), p.getFtpPass());
File file = new File(p.getFileDir()+etlDate);
this.upload(file);
System.out.println("上传成功");
this.disConnect();
}
/*public static void main(String[] args) throws Exception{
FTP t = new FTP();
t.connect("/jxjk/", "10.10.1.39", 21, "administrator", "passwd");
File file = new File("e:\\interface\\jxjk\\/jxjk/");
System.out.println(file.getName());
t.upload(file);
t.disConnect();
} */
}</span>
3.java调用sqlldr将txt文件导入到指定的表中(sqlldr username/password@sid control=*.ctl)
我们可以通过IO操作通过对字符串的操作,通过string.split分割,然后循环将数据插入到表中,可以完成操作,可是速度比较慢。所以我们调用oracle的sqlldr完成对数据的导入。下面是部分代码
<span style="font-size:14px;">strCmd="sqlldr.exe query/oracle@dw control=控制文件目录(d:/tableName.ctl)" log="d:\tablename.log" bad="d:\tablename.bad" direct=true"
Runtime rt = Runtime.getRuntime();
Process pro = rt.exec(strCmd);
if (pro.waitFor() == 0) {}</span>
控制文件格式
load data
infile 'd:\tablename.txt'
append into table tablename
fields terminated by '|||'
TRAILING NULLCOLS
(a varchar2,
b number,
c varchar2)
总结:以前学习时,由于数据量都不大我们很少关注程序的性能,只知道运用java基础去完成功能,可是进入工作后我们要考虑的东西就不只是完成功能啦,我们要考虑程序的性能与效率