首页
技术
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

handazao

养家糊口
首页
技术
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Docker使用

  • 数据库相关

    • Mysql事务
    • Mysql
    • Mysql 高版本 group by 错误
    • Mongo 文件迁移
    • centos7 Mysql安装
  • Java相关

  • Linux学习

  • 工具

  • vue3

  • Git

  • 技术
  • 数据库相关
handazao
2020-10-10

Mongo 文件迁移

艹,打包部署程序,配置文件写错啦.mongo连错库了

    @RequestMapping(value = "/moveFile")
    public ModelMap moveFile(String fileId, String startTime, String endTime) {

        Long startTimes = System.currentTimeMillis();
        //目的源
        MongoCredential credentialTo = MongoCredential.createCredential("ceshi", "ceshi", "123456".toCharArray());
        MongoClient mongoClientTo = new MongoClient(new ServerAddress("192.168.1.15", 27017), Arrays.asList(credentialTo));
        MongoDatabase mongoDatabaseTo = mongoClientTo.getDatabase("ceshi");
        //复制源
        MongoCredential credential = MongoCredential.createCredential("root", "travel_file", "123456".toCharArray());
        MongoClient mongoClient = new MongoClient(new ServerAddress("127.0.0.1", 27017), Arrays.asList(credential));
        MongoDatabase mongoDatabaseFrom = mongoClient.getDatabase("travel_file");

        log.info("Mongo Connect to database successfully");

        int successfulFiles = 0;
        int successfulChunks = 0;
        int existNum = 0;
        //数据库集合
        String[] collections = {"fs.files", "fs.chunks"};

        if (StringUtils.isBlank(startTime)) {
            startTime = "2020-09-22 11:00:00";
        }
        if (StringUtils.isBlank(endTime)) {
            endTime = "2020-09-25 12:00:00";
        }
        Example uploadExample = new Example(UploadFile.class);
        Example.Criteria criteria = uploadExample.createCriteria();
        criteria.andEqualTo("isDel", BaseEnum.notDel.getCode())
                .andEqualTo("isValid", 1)
                .andBetween("createTime", startTime, endTime);
        if(StringUtils.isNotBlank(fileId)){
            criteria.andEqualTo("filedId",fileId);
        }
        uploadExample.setOrderByClause("create_time desc");
        List<UploadFile> uploadFileList = uploadFileService.selectByExample(uploadExample);
        log.info("目标文件一个获取了" + uploadFileList.size() + "个");
        Iterator<UploadFile> iterator = uploadFileList.iterator();
        while (iterator.hasNext()) {
            UploadFile uploadFile = iterator.next();
            List<GridFSDBFile> list = gridFSRepository.findOne(uploadFile.getFiledId());
            if (CollectionUtils.isEmpty(list)) {
                for (int i = 0; i < collections.length; i++) {
                    int a = 0;
                    MongoCollection<Document> fromCollection = mongoDatabaseFrom.getCollection(collections[i]);

                    MongoCollection<Document> toCollection = mongoDatabaseTo.getCollection(collections[i]);

                    String id = i == 0 ? "_id" : "files_id";
                    BasicDBObject dbObject = new BasicDBObject();
                    dbObject.put(id, new ObjectId(uploadFile.getFiledId()));
                    FindIterable<Document> findIterable = fromCollection.find(dbObject);
                    MongoCursor<Document> mongoCursor = findIterable.iterator();
                    while (mongoCursor.hasNext()) {
                        Document fromDocument = mongoCursor.next();
                        try {
                            toCollection.insertOne(fromDocument);
                            fromCollection.updateOne(Filters.eq("_id", fromDocument.get("_id")), new Document("$set", new Document("transfered", 1)));
                            log.info("集合" + collections[i] + "=====" + fromDocument.get("_id") + " transfer successful , added flag transfered ====" + a++);
                            if (i == 0) {
                                successfulFiles++;
                            } else {
                                successfulChunks++;
                            }
                        } catch (Exception e) {
                            log.error("集合" + collections[i] + "=====" + fromDocument.get("_id") + " transfer filed , " + e.getLocalizedMessage());
                        }
                    }
                }
            } else {
                existNum++;
                log.info("===========" + uploadFile.getFiledId() + "文件已经存在===="+existNum);
            }
        }
        Long endTimes = System.currentTimeMillis();
        log.info("集合files共处理了" + successfulFiles + "个文档,集合files共处理了" + successfulChunks + "个文档,耗时:" + (endTimes - startTimes) / 1000 + "s");

        return ReturnUtil.Success("目标文件一个获取了" + uploadFileList.size() + "个,已经存在了"+existNum+"个,集合files共处理了" + successfulFiles + "个文档,集合files共处理了" + successfulChunks + "个文档");
    }
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
#Mongo
上次更新: 2022/12/06, 11:10:28
Mysql 高版本 group by 错误
centos7 Mysql安装

← Mysql 高版本 group by 错误 centos7 Mysql安装→

最近更新
01
pre-push
08-07
02
commit-msg
08-07
03
pre-commit
08-07
更多文章>
Theme by Vdoing | Copyright © 2020-2024 handazao | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式