我附上了以下代码
功能
在用webmacro替换值后读取csv并插入db。
从csv @读取第一个标题信息 NO,NAME 旁边的值逐一读取值并放入webmacro上下文context.put(“1”,“RAJARAJAN”)下一个webmacro替换$ (NO)==> 1和$(NAME)==> RAJARAJAN并在达到1000执行批处理后添加批处理。
代码按功能运行,但解析50,000条记录需要4分钟才能提高性能或需要更改逻辑....如果有任何疑问,请告诉我。 对性能的任何改变......
注意:我使用webmacro是因为要将合并查询中的$(NO)替换为CSV中读取的值
Bala.csv
NO?NAME
1?RAJARAJAN
2?ARUN
3?ARUNKUMAR
Connection con=null;
Statement stmt=null;
Connection con1=null;
int counter=0;
try{
WebMacro wm = new WM();
Context context = wm.getContext();
String strFilePath = "/home/vbalamurugan/3A/email-1822820895/Bala.csv";
String msg="merge into temp2 A using
(select '$(NO)' NO,'$(NAME)' NAME from dual)B on(A.NO=B.NO)
when not matched then insert (NO,NAME)
values(B.NO,B.NAME) when matched then
update set A.NAME='Attai' where A.NO='$(NO)'";
String[]rowsAsTokens;
con=getOracleConnection("localhost","raymedi_hq","raymedi_hq","XE");
con.setAutoCommit(false);
stmt=con.createStatement();
File file = new File(strFilePath);
Scanner scanner = new Scanner(file);
try {
String headerField;
String header[];
headerField=scanner.nextLine();
header=headerField.split("\\?");
long start=System.currentTimeMillis();
while(scanner.hasNext()) {
String scan[]=scanner.nextLine().split("\\?");
for(int i=0;i<scan.length;i++){
context.put(header[i],scan[i]);
}
if(context.size()>0){
String m=replacingWebMacroStatement(msg,wm,context);
if(counter>1000){
stmt.executeBatch();
stmt.clearBatch();
counter=0;
}else{
stmt.addBatch(m);
counter++;
}
}
}
long b=System.currentTimeMillis()-start;
System.out.println("=======Total Time Taken"+b);
}catch(Exception e){
e.printStackTrace();
}
finally {
scanner.close();
}
stmt.executeBatch();
stmt.clearBatch();
stmt.close();
}catch(Exception e){
e.printStackTrace();
con.rollback();
}finally{
con.commit();
}
// Method For replace webmacro with $
public static String replacingWebMacroStatement(String Query, WebMacro wm,Context context) throws Exception {
Template template = new StringTemplate(wm.getBroker(), Query);
template.parse();
String macro_replaced = template.evaluateAsString(context);
return macro_replaced;
}
// for getting oracle connection
public static Connection getOracleConnection(String IPaddress,String username,String password,String Tns)throws SQLException{
Connection connection = null;
try{
String baseconnectionurl ="jdbc:oracle:thin:@"+IPaddress+":1521:"+Tns;
String driver = "oracle.jdbc.driver.OracleDriver";
String user = username;
String pass = password;
Class.forName(driver);
connection=DriverManager.getConnection(baseconnectionurl,user,pass);
}catch(Exception e){
e.printStackTrace();
}
return connection;
}
答案 0 :(得分:0)
我可以告诉你,这个代码在我的机器上平均需要大约150毫秒:
StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
for (int i=0;i<50000;i++) {
tokenizer.reset("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
String toks[] = tokenizer.getTokenArray();
}
你会在apache commons-lang包中找到StrTokenizer,但我怀疑String.split(),StringTokenizer或Scanner.nextLine()在任何情况下都是你的瓶颈。我认为这是你的数据库插入时间。
如果是这种情况,你可以做两件事之一:
正如所建议的,分析器将有助于确定您的时间花在哪里。