liulin
2024-08-02 39815aaeb317c656eac4c67555ffb1148fb081b3
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
package com.lunhan.xxx.host;
 
import com.lunhan.xxx.common.util.ListUtil;
import com.lunhan.xxx.common.util.SerializeUtil;
 
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
public class Mysql2Gauss {
    private static final String Mysql_DB = "water_basic";
    private static final String Gauss_DB = "water_basic";
    public static void main(String[] args) {
        // 获取mysql所有的表名称
        Map<String, String> tables = GenCodeMysql.getTables(Mysql_DB);
        // 遍历表名,获取表结构
        for (Map.Entry<String, String> table : tables.entrySet()) {
            // 获取mysql表结构
            List<Map<String, Object>> columns = GenCodeMysql.getColumns(table.getKey(), Mysql_DB);
            // 获取mysql表的主键
            Map<String, String> mapPK = GenCodeMysql.getPK(table.getKey());
            // 获取mysql表的索引
            Map<String, String> mapIndex = GenCodeMysql.getIndex(table.getKey());
 
            System.out.println("从mysql查询表结构:" + table.getKey());
            // 调用gauss创建表结构
            boolean genTale = GenCodeGauss.genTable(table.getKey(), table.getValue(), columns, mapPK, mapIndex);
            System.out.println("向gauss数据库创建表结果:" + genTale);
 
            // 获取mysql表所有数据
            String sqlSelect = "SELECT * FROM " + Mysql_DB + "." + table.getKey();
            Integer count = 1;
            Integer pageSize = 500;
            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(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(list, table.getKey(), pageSize);
                System.out.println("向gauss数据库插入数据结果:" + SerializeUtil.toJson(insertAll));
            }
        }
    }
}