以下代码是,根据新建的一张表的sql语句,程序跑出多个的分表sql语句,仅供参考
/**
* @author liusn19096
*
*/
public class SeparateTableSql {
/**
* 文件编码
*/
private static final String ENCODE = "GBK";
/**
* @param args
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException {
// 读的文件路径,一个建表的sql语句的文件
String readFilePath = "D:\\svn\\tenant\\trunk\\Sources\\sqlsrc\\stockDiagnose\\1.0.0\\aaa.sql";
File readFile = new File(readFilePath);
// 写文件路径,多个分表的建表sql语句
String writeFilePath = "D:\\svn\\tenant\\trunk\\Sources\\sqlsrc\\stockDiagnose\\1.0.0\\aaa_1.sql";
File writeFile = new File(writeFilePath);
// 创建文件路径
if (!writeFile.getParentFile().exists()) {
writeFile.getParentFile().mkdirs();
}
try {
// 写文件
BufferedOutputStream buffOut = new BufferedOutputStream(new FileOutputStream(writeFilePath));
String readLine;
String writeLine;
// 循环,分表的数量
for (int i = 0; i < 50; i++) {
// 每次都重新读文件
FileInputStream stream = new FileInputStream(readFile);
InputStreamReader read = new InputStreamReader(stream, ENCODE);
BufferedReader bufferedReader = new BufferedReader(read);
System.err.println("i:" + i);
// readLine会去掉换行符
while ((readLine = bufferedReader.readLine()) != null) {
// check deal this line data
if (readLine.contains("fs_period_calculate_result")) {
writeLine = readLine.replace("fs_period_calculate_result", "fs_period_calculate_result_" + i);
} else {
writeLine = readLine;
}
// 写入文件
buffOut.write(writeLine.getBytes());
// 换行
buffOut.write(("\n").getBytes());
// 清缓存写下一行
buffOut.flush();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文介绍了一个Java工具,该工具能够将一张大型表的创建SQL语句拆分成多个针对分表的SQL语句。通过简单的替换表名并重复执行,可以快速生成大量用于水平分表的SQL脚本。
517

被折叠的 条评论
为什么被折叠?



