分表SQL

本文介绍了一个Java工具,该工具能够将一张大型表的创建SQL语句拆分成多个针对分表的SQL语句。通过简单的替换表名并重复执行,可以快速生成大量用于水平分表的SQL脚本。

以下代码是,根据新建的一张表的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();
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值