当前位置:   article > 正文

将MySQL一张表的数据迁移到MongoDB数据库的Java代码示例

java mysql 数据同步mongodb

Java代码:

  1. package com.zifeiy.snowflake.handle.etl.mongodb;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.bson.Document;
  8. import com.mongodb.MongoClient;
  9. import com.mongodb.client.FindIterable;
  10. import com.mongodb.client.MongoCollection;
  11. import com.mongodb.client.MongoCursor;
  12. import com.mongodb.client.MongoDatabase;
  13. import com.zifeiy.snowflake.handle.etl.dbconnection.DatabaseConnectionFactory;
  14. /*
  15. * 这个类用于将MySQL中的一张表的数据原模原样地传输到MongoDB中
  16. */
  17. public class MySQLtoMongoDBDataTransfer {
  18. public boolean transfer(String databaseName, String tableName) {
  19. try {
  20. // mysql connection
  21. Connection connection = DatabaseConnectionFactory.createConnection(databaseName);
  22. Statement statement = connection.createStatement();
  23. ResultSet resultSet = statement.executeQuery("select * from " + tableName);
  24. // mongo connect
  25. MongoClient mongoClient = new MongoClient();
  26. MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName);
  27. MongoCollection<Document> collection = mongoDatabase.getCollection(tableName);
  28. // 首先清空collection
  29. FindIterable<Document> deleteDocuments = collection.find();
  30. MongoCursor<Document> deleteIterator = deleteDocuments.iterator();
  31. while (deleteIterator.hasNext()) {
  32. collection.deleteOne(deleteIterator.next());
  33. }
  34. // begin transfer
  35. int columnCount = -1, count = 0;
  36. List<Document> documents = new ArrayList<Document>();
  37. while (resultSet.next()) {
  38. if (columnCount == -1) {
  39. columnCount = resultSet.getMetaData().getColumnCount();
  40. }
  41. Document document = new Document();
  42. for (int i = 1; i <= columnCount; i ++) {
  43. String columnName = resultSet.getMetaData().getColumnName(i);
  44. Object value = resultSet.getObject(i);
  45. document.append(columnName, value);
  46. }
  47. documents.add(document);
  48. count ++;
  49. if (count % 1000 == 0) { // 每1000条数据执行一次插入
  50. collection.insertMany(documents);
  51. documents = new ArrayList<Document>();
  52. System.out.println("transfer " + count + " lines from mysql to mongodb ...");
  53. }
  54. }
  55. System.out.println("total transfer " + count + " lines!");
  56. DatabaseConnectionFactory.closeConnection(connection);
  57. return true;
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. return false;
  61. }
  62. }
  63. // main for test
  64. public static void main(String[] args) {
  65. new MySQLtoMongoDBDataTransfer().transfer("naturaldb", "NAT_IMBS_T_SIGN");
  66. }
  67. }

其中:

  • databaseName对应MySQL数据库的database,对应MongoDB的database;
    ` tableName对应MySQL数据库的表,对应MongoDB的collection。

转载于:https://www.cnblogs.com/zifeiy/p/10249290.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/675581
推荐阅读
相关标签
  

闽ICP备14008679号