所以我正在尝试从Mysql数据库迁移到neo4j数据库。对于新的 neo4j 数据库,我计划了新闻源功能,如下所述 http://neo4j.com/docs/stable/cypher-cookbook-newsfeed.html这种机制显然与 mysql 不同,mysql 的新闻源只是一堆行。那么有没有办法以 -[status_update]->()-[next]->()-
格式快速将 mysql 行导入到 neo4j 中呢?目前我有 mysql 数据库,如下所示:
user table
+-------+--------------+
| id | username |
+-------+--------------+
| 1 | pewpewlasers |
+-------+--------------+
posts table
+-------+----------+------------+
| id | user_id | status |
+-------+----------+------------+
| 1 | 1 | something |
+-------+----------+------------+
| 2 | 1 | new update |
+-------+----------+------------+
最佳答案
如果将其输出为 CSV,则可以使用LOAD CSV
,如下所示:
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///path/to/file/user.csv" AS line
CREATE (:User {id: toInt(line.id), username: line.username});
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///path/to/file/posts.csv" AS line
CREATE (:Post {id: toInt(line.id), line.status});
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///path/to/file/posts.csv" AS line
MATCH (user:User {id: toInt(line.user_id)})
MATCH (post:Post {id: toInt(line.id)})
CREATE user-[:status_update]->post;
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///path/to/file/posts.csv" AS line
MATCH
(post:Post {id: toInt(line.id)})
(previous_post:Post {id: toInt(line.id) - 1})
CREATE previous_post-[:next]->post;
您也许可以组合其中一些,但由于这里提到的 EAGER 问题,我总是小心地拆分查询:
http://www.markhneedham.com/blog/2014/10/23/neo4j-cypher-avoiding-the-eager/
关于Neo4j CSV 导入状态更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27763377/