Browse Source

调整导入程序

Airhead 7 years ago
parent
commit
e4e335be42

+ 38 - 0
src/com/yihu/util/mysql2Oracle/MyThread.java

@ -0,0 +1,38 @@
package com.yihu.util.mysql2Oracle;
public class MyThread extends Thread {
    private String tableName;
    private String index;
    private int total;
    private int maxId;
    private int rows;
    public MyThread(String tableName, String index, int maxId, int row, int total) throws Exception {
        this.tableName = tableName;
        this.maxId = maxId;
        this.rows = row;
        this.total = total;
        this.index = index;
    }
    public void run() {
        int count = total / rows + 1;
        long start = System.currentTimeMillis();
        try {
            for (int i = 0; i < count; i++) {
                TranslateMysqlToOracle.exportSql(tableName, index, maxId, i, rows); //查询mysql 导入oracle
            }
            long end = System.currentTimeMillis();
            long coout = end - start;
            int num = total - maxId;
            System.out.println("表" + tableName + " ,总导入数据" + num + " ,耗时:" + coout);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.run();
    }
}

+ 0 - 38
src/com/yihu/util/mysql2Oracle/MyThread2.java

@ -1,38 +0,0 @@
package com.yihu.util.mysql2Oracle;
public class MyThread2 extends Thread {
    private String[] tables;
    private String  index;
    private int total;
    private int maxId;
    private int rows;
    public MyThread2(String[] tables, String index,int maxId,  int row,int total)  throws Exception{
        this.tables = tables;
        this.maxId = maxId;
        this.rows = row;
        this.total = total;
        this.index = index;
    }
    public void run() {
        int count = total/rows + 1;
        long start = System.currentTimeMillis();
        try {
            for (int i= 0;i<count ;i++){
                TranslateMysqlToOracle.exportSql(tables,index,maxId,i,rows); //查询mysql 导入oracle
            }
            long end = System.currentTimeMillis();
            long coout = end - start;
            int num = total-maxId;
            System.out.println("表"+tables[0] + " ,总导入数据"+ num + " ,耗时:"+coout);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.run();
    }
}

+ 21 - 28
src/com/yihu/util/mysql2Oracle/MysqlToOracleApplication.java

@ -1,38 +1,31 @@
package com.yihu.util.mysql2Oracle;
import java.util.ArrayList;
import java.util.List;
public class MysqlToOracleApplication {
    public static void main(String[] args) throws Exception {
        String[] tables1 = {"OutPatient"};
        String[] tables2 = {"Outpatient_Diag"};
        String[] tables3 = {"Outpatient_Drug"};
        String[] tables4 = {"Outpatient_Fee"};
        String[] tables5 = {"Inpatient_Drug"};
        String[] tables6 = {"Inpatient_Outdiag"};
        String[] tables7 = {"Inpatient_Longorder"};
        String[] tables8 = {"Inpatient_Fee"};
        String[] tables9 = {"Eds_Disp_Pres_Master"};
        MyThread2 MyThread21 = new MyThread2(tables1,"PK_Outpatient",210001,10000,566520);
        MyThread2 MyThread22 = new MyThread2(tables2,"PK_Outpatient_Diag ",390001,10000,1147364);
        MyThread2 MyThread23 = new MyThread2(tables3,"PK_Outpatient_Drug",130001,10000,3486629);
        MyThread2 MyThread24 = new MyThread2(tables4,"PK_Outpatient_Fee",150001,10000,1417369);
        MyThread2 MyThread25 = new MyThread2(tables5,"PK_Inpatient_Drug",170001,10000,3605361);
        MyThread2 MyThread26 = new MyThread2(tables6,"PK_Inpatient_Outdiag",430001,10000,1374793);
        MyThread2 MyThread27 = new MyThread2(tables7,"PK_Inpatient_Longorder",170001,10000,406293);
        MyThread2 MyThread28 = new MyThread2(tables8,"PK_Inpatient_Fee",170001,10000,1417369);
        MyThread2 MyThread29 = new MyThread2(tables9,"PK_Eds_Disp_Pres_Master",140001,10000,770550);
        TableInfo[]  tables = {
                new TableInfo("BaseInfo", "PK_BaseInfo", 0, 676400),
//                new TableInfo("OutPatient", "PK_Outpatient", 210001, 566520),
//                new TableInfo("Outpatient_Diag", "PK_Outpatient_Diag", 390001, 1147364),
//                new TableInfo("Outpatient_Drug", "PK_Outpatient_Drug", 130001, 3486629),
//                new TableInfo("Outpatient_Fee", "PK_Outpatient_Fee", 150001, 1417369),
//                new TableInfo("Inpatient_Drug", "PK_Outpatient", 170001, 3605361),
//                new TableInfo("Inpatient_Outdiag", "PK_Outpatient", 430001, 1374793),
//                new TableInfo("Inpatient_Longorder", "PK_Outpatient", 170001, 406293),
//                new TableInfo("Inpatient_Fee", "PK_Outpatient", 170001, 1417369),
//                new TableInfo("Eds_Disp_Pres_Master", "PK_Outpatient", 140001, 770550)
        };
        List<MyThread> threadList = new ArrayList<>();
        for (TableInfo table : tables) {
            MyThread myThread = new MyThread(table.getTableName(), table.getPk(), table.getOffset(), 10000, table.getTotal());
            threadList.add(myThread);
            myThread.start();
        }
        MyThread21.start();
        MyThread22.start();
        MyThread23.start();
        MyThread24.start();
        MyThread25.start();
        MyThread26.start();
        MyThread27.start();
        MyThread28.start();
        MyThread29.start();
    }
}

+ 48 - 0
src/com/yihu/util/mysql2Oracle/TableInfo.java

@ -0,0 +1,48 @@
package com.yihu.util.mysql2Oracle;
public class TableInfo {
    String tableName;
    String pk;
    int offset;
    int total;
    public TableInfo(String tableName, String pk, int offset, int total) {
        this.tableName = tableName;
        this.pk = pk;
        this.offset = offset;
        this.total = total;
    }
    public String getTableName() {
        return tableName;
    }
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
    public String getPk() {
        return pk;
    }
    public void setPk(String pk) {
        this.pk = pk;
    }
    public int getOffset() {
        return offset;
    }
    public void setOffset(int offset) {
        this.offset = offset;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
}

+ 38 - 42
src/com/yihu/util/mysql2Oracle/TranslateMysqlToOracle.java

@ -1,28 +1,26 @@
package com.yihu.util.mysql2Oracle;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
 *    查询mysql 导入执行 到oracle
 * 查询mysql 导入执行 到oracle
 */
public class TranslateMysqlToOracle {
    /**
     *
     * @param tables
     * @param tableName
     * @param startId
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    public static List<String> exportSql(String[] tables, String index,  int startId, int page, int rows) throws Exception {
    public static List<String> exportSql(String tableName, String index, int startId, int page, int rows) throws Exception {
        List<String> sqlList = new ArrayList<>();
        String[] tableNames = tables;
//        String[] tableNames = tables;
        String indexName = index;
        //TODO 修改链接
@ -34,44 +32,43 @@ public class TranslateMysqlToOracle {
        Connection connection = DriverManager.getConnection(url, user, password);
        int maxId = startId + (page * rows);
        for (String tableName : tableNames) {
            String sql = "select * from " + tableName + " where inner_id> "+ maxId + " limit "+ rows ;
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            while (resultSet.next()) {
                String insertSql = "";
                if (!indexName.isEmpty()) {
                    insertSql = "INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(" + tableName + "," + indexName + ")*/ INTO " + tableName + " ";
                } else {
                    insertSql = "INSERT INTO " + tableName + " ";
                }
                StringBuilder namesStr = new StringBuilder();
                StringBuffer valuesStrBuf = new StringBuffer();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i);
                    if (!"inner_id".equals(columnName)) {
                        String columnTypeName = metaData.getColumnTypeName(i);
                        Object value = resultSet.getObject(i);
                        //System.out.println(columnName + " - " + columnTypeName + " - " + value);
                        namesStr.append(columnName).append(",");
                        valuesStrBuf = joinValuesFragment(valuesStrBuf, columnTypeName, value);
                    }
        String sql = "select * from " + tableName + " where inner_id> " + maxId + " limit " + rows;
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();
        while (resultSet.next()) {
            String insertSql = "";
            if (!indexName.isEmpty()) {
                insertSql = "INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(" + tableName + "," + indexName + ")*/ INTO " + tableName + " ";
            } else {
                insertSql = "INSERT INTO " + tableName + " ";
            }
            StringBuilder namesStr = new StringBuilder();
            StringBuffer valuesStrBuf = new StringBuffer();
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                String columnName = metaData.getColumnName(i);
                if (!"inner_id".equals(columnName)) {
                    String columnTypeName = metaData.getColumnTypeName(i);
                    Object value = resultSet.getObject(i);
                    //System.out.println(columnName + " - " + columnTypeName + " - " + value);
                    namesStr.append(columnName).append(",");
                    valuesStrBuf = joinValuesFragment(valuesStrBuf, columnTypeName, value);
                }
                String valuesStr = valuesStrBuf.toString();
                namesStr = new StringBuilder(namesStr.substring(0, namesStr.length() - 1));
                valuesStr = valuesStr.substring(0, valuesStr.length() - 1);
                insertSql += "(" + namesStr + ") VALUES (" + valuesStr + ")";
            }
            String valuesStr = valuesStrBuf.toString();
            namesStr = new StringBuilder(namesStr.substring(0, namesStr.length() - 1));
            valuesStr = valuesStr.substring(0, valuesStr.length() - 1);
            insertSql += "(" + namesStr + ") VALUES (" + valuesStr + ")";
//                String sqlFilePath = directoryPath + tableName + ".sql";
//                writeFileContent(sqlFilePath, insertSql);
                sqlList.add(insertSql);
            }
            resultSet.close();
            preparedStatement.close();
            sqlList.add(insertSql);
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        exportSqlToOracle(sqlList);//执行sql到oracle
        return sqlList;
@ -102,7 +99,7 @@ public class TranslateMysqlToOracle {
                //执行批量执行
                index++;
                if (index % 1000 == 0) {
                    System.out.println("index:"+ index);
                    System.out.println("index:" + index);
                    preparedStatement.executeBatch();
                }
            }
@ -113,7 +110,7 @@ public class TranslateMysqlToOracle {
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (connection!=null) {
            if (connection != null) {
                connection.close();
            }
        }
@ -145,5 +142,4 @@ public class TranslateMysqlToOracle {
    }
}