java实现oracle 快速卸载数据并ftp上传数据给各个其他业务系统[从学习到工作(一)]

本文介绍了一种使用Java进行大数据量处理的方法,包括利用Oracle的OCI工具进行数据卸载、通过FTP上传文件以及使用SQL*Loader导入数据到Oracle数据库。这些步骤能够显著提高数据处理的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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基础去完成功能,可是进入工作后我们要考虑的东西就不只是完成功能啦,我们要考虑程序的性能与效率

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值