MongoParseTest.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import com.mongodb.client.model.UpdateOneModel;
  2. import org.bson.*;
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.InputStream;
  6. import java.nio.file.Files;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * MongoParseTest$
  12. *
  13. * @author wukai
  14. * @date 2024/4/19 13:00
  15. */
  16. public class MongoParseTest {
  17. public static void main(String[] args) {
  18. String fileName = "D:\\SYSTEM\\Desktop\\temp\\sync\\oplog.rs.bson";
  19. parse(fileName);
  20. }
  21. /**
  22. * 如果是分片集群,则单独处理
  23. *
  24. * @param filename 文件名
  25. */
  26. public static void parse(String filename) {
  27. File file = new File(filename);
  28. BSONDecoder decoder = new BasicBSONDecoder();
  29. try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()));) {
  30. Map<String, List<BasicBSONObject>> iMap = new HashMap<>(16);
  31. // Map<String, List<Pair<Bson, Document>>> uMap = new HashMap<>(16);
  32. Map<String, List<UpdateOneModel<Document>>> mMap = new HashMap<>(16);
  33. long records = 0;
  34. long iIndex = 0;
  35. long uIndex = 0;
  36. long cIndex = 0;
  37. while (inputStream.available() > 0) {
  38. // if (records % 10000 == 0) {
  39. // System.err.println("正在解析文件:{},当前处理记录数:{}" + filename + records);
  40. // }
  41. BSONObject obj = decoder.readObject(inputStream);
  42. if (obj == null) {
  43. break;
  44. }
  45. //操作符
  46. String op = obj.get("op").toString();
  47. //数据库和表名
  48. String ns = obj.get("ns").toString();
  49. if (ns.startsWith("config.system.")) {
  50. continue;
  51. }
  52. BasicBSONObject bson = (BasicBSONObject) obj.get("o");
  53. if ("i".equals(op)) {
  54. iIndex++;
  55. } else if ("u".equals(op)) {
  56. uIndex++;
  57. // // 数据更新
  58. //// List<Pair<Bson, Document>> updates = uMap.get(ns);
  59. // List<UpdateOneModel<Document>> us = mMap.get(ns);
  60. // if (us == null) {
  61. // us = new ArrayList<>();
  62. // }
  63. //
  64. // System.err.println(obj);
  65. //
  66. // BasicBSONObject bson2 = (BasicBSONObject) obj.get("o2");
  67. // Bson f = Filters.eq("_id", bson2.get("_id"));
  68. // BasicBSONObject set = (BasicBSONObject) bson.get("$set");
  69. // Document x = new Document("$set", set);
  70. // us.add(new UpdateOneModel<>(f, x));
  71. // mMap.put(ns, us);
  72. // Pair<Bson, Document> pair = new Pair<>(f, x);
  73. // updates.add(pair);
  74. // uMap.put(ns, updates);
  75. } else if ("c".equals(op)) {
  76. cIndex++;
  77. System.err.println(obj);
  78. // MongoDatabase database = getDatabase4Ns(mongoClient, ns, dbs);
  79. if (bson.get("commitIndexBuild") != null) {
  80. // MongoCollection<Document> coll = database.getCollection(bson.get("commitIndexBuild").toString());
  81. // BasicBSONList indexes = (BasicBSONList) bson.get("indexes");
  82. //
  83. // List<IndexModel> indexModels = new ArrayList<>();
  84. // //组合索引
  85. //
  86. // for (int i = 0; i < indexes.size(); i++) {
  87. // BasicDBObject keyObj = new BasicDBObject();
  88. // BasicBSONObject index = (BasicBSONObject) indexes.get(i);
  89. // BasicBSONObject keys = (BasicBSONObject) index.get("key");
  90. // for (String s : keys.keySet()) {
  91. // keyObj.put(s, keys.get(s));
  92. // }
  93. //
  94. // //添加配置
  95. // IndexOptions indexOptions = new IndexOptions();
  96. // if (index.get("unique") != null) {
  97. // indexOptions.unique(index.getBoolean("unique"));
  98. // }
  99. // if (index.get("background") != null) {
  100. // indexOptions.background(index.getBoolean("background"));
  101. // }
  102. // //索引名称
  103. // indexOptions.name(index.get("name").toString());
  104. // indexModels.add(new IndexModel(keyObj, indexOptions));
  105. // }
  106. // coll.createIndexes(indexModels);
  107. } else if (bson.get("createIndexes") != null) {
  108. // MongoCollection<Document> coll = database.getCollection(bson.get("createIndexes").toString());
  109. //
  110. // List<IndexModel> indexModels = new ArrayList<>();
  111. // //组合索引
  112. //
  113. // BasicDBObject keyObj = new BasicDBObject();
  114. // BasicBSONObject keys = (BasicBSONObject) bson.get("key");
  115. // for (String s : keys.keySet()) {
  116. // keyObj.put(s, keys.get(s));
  117. // }
  118. //
  119. // //添加配置
  120. // IndexOptions indexOptions = new IndexOptions();
  121. // if (bson.get("unique") != null) {
  122. // indexOptions.unique(bson.getBoolean("unique"));
  123. // }
  124. // if (bson.get("background") != null) {
  125. // indexOptions.background(bson.getBoolean("background"));
  126. // }
  127. // //索引名称
  128. // indexOptions.name(bson.get("name").toString());
  129. // indexModels.add(new IndexModel(keyObj, indexOptions));
  130. // coll.createIndexes(indexModels);
  131. }
  132. }
  133. records++;
  134. }
  135. System.err.printf("新增:%s\t更新:%s\t索引:%s", iIndex, uIndex, cIndex);
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. }