简体   繁体   English

将 mongoDB 集合转换为 mySQL 数据库

[英]convert mongoDB Collection to mySQL Database

I was created my project in spring 4 MVC + Hibernate with MongoDB.我在 spring 4 MVC + Hibernate 中使用 MongoDB 创建了我的项目。 now, I have to convert it into the Hibernate with MySQL.现在,我必须使用 MySQL 将其转换为 Hibernate。 my problem is I have too many collections in MongoDB the format of bson and json.我的问题是我在 MongoDB 中有太多集合,格式为 bson 和 json。 how can I convert that file into MySQL table format?如何将该文件转换为 MySQL 表格式? is that possible?这可能吗?

The documents in your MongoDB collections represent serialised forms of some classes (POJOs, domain object etc) in your project. MongoDB 集合中的文档表示项目中某些类(POJO、域对象等)的序列化形式。 Presumably, you read this data from MongoDB deserialise it into its class form and use it in your project eg display it to end users, use it in calculations, generate reports from it etc.据推测,您从 MongoDB 读取这些数据将其反序列化为类形式并在您的项目中使用它,例如将其显示给最终用户、在计算中使用它、从中生成报告等。

Now, you'd prefer to host that data in MySQL so you'd like to know how to migrate the data from MongoDB to MySQL but since the persistent formats are radically different you are wondering how to do that.现在,您更愿意将这些数据托管在 MySQL 中,因此您想知道如何将数据从 MongoDB 迁移到 MySQL,但由于持久性格式完全不同,您想知道如何做到这一点。

Here are two options:这里有两个选项:

  • Use your application code to read the data from MongoDB, deserialise it into your classes and then write that data into MySQL using JDBC or an ORM mapping layer etc.使用您的应用程序代码从 MongoDB 读取数据,将其反序列化到您的类中,然后使用 JDBC 或 ORM 映射层等将该数据写入 MySQL。
  • Use mongoexport to export the data from MongoDB (in JSON format) and then write some kind of adapter which is capable of mapping this data into the desired format for your MySQL data model.使用mongoexport从 MongoDB 导出数据(以 JSON 格式),然后编写某种适配器,该适配器能够将此数据映射到 MySQL 数据模型所需的格式。

The non functionals (especially for the read and write aspects) will differ between these approaches but fundamentally both approaches are quite similar;这些方法之间的非功能性(尤其是在读取和写入方面)会有所不同,但从根本上说,这两种方法都非常相似; they both (1) read from MongoDB;他们都 (1) 从 MongoDB 读取; (2) map the document data to the relational model; (2) 将文档数据映射到关系模型; (3) write the mapped data into MySQL. (3)将映射后的数据写入MySQL。 The trickiest aspect of this flow is no.这个流程最棘手的方面是没有。 2 and since only you understand your data and your relational model there is no tool which can magically do this for you. 2 并且由于只有您了解您的数据和您的关系模型,因此没有任何工具可以神奇地为您做到这一点。 How would a thirdparty tool be sufficiently aware of your document model and your relational model to be able to perform this transformation for you?第三方工具如何充分了解您的文档模型和关系模型,以便能够为您执行此转换?

You could investigate a MongoDB JDBC driver or use something like Apache Drill to facilitate JDBC queries onto your Mongo DB.您可以研究 MongoDB JDBC 驱动程序或使用 Apache Drill 之类的工具来促进 JDBC 查询到您的 Mongo DB。 Since these could return java.sql.ResultSet you would be dealing with a result format which is more suited for writing to MySQL but it's likely that this still wouldn't match your target relational model and hence you'd still need some form of transformation code.由于这些可能返回java.sql.ResultSet您将处理更适合写入 MySQL 的结果格式,但很可能这仍然与您的目标关系模型不匹配,因此您仍然需要某种形式的转换代码。

Mongodb is a non-relational database, while MySQL is relational. Mongodb 是非关系型数据库,而 MySQL 是关系型数据库。 The key difference is that the non relational database contains documents (JSON objects) which can contain hierarchical structure, where as the relational database expects the objects to be normalised, and broken down into tables.关键区别在于非关系数据库包含可以包含层次结构的文档(JSON 对象),而关系数据库期望对象被规范化并分解为表。 It is therefore not possible to simply convert the bson data from MongoDB into something which MySQL will understand.因此,不可能简单地将 MongoDB 中的 bson 数据转换为 MySQL 能够理解的内容。 You will need to write some code that will read the data from MongoDB and the write it into MySQL.您将需要编写一些代码来从 MongoDB 读取数据并将其写入 MySQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM