@Override
public List<IParam> split() {
List<IParam> paramList = new ArrayList<IParam>() ;
if(!needSplit) {
IParam paramNoSplitted = param.clone();
if(sql.isEmpty()&&!tableName.isEmpty()&&!columns.isEmpty()){
String noSplitSql = "";
if(!where.isEmpty()) {
noSplitSql = String.format(SQL_WITH_WHERE_PATTEN, columns, tableName, where);
} else {
noSplitSql = String.format(SQL_WITHOUT_WHERE_PATTEN, columns, tableName);
}
paramNoSplitted.putValue(ParamKey.sql, noSplitSql);
}
paramList.add(paramNoSplitted);
return paramList;
}
if(autoIncKey.isEmpty()){
return candidateSplitter.split();
}
logger.info("Mysql reader start to split");
try {
conn = DBSource.getConnection(MysqlReader.class, ip, port, dbname);
} catch (Exception e) {
throw new WormholeException(e, JobStatus.READ_CONNECTION_FAILED.getStatus() + MysqlReader.ERROR_CODE_ADD);
}
String rangeSql = "";
if(!where.isEmpty()) {
rangeSql = String.format(RANGE_SQL_WITH_WHERE_PATTERN, autoIncKey,autoIncKey,tableName,where);
} else {
rangeSql = String.format(RANGE_SQL_WITHOUT_WHERE_PATTERN, autoIncKey,autoIncKey,tableName);
}
long min=0,max=0;
try {
logger.debug("RangeSql: " + rangeSql);
ResultSet rs = DBUtils.query(conn, rangeSql);
rs.next();
min = rs.getInt(1);
max = rs.getInt(2);
rs.close();
} catch (Exception e) {
logger.error(e.getMessage(),e);
throw new WormholeException(e,JobStatus.READ_FAILED.getStatus()+MysqlReader.ERROR_CODE_ADD);
}
long start = min - 1;
long end = min - 1 + blockSize;
StringBuilder []sqlArray = new StringBuilder[concurrency];
for(long i = 0; i <= (max-min)/blockSize; i++){
String sqlSplitted = null;
if(!where.isEmpty()) {
sqlSplitted = String.format(SPLIT_SQL_WITH_WHERE_PATTEN, columns, tableName, where, autoIncKey, start, autoIncKey,end);
} else {
sqlSplitted = String.format(SPLIT_SQL_WITHOUT_WHERE_PATTEN, columns, tableName, autoIncKey, start, autoIncKey,end);
}
int index = (int) (i%concurrency);
if(sqlArray[index] == null){
sqlArray[index] = new StringBuilder();
}
sqlArray[index].append(sqlSplitted).append(";") ;
start += blockSize;
end += blockSize;
if(end > max) {
end = max;
}
}
for(int j = 0; j < concurrency; j++){
if(sqlArray[j] == null) {
continue;
}
IParam paramSplitted = param.clone();
paramSplitted.putValue(ParamKey.sql, sqlArray[j].toString());
paramList.add(paramSplitted);
}
logger.info("Mysql reader is splitted successfully");
return paramList;
}