Invoked Sqlload in Java

http://www.javabulls.com/node/1
public class CSVLoader {

public void execute(LoadableCSV csv) throws Exception{
try {
String comand = init(csv);
service(comand);
} catch (Throwable e) {
throw new Exception(e);
} finally {
shutdown();
}
}

protected String init(LoadableCSV csv) {
StringBuffer command = new StringBuffer();
command.append("sqlldr").append(" ");
command.append(csv.getLoaderCommand());

System.out.println("CSVLoader initialized.");

return command.toString();
}

protected void service(String command) throws Exception {

Runtime r = Runtime.getRuntime();
long startTime = System.currentTimeMillis();

try {
Process process = r.exec(command);
if (process == null) {
System.out.println("Process NOT Initialized for command [" + command + "]");
} else {
// wait for the process to exit
process.waitFor();
System.out.println("Loading completed. Exit Value [" + process.exitValue() + "] Time taken ["
+ (System.currentTimeMillis() - startTime) / 1000 + "] sec");

StringBuffer strBuffer = new StringBuffer();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null) {
strBuffer.append(line).append("\n");
}
reader.close();

if (process.exitValue() != 0) {
System.out.println("ERROR:" + strBuffer.toString());
throw new Exception("Sql Loading Exited with [" + process.exitValue() + "]");
} else {
System.out.println("Loader completed execution. \n" + strBuffer.toString());
}
}
} catch (InterruptedException e) {
System.out.println("Exception while waiting for process [" + command + "] to complete");
throw e;
} catch (IOException e) {
System.out.println("Exception executing process [" + command + "]");
}

System.out.println("CSVLoader service done.");
}

protected void shutdown() {
System.out.println("CSVLoader shutdown.");
}

}

The Loader class is invoked from any Java process using a LoadableCSV instance.
LoadableCSV is an interface which can be implemented for different flat files
as per the requirement.

public interface LoadableCSV {

public int getRecordsToSkip();

public int getRecordsToLoad();

public int getErrorsAllowed();

public String getSeparator();

public String getLoaderCommand();

}

The CSVLoader class just uses the getLoaderCommand() in the example above,
but can be enhanced to use other methods mentioned above or more methods
can be added to the interface and each implementing csv can provide it's
own implementation. This simple design above helps load different types
of csv files from a single loader class.

**Regular caveats apply, the code may have some bugs or may just not be
the ideal choice for your implementation. Please feel free to use the above
as a reference for your implementation.

Happy Loading.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值