TransOracle2Greenplum.java 14.4 KB
Newer Older
MaxKey单点登录官方's avatar
init v1  
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/**
 * 
 */
package com.blazer.trans.impl;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blazer.db.TableColumns;
import com.blazer.db.TableDescribe;


/**
 * @author mhshi
 * 
 */
public class TransOracle2Greenplum extends TransBase {
	private static final Logger _logger = LoggerFactory.getLogger(TransOracle2Greenplum.class);
	
	public TransOracle2Greenplum(
			DataSource sourceDataSource,
			DataSource targetDataSource,
			ArrayList<TableDescribe> listTables, 
			String transType,
			String owner,
			int commitNum,
			int threadNum) {
		super();
		this.sourceDataSource = sourceDataSource;
		this.targetDataSource = targetDataSource;
		this.commitNum = commitNum;
		this.listTables = listTables;
		this.threadNum=threadNum;
		this.owner=owner;
		this.transType=transType;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(TableDescribe tableQuery :listTables){
			listTableColumns = new ArrayList<TableColumns>();
			listTablePKs = new ArrayList<String>();
			listTableLobColumns = new ArrayList<TableColumns>();
			table=tableQuery;
			try {
				getRecordCount(tableQuery.getTableName()+tableQuery.getWhereSqlString());
				if(table.getTargetTableName()==null ||table.getTargetTableName().equalsIgnoreCase("")) {
					table.setTargetTableName(table.getTableName());
				}
				_logger.info("--thread "+threadNum+" --Trans Record Count "+recordsCount+" , Table " + tableQuery.getTableName().toUpperCase());
				if(recordsCount<=0)continue;
				
				Connection sourcConn=sourceDataSource.getConnection();
				Connection targetConn=targetDataSource.getConnection();
				Statement  targetStmt=targetConn.createStatement();
				if(transType.equalsIgnoreCase("FULL")){
					deleteRecordsCount=targetStmt.executeUpdate("TRUNCATE TABLE "+tableQuery.getTargetTableName()+tableQuery.getWhereSqlString());
					_logger.info("--thread "+threadNum+" --Trans Delete Records "+deleteRecordsCount+" , TRUNCATE TABLE "+tableQuery.getTargetTableName()+tableQuery.getWhereSqlString());
				}else if(transType.equalsIgnoreCase("INCREMENT")){
					deleteRecordsCount=targetStmt.executeUpdate("DELETE FROM "+tableQuery.getTargetTableName()+tableQuery.getWhereSqlString());
					_logger.info("--thread "+threadNum+" --Trans Delete Records "+deleteRecordsCount+" , DELETE FROM "+tableQuery.getTargetTableName()+tableQuery.getWhereSqlString());
				}
				targetStmt.close();
				targetConn.close();
				
				Statement  sourcStmt = sourcConn.createStatement();
				ResultSet  sourceRs=sourcStmt.executeQuery("SELECT * FROM "+tableQuery.getTableName()+tableQuery.getWhereSqlString());
				buildMetaData(sourceRs);
				getTableCons();
				buildInsertSql();
				batchInsert(sourceRs);
				sourceRs.close();
				sourcStmt.close();
				sourcConn.close();
				Thread.sleep(2000);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		_logger.info("--thread "+threadNum+" -- Complete .");
	}
	
	public void batchInsert(ResultSet rs) throws Exception{
		Connection targetConn=targetDataSource.getConnection();
		targetConn.setAutoCommit(false);
		PreparedStatement targetPstmt = targetConn.prepareStatement(insertSql);
		long insertNum=0;
		long commitCount=0;
		while(rs.next()){
			int pos=1;
			for(TableColumns tc : listTableColumns){
				//_logger.info("--column "+tc.getColumnName()+" , "+tc.getDataType() );
				if(tc.getDataType().equalsIgnoreCase("VARCHAR2")||
						tc.getDataType().equalsIgnoreCase("VARCHAR")||
						tc.getDataType().equalsIgnoreCase("NVARCHAR2")||
						tc.getDataType().equalsIgnoreCase("CHAR")||
						tc.getDataType().equalsIgnoreCase("RAW")
						){
					//_logger.info("==="+tc.getColumnName());
					targetPstmt.setString(pos, rs.getString(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("NUMBER")){
					if(tc.getDataLength()==22&&tc.getDataScale()>0){//NUMBER
						targetPstmt.setLong(pos, rs.getLong(tc.getColumnName()));
					}else if(tc.getDataLength()==22&&tc.getDataScale()==0){//INTEGER
						targetPstmt.setInt(pos, rs.getInt(tc.getColumnName()));
					}else if(tc.getDataPrecision()==0||tc.getDataScale()==0||tc.getDataScale()==0){//LONG
						targetPstmt.setLong(pos, rs.getLong(tc.getColumnName()));
					}else{//DOUBLE
						targetPstmt.setDouble(pos, rs.getDouble(tc.getColumnName()));
					}
				}else if(tc.getDataType().equalsIgnoreCase("blob")){
					
				}else if(tc.getDataType().equalsIgnoreCase("clob")){
					
				}else if(tc.getDataType().equalsIgnoreCase("DATE")){
					targetPstmt.setDate(pos, rs.getDate(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("NCLOB")){
					//targetPstmt.setString(pos, rs.getString(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("LONG")){
					targetPstmt.setLong(pos, rs.getLong(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("ROWID")){
					targetPstmt.setString(pos, rs.getString(tc.getColumnName()));
					//targetPstmt.setRowId(pos, rs.getRowId(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("BINARY_DOUBLE")){
					targetPstmt.setString(pos, rs.getString(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("BINARY_FLOAT")){
					targetPstmt.setFloat(pos, rs.getFloat(tc.getColumnName()));
				}else if(tc.getDataType().indexOf(")")>-1){
					targetPstmt.setString(pos, rs.getString(tc.getColumnName()));
				}else{
					targetPstmt.setObject(pos, rs.getObject(tc.getColumnName()));
				}
				
				if(tc.getDataType().equalsIgnoreCase("BLOB")||tc.getDataType().equalsIgnoreCase("CLOB")||tc.getDataType().equalsIgnoreCase("NCLOB")){
					
				}else{
					pos++;
				}
			}
			
			commitCount++;
			if(hasLob){
				targetPstmt.execute();
				targetConn.commit();
				updateLob(rs);
				_logger.info("--thread "+threadNum+"--Commit Has LOB Count "+commitCount );
			}else{
				targetPstmt.addBatch();
				insertNum += 1;
				if (insertNum >= this.commitNum) {
					insertNum = 0;
					targetPstmt.executeBatch();
					targetConn.commit();
					_logger.info("--thread "+threadNum+"--Commit Count "+commitCount );
				}
			}
		}
		if (insertNum >0){
			targetPstmt.executeBatch();
			targetConn.commit();
			_logger.info("--thread "+threadNum+"--Commit Count "+commitCount +" Complete .");
		}
		_logger.info("--thread "+threadNum+"--Commit Count "+commitCount +" Complete .");
		targetPstmt.close();
		targetConn.close();
	}
	
	public void updateLob(ResultSet rs) throws Exception{
		Connection targetConn=targetDataSource.getConnection();
		PreparedStatement targetPstmt=targetConn.prepareStatement(updateLobSql);
		
		int pos=1;
		for(String pk :listTablePKs){
			for(TableColumns tc : listTableColumns){
				if(!pk.equalsIgnoreCase(tc.getColumnName()))continue;
				//_logger.info("--column "+tc.getColumnName()+" , "+tc.getDataType() );
				if(tc.getDataType().equalsIgnoreCase("VARCHAR2")||
						tc.getDataType().equalsIgnoreCase("VARCHAR")||
						tc.getDataType().equalsIgnoreCase("NVARCHAR2")||
						tc.getDataType().equalsIgnoreCase("CHAR")||
						tc.getDataType().equalsIgnoreCase("RAW")
						){
					targetPstmt.setString(pos, rs.getString(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("NUMBER")){
					if(tc.getDataLength()==22&&tc.getDataScale()>0){//NUMBER
						targetPstmt.setLong(pos, rs.getLong(tc.getColumnName()));
					}else if(tc.getDataLength()==22&&tc.getDataScale()==0){//INTEGER
						targetPstmt.setInt(pos, rs.getInt(tc.getColumnName()));
					}else if(tc.getDataPrecision()==0||tc.getDataScale()==0||tc.getDataScale()==0){//LONG
						targetPstmt.setLong(pos, rs.getLong(tc.getColumnName()));
					}else{//DOUBLE
						targetPstmt.setDouble(pos, rs.getDouble(tc.getColumnName()));
					}
				}else if(tc.getDataType().equalsIgnoreCase("BLOB")){
					//oracle.sql.BLOB sb=(oracle.sql.BLOB)rs.getBlob(tc.getColumnName());
					//targetPstmt.setBinaryStream(pos, rs.getBinaryStream(tc.getColumnName()), sb.length());
				}else if(tc.getDataType().equalsIgnoreCase("CLOB")||tc.getDataType().equalsIgnoreCase("NCLOB")){
					//oracle.sql.CLOB sb=(oracle.sql.CLOB)rs.getClob(tc.getColumnName());
					//targetPstmt.setAsciiStream(pos, rs.getAsciiStream(tc.getColumnName()), sb.length());
				}else if(tc.getDataType().equalsIgnoreCase("DATE")){
					targetPstmt.setDate(pos, rs.getDate(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("LONG")){
					targetPstmt.setLong(pos, rs.getLong(tc.getColumnName()));
				}else if(tc.getDataType().equalsIgnoreCase("ROWID")){
					targetPstmt.setString(pos, rs.getString(tc.getColumnName()));
					//targetPstmt.setRowId(pos, rs.getRowId(tc.getColumnName()));
				}else{
					targetPstmt.setObject(pos, rs.getObject(tc.getColumnName()));
				}
				pos++;
			}
		}
		
		ResultSet targetRs=targetPstmt.executeQuery();
		if(targetRs.next()){
			for(TableColumns tc:listTableLobColumns){
				if(tc.getDataType().equalsIgnoreCase("BLOB")){
					updateBlob(rs,targetRs,tc.getColumnName());
				}else if(tc.getDataType().equalsIgnoreCase("CLOB")||tc.getDataType().equalsIgnoreCase("NCLOB")){
					updateClob(rs,targetRs,tc.getColumnName());
				}
			}
			targetConn.commit();
			targetConn.close();
		}
	}
	
	@SuppressWarnings("deprecation")
	public void updateClob(ResultSet sourceRs,ResultSet targetRs,String column) throws Exception{
		oracle.sql.CLOB sb=(oracle.sql.CLOB)sourceRs.getClob(column);
		if(sb==null)return;
		Reader is=sb.getCharacterStream();
		char[]data=new char[(int)sb.length()];
		is.read(data);
		is.close();
		
		oracle.sql.CLOB tb=(oracle.sql.CLOB)targetRs.getClob(column);
		Writer os=tb.getCharacterOutputStream();
		os.write(data,0,data.length);
		os.flush();
		os.close();
	}
	
	@SuppressWarnings("deprecation")
	public void updateBlob(ResultSet sourceRs,ResultSet targetRs,String column) throws Exception{
		oracle.sql.BLOB sb=(oracle.sql.BLOB)sourceRs.getBlob(column);
		if(sb==null)return;
		InputStream is=sb.getBinaryStream();
		byte[]data=new byte[(int)sb.length()];
		is.read(data);
		is.close();
		
		oracle.sql.BLOB tb=(oracle.sql.BLOB)targetRs.getBlob(column);
		OutputStream os=tb.getBinaryOutputStream();
		os.write(data,0,data.length);
		os.flush();
		os.close();
		
	}
	public void buildMetaData(ResultSet rs) throws SQLException{
		ResultSetMetaData metaData = rs.getMetaData();
		_logger.info("--thread "+threadNum+"--column Count "+metaData.getColumnCount() );
		for (int i = 1; i <= metaData.getColumnCount(); i++) {
			TableColumns tc=new TableColumns();
			tc.setColumnName(metaData.getColumnName(i));
			tc.setDataType(metaData.getColumnTypeName(i));
			tc.setTableName(metaData.getTableName(i));
			tc.setDataPrecision(metaData.getPrecision(i));
			tc.setDataScale(metaData.getScale(i));
			_logger.info("--thread "+threadNum+"--No. "+i+" , Column "+tc.getColumnName()+" , DataType "+tc.getDataType() );
			listTableColumns.add(tc);
		}
	}
	
	public void getRecordCount(String tableQuery) throws SQLException{
        String countSql="SELECT COUNT(*) COUNTROWS  FROM "+tableQuery;
        _logger.debug("--thread "+threadNum+" countSql : "+countSql);
        Connection conn=sourceDataSource.getConnection();
		Statement  sourcStmt = conn.createStatement();
        ResultSet  contResultSet = sourcStmt.executeQuery(countSql);
        insertRecordsCount=0;
        recordsCount = 0;
        while(contResultSet.next()){
        	recordsCount = contResultSet.getLong(1); 
        }
        _logger.debug("--thread "+threadNum+" recordsCount : "+recordsCount);
	}
	public void buildInsertSql(){
		insertSql="INSERT INTO "+table.getTargetTableName();
		updateLobSql="SELECT  ";
		String c_str="";
		String v_str="";
		String where_sql="     ";
		for (TableColumns tc : listTableColumns){
			c_str=c_str+tc.getColumnName()+" ,";
			if(tc.getDataType().equalsIgnoreCase("BLOB")){
				hasLob=true;
				v_str=v_str+" empty_blob() ,";
				updateLobSql=updateLobSql+tc.getColumnName()+" ,";
				listTableLobColumns.add(tc);
			}else if(tc.getDataType().equalsIgnoreCase("CLOB")){
				hasLob=true;
				v_str=v_str+" empty_clob() ,";
				updateLobSql=updateLobSql+tc.getColumnName()+" ,";
				listTableLobColumns.add(tc);
			}else if(tc.getDataType().equalsIgnoreCase("NCLOB")){
				hasLob=true;
				v_str=v_str+" empty_clob() ,";
				updateLobSql=updateLobSql+tc.getColumnName()+" ,";
				listTableLobColumns.add(tc);
			}else{
				v_str=v_str+" ? ,";
			}
			for(String pk :listTablePKs){
				if(pk.equalsIgnoreCase(tc.getColumnName())){
					if(tc.getDataType().equalsIgnoreCase("ROWID")){
						where_sql=where_sql+" ROWIDTOCHAR("+pk+") = ? AND";
					}else{
						where_sql=where_sql+" "+pk+" = ? AND";
					}
				}
			}
		}
		insertSql=insertSql+"("+c_str.substring(0, c_str.length()-1)+")";
		insertSql=insertSql+" VALUES ("+v_str.substring(0, v_str.length()-1)+")";
		
		updateLobSql=updateLobSql.substring(0, updateLobSql.length()-1)+" FROM "+table.getTargetTableName()+" WHERE "+where_sql.substring(0, where_sql.length()-4)+" FOR UPDATE";
		_logger.info("--thread "+threadNum+" Insert SQL : "+insertSql);
		_logger.info("--thread "+threadNum+" Update LOB SQL : "+updateLobSql);
	}
	
	public void  getTableCons() throws Exception{
		Connection conn=sourceDataSource.getConnection();
		Statement stmt=conn.createStatement();
		/*String sql="select t.owner,t.table_name,c.COMMENTS from sys.all_all_tables t,sys.all_tab_comments c "+
					" where t.table_name=c.TABLE_NAME "+
					" and t.table_name='"+tableName+"' and t.owner ='"+owner+"' "+
					" order by t.owner,t.table_name";*/
		String sql="select ac.OWNER,ac.CONSTRAINT_NAME,ac.CONSTRAINT_TYPE,ac.TABLE_NAME,acc.COLUMN_NAME,acc.POSITION " +
				" from sys.all_constraints ac ,sys.all_cons_columns acc "  +
				" where ac.OWNER=acc.OWNER  "  +
				" and ac.CONSTRAINT_NAME=acc.CONSTRAINT_NAME "  +
				" and ac.TABLE_NAME='"+table.getTableName().toUpperCase()+"' "  +
				" and ac.owner ='"+owner.toUpperCase()+"'"+
				" and acc.POSITION IS NOT NULL "+
				" order by ac.CONSTRAINT_NAME, acc.POSITION";
		
		_logger.info("--thread "+threadNum+" "+sql);

		ResultSet rs=stmt.executeQuery(sql);
		while(rs.next()){
			_logger.info("--thread "+threadNum+" CONSTRAINT_NAME : "+rs.getString("CONSTRAINT_NAME"));
			if(rs.getString("CONSTRAINT_TYPE").equalsIgnoreCase("P")){
				listTablePKs.add(rs.getString("COLUMN_NAME"));
			}
		}
		_logger.info("--thread "+threadNum+" PK CONSTRAINT_NAME : "+listTablePKs);
		rs.close();
		stmt.close();
	}

}