How to export data from neo4j to a MySQL table - neo4j

I have below data in my neo4j database which I want to insert into mysql table using jdbc.
"{""id"":7512,""labels"":[""person1""],""properties"":{""person1"":""Nishant"",""group_uuid"":""6b27c9c8-4d5b-4ebc-b8c2-667bb159e029""}}"
"{""id"":7513,""labels"":[""person1""],""properties"":{""person1"":""anish"",""group_uuid"":""6b27c9c8-4d5b-4ebc-b8c2-667bb159e029""}}"
"{""id"":7519,""labels"":[""person1""],""properties"":{""person1"":""nishant"",""group_uuid"":""6b27c9c8-4d5b-4ebc-b8c2-667bb159e029""}}"
"{""id"":7520,""labels"":[""person1""],""properties"":{""person1"":""xiaoyi"",""group_uuid"":""9d7d4bf6-6db6-4cf2-8186-d8d0621a58c5""}}"
"{""id"":7521,""labels"":[""person1""],""properties"":{""person1"":""pavan"",""group_uuid"":""3ddc954a-16f5-4c59-a94a-b262f9784211""}}"
"{""id"":7522,""labels"":[""person1""],""properties"":{""person1"":""jose"",""group_uuid"":""6b27c9c8-4d5b-4ebc-b8c2-667bb159e029""}}"
"{""id"":7523,""labels"":[""person1""],""properties"":{""person1"":""neil"",""group_uuid"":""9d7d4bf6-6db6-4cf2-8186-d8d0621a58c5""}}"
"{""id"":7524,""labels"":[""person1""],""properties"":{""person1"":""menish"",""group_uuid"":""9d7d4bf6-6db6-4cf2-8186-d8d0621a58c5""}}"
"{""id"":7525,""labels"":[""person1""],""properties"":{""person1"":""ankur"",""group_uuid"":""3ddc954a-16f5-4c59-a94a-b262f9784211""}}"
Desired Output in mysql database table.
id,name,group_id
7525,ankur,3ddc954a-16f5-4c59-a94a-b262f9784211
7524,menish,9d7d4bf6-6db6-4cf2-8186-d8d0621a58c5
...

Since you did not provide much info in your question, here is a general approach for exporting from neo4j to MySQL.
Execute a Cypher query using one of the APOC export to CSV procedures to export the data intended for the table to a CSV file.
Import from the CSV file into MySQL. (E.g., here is a tutorial.)

Related

Neo4j dumps could be merged in single database?

I have two database instances: one remote and one local. I want to dump the local database and merge it with remote (structure of nodes & relationships are the same). How may I do it? Simple neo4j-admin dump & load will help?
This is not possible with the dump & load command.
To do what you want, you need to export your local database as CSVs and then make some LOAD CSV queries with MERGE command.
To export your database as CSVs, take a look at APOC with the export procedures : https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_export_import

Export Neo4j db creation statements

Such as the example movies database
Is it possible to extract the single statement that creates the current database with all it's properties, relations and nodes?
You can install APOC procedures and use apoc.export.cypher.all. From the docs:
apoc.export.cypher.all(file,config) - exports whole database incl.
indexes as cypher statements to the provided file

how to export neo4j data of database A to database B?

I have two cases:
case 1:export a part of data in neo4j database A to database B,like data of Label "Person" in database A ,I wanna export "Person" data from A to B
case 2: export whole data from A to B
so how to deal with these two cases? thanks
APOC allows to export the full graph or subgraphs into a cypher file consisting of create statements, see https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_export_to_cypher_script for details.
The other option would be access the other database via the neo4j jdbc driver and use apoc.load.jdbc to retrieve data from there.

Migrating mysql data to neo4j database

I wanted to migrate data from Mysql to neo4j. I'm using Neo4j 2.1.2 64 bit installer on 64 bit windows machine.
I followed the blog in the link http://maxdemarzi.com/2012/02/28/batch-importer-part-2/#more-660 where migrating data from postgreSQL is well well explained.
Even I took the same example and created the sames tables in mysql. After creating nodes and relationship tables in mysql, i exported them as a csv file . So that I can use them in the batch import command.
Here all my fields are varchar and row_number() fiels is also a varchar field.
I used the below command to export mysql's relationship table into myrels.csv file (same thing for nodes table):
SELECT *
INTO OUTFILE 'D:/Tech_Explorations/BigData_Related/Neo4j/mqytoneo4j/myrels.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
FROM
(
SELECT 'start' AS `start`, 'end' AS `end`,'type' AS `type`,'status' AS `status`
UNION ALL
SELECT `start`, `end`,`type`,`status`
FROM `vouch_rels`
) `sub_query`;
Used below query to load the mynodes.csv and myrels.csv o neo4j:
java -server -Xms1024M -jar D:/Neo4j/target/batch-import-jar-with-dependencies.jar
neo4j/data/graph.db mynodes.csv myrels.csv
When i executed the above batch import query , it's giving me an error saying
Exception in thread "main" java.lang.NumberFormatException: For input string: "1
,"1","python,confirmed"
Where "1,"1","python,confirmed" is row in the myrels.csv.
The above error might be because of some datatype or csv file issue but I'm not able to figure it out. Even I tried with changing different csv load options while loading from mysql to csv file. But still getting the same error.
MySQL to Neo4j migration is not a straightforward export-load problem. The property graph needs to be clear for Neo4j and should be consistent with the MySQL schema. There is no way to automatically generate Neo4j property graph from MySQL schema to my knowledge. After the 2 schemas are well defined you can write your own migrations in any programming language.
The python way to do the migration
py2neo is a Python library that makes it easy to write migrations as it provides a ton of useful functions, option to run cypher queries, transaction support, etc.
I used py2neo in a project to migrate around 100MB data from MySQL to Neo4j. Here is the sample code for reference along with documentation. The data is not provided but the schema of both MySQL and Neo4j property graph is given.
P.S: I might have digressed from trying to address your problem. But I have written this answer as it might help readers who are looking to solve the MySQL to Neo4j migration problem using Python.
I'd suggest looking at the LOAD CSV Cypher option. There are detailed docs on the Neo4j website.
Basically, you can use a Cypher query like the following to import your data.
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/path/to/your.csv" AS csvLine
MATCH (person:Person { id: toInt(csvLine.personId)}),(movie:Movie { id: toInt(csvLine.movieId)})
CREATE (person)-[:PLAYED { role: csvLine.role }]->(movie)
If you wish to proceed with the Java batch import tool then I believe your file needs to be tab delimited not comma delimited.

Is there a tool to dump a Neo4j graph as Cypher and re-load it from Cypher?

Everyone familiar with MySQL has likely used the mysqldump command which can generate a file of SQL statements representing both the schema and data in a MySQL database.
These SQL text files are commonly used for many purposes: backups, seeding replicas, copying databases between installations (- copy prod DBs to staging environments etc) and others.
Is there a similar tool for Neo4j that can dump an entire graph into a text file of Cypher statements, that when executed on an empty database would reconstruct the original data?
Thanks.
In neo4j version 2 (e.g. 2.0.0M3), using neo4j-shell, you can use the command
dump
which will create the cypher statements (pretty much like mysqldump would do. To read in the file you can use
cat dump.cql | neo4j-shell
Cypher is just a query language for Neo4J just as SQL is for MySQL or other relational databases. If you wish to transfer the db, then you just need to copy the folder containing the database files. Simple.
For example my folder simple-graph contains all the db files. Just copy the folder and store it at some other location. You can directly start using it as:
GraphDatabaseServiceraphDb = new EmbeddedGraphDatabase(DB_PATH);//DB_PATH is path to the new location
You can use the procedure apoc.export.cypher.all() to dump all the data in your database.
For example, you can dump the database into a single file called dump-file.cypher:
neo4j#neo4j> CALL apoc.export.cypher.all('dump-file.cypher');
For details of the procedure, please see the documentation: https://neo4j.com/labs/apoc/4.4/overview/apoc.export/apoc.export.cypher.all/.

Resources