liulin
2024-08-03 fbc3f392552c14074e1295e4c6e74c5cb87ff8b5
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
package com.lunhan.xxx.host;
 
import com.lunhan.xxx.common.util.ListUtil;
import com.lunhan.xxx.common.util.SerializeUtil;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
public class Mysql2Gauss {
    private static final String Mysql_HOST = "113.250.189.120";
    private static final String Mysql_PORT = "23306";
    private static final String Mysql_USER = "lunhan";
    private static final String Mysql_PASSWORD = "lunhan123";
    private static final String Mysql_DB_NAME = "water_basic";
    private static final String Mysql_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String Mysql_CONNECTION_STR = "jdbc:mysql://" + Mysql_HOST + ":" + Mysql_PORT + "/" + Mysql_DB_NAME + "?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false";
 
    private static final String Gauss_HOST = "113.250.189.120";
    private static final String Gauss_PORT = "57654";
    private static final String Gauss_USER = "lunhan";
    private static final String Gauss_PASSWORD = "lunhan.20240330";
    private static final String Gauss_DB_NAME = "water_basic";
    private static final String Gauss_DRIVER = "org.postgresql.Driver";
    private static final String Gauss_CONNECTION_STR = "jdbc:postgresql://" + Gauss_HOST + ":" + Gauss_PORT + "/" + Gauss_DB_NAME;
 
    private static Connection CONN_Mysql = null;
    private static Connection CONN_Gauss = null;
 
    static {
        //加载驱动
        try {
            Class.forName(Mysql_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获得连接
        try {
            CONN_Mysql = DriverManager.getConnection(Mysql_CONNECTION_STR, Mysql_USER, Mysql_PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //加载驱动
        try {
            Class.forName(Gauss_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获得连接
        try {
            CONN_Gauss = DriverManager.getConnection(Gauss_CONNECTION_STR, Gauss_USER, Gauss_PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) throws SQLException {
        // 创建触发器需要的函数
        GenCodeGauss.execSql(CONN_Gauss, "CREATE OR REPLACE FUNCTION \"public\".\"update_timestamp_column\"()\n" +
                "  RETURNS \"pg_catalog\".\"trigger\" AS $BODY$\n" +
                "BEGIN\n" +
                "NEW.timestamp_column := current_timestamp;\n" +
                "RETURN NEW;\n" +
                "END;\n" +
                "$BODY$\n" +
                "  LANGUAGE plpgsql VOLATILE\n" +
                "  COST 100");
 
        // 获取mysql所有的表名称
        Map<String, String> tables = GenCodeMysql.getTables(CONN_Mysql, Mysql_DB_NAME);
        // 遍历表名,获取表结构
        for (Map.Entry<String, String> table : tables.entrySet()) {
            // 获取mysql表结构
            List<Map<String, Object>> columns = GenCodeMysql.getColumns(CONN_Mysql, table.getKey(), Mysql_DB_NAME);
            // 获取mysql表的主键
            Map<String, String> mapPK = GenCodeMysql.getPK(CONN_Mysql, table.getKey());
            // 获取mysql表的索引
            Map<String, String> mapIndex = GenCodeMysql.getIndex(CONN_Mysql, table.getKey());
 
            System.out.println("从mysql查询表结构:" + table.getKey());
            // 调用gauss创建表结构
            boolean genTale = GenCodeGauss.genTable(CONN_Gauss, table.getKey(), table.getValue(), columns, mapPK, mapIndex);
            System.out.println("向gauss数据库创建表结果:" + genTale);
 
            // 获取mysql表所有数据
            String sqlSelect = "SELECT * FROM " + Mysql_DB_NAME + "." + table.getKey();
            Integer count = 1;
            Integer pageSize = 1;
            String orderBy = "id";
            Object lastId = null;
            while (count > 0) {
                count = 0;
                String sql = sqlSelect + (Objects.isNull(lastId) ? "" : " WHERE " + orderBy + " > " + GenCodeGauss.toDBValue(lastId) + "") + " ORDER BY " + orderBy + " LIMIT " + pageSize;
                List<Map<String, Object>> list = GenCodeMysql.getListAll(CONN_Mysql, sql);
                if (ListUtil.isNullOrEmpty(list)) {
                    continue;
                }
                count = list.size();
                lastId  = list.get(list.size() - 1).get(orderBy);
 
                System.out.println("从mysql获取到数据:" + count);
                // 批量插入gauss数据库
                List<Boolean> insertAll = GenCodeGauss.insertAll(CONN_Gauss, list, table.getKey(), pageSize);
                System.out.println("向gauss数据库插入数据结果:" + SerializeUtil.toJson(insertAll));
            }
        }
    }
}