将数据从 MySQL 迁移到 Firebase
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18916172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Migrating data into Firebase from MySQL
提问by LesC
I have an existing PHP/MySQL app which I am trying to migrate to AngularJS/Firebase, just as a way to learn these newer technologies.
我有一个现有的 PHP/MySQL 应用程序,我正在尝试将其迁移到 AngularJS/Firebase,作为学习这些新技术的一种方式。
The app has its own schema of tables in MySQL. One such table looks something like:
该应用程序在 MySQL 中有自己的表架构。一张这样的表看起来像:
CREATE TABLE `dictionary` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`word` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`wordmeaning` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`wordlength` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
My question is: how do I migrate this table structure, and the data in it, from MySQL to Firebase?
我的问题是:如何将此表结构及其中的数据从 MySQL 迁移到 Firebase?
I have tried exporting the data into a JSON string via a query such as:
我尝试通过查询将数据导出为 JSON 字符串,例如:
SELECT CONCAT("[",
GROUP_CONCAT(
CONCAT("{id:'",id,"',"),
CONCAT("word:'",word,"',"),
CONCAT("wordmeaning:'",wordmeaning,"',"),
CONCAT("wordlength:'",wordlength,"'}")
)
,"]")
AS json FROM dictionary;
This gives a valid JSON string such as:
这给出了一个有效的 JSON 字符串,例如:
[{id:'1',word:'cat',wordmeaning:'a mammal species',wordlength:'3'}, {id:'2',word:'catapult',wordmeaning:'throwing device',wordlength:'8'}, {id:'3',word:'cart',wordmeaning:'something to carry things in',wordlength:'4'}]
I saved this in a file and tried to import the file from Firebase, using the Import JSON button, to which I got:Error parsing JSON data. Please validate your input. (The JSON string is valid. I checked it at http://jsonviewer.stack.hu/)
我将其保存在一个文件中,并尝试使用“导入 JSON”按钮从 Firebase 导入文件,我得到了:解析 JSON 数据时出错。请验证您的输入。(JSON 字符串是有效的。我在http://jsonviewer.stack.hu/ 上检查过)
Any ideas about what I could be doing wrong? I am assuming that Firebase can handle such structures and the data in them.
关于我可能做错了什么的任何想法?我假设 Firebase 可以处理此类结构及其中的数据。
Thanks.
谢谢。
采纳答案by Frank van Puffelen
Your JSON is not valid. Change it to this to be valid:
您的 JSON 无效。将其更改为有效:
[{"id":"1","word":"cat","wordmeaning":"a mammal species","wordlength":"3"},
{"id":"2","word":"catapult","wordmeaning":"throwing device","wordlength":"8"},
{"id":"3","word":"cart","wordmeaning":"something to carry things in","wordlength":"4"}]
So these are the changes:
所以这些是变化:
- use double-quotes instead of single-quotes
- put (double) quotes around the keys, not just the values
- 使用双引号代替单引号
- 在键周围放置(双)引号,而不仅仅是值
You may want to consider not quoting the values of id
and wordlength
, since these properties seem to be numeric.
您可能需要考虑不引用id
and的值wordlength
,因为这些属性似乎是数字。
Edit
编辑
These two online tools seem to validate the JSON correctly (or at least in line with what Firebase expects):
这两个在线工具似乎正确验证了 JSON(或至少符合 Firebase 的预期):
The second one also pretty prints the JSON, so that might be a reason to prefer one or the other.
第二个也很好地打印了 JSON,所以这可能是选择其中一个的原因。
回答by Chip Dean
Just in case anyone else comes across this question in the future there is a much easier way of doing this.
以防万一其他人将来遇到这个问题,有一个更简单的方法来做到这一点。
You can export your MySQL tables to a CSV file using a tool like SQLYog.
您可以使用 SQLYog 等工具将 MySQL 表导出为 CSV 文件。
Once you have your CSV file you can then convert that to a JSON format using a free online tool like this one: http://www.convertcsv.com/csv-to-json.htm
获得 CSV 文件后,您可以使用如下免费在线工具将其转换为 JSON 格式:http: //www.convertcsv.com/csv-to-json.htm
I just imported 40 tables from MySQL to Firebase in about 15 minutes. Hope that helps someone!
我刚刚在大约 15 分钟内将 40 个表从 MySQL 导入到 Firebase。希望对某人有所帮助!