Use CSV to populate Neo4j - neo4j

I am very new for Neo4j. I am a learner of this graph database. I need to load a csv file into Neo4j database. I am trying from 2 days,I couldn't able to find good information of reading the csv file in to Neo4j. Please suggest me wil sample code or blogs of reading csv file into Neo4j.
Example:
Suppose if i have a csv file in This way how can we read it into Neo4j
id name language
1 Victor Richards West Frisian
2 Virginia Shaw Korean
3 Lois Simpson Belarusian
4 Randy Bishop Hiri Motu
5 Lori Mendoza Tok Pisin

You may want to try https://github.com/sroycode/neo4j-import
This populates data directly from a pair of CSV files ( entries must be COMMA separated )
To build: (you need maven)
sh build.sh
The nodes file has a mandatory field id and any other fields you like
NODES.txt
id,name,language
1,Victor Richards,West Frisian
2,Virginia Shaw,Korean
3,Lois Simpson,Belarusian
The relationships file has 3 mandatory fields from,to,type. Assuming you have a field age ( long integer), and info, the relations file will look like
RELNS.txt
from,to,type,age#long,info
1,2,KNOWS,10,known each other from school
1,3,CLUBMATES,5,member of country club
Running:
sh run.sh graph.db NODES.txt RELNS.txt
will create graph.db in the current folder which you can copy to the neo4j data folder.
Note:
If you are using neo4j later than 1.6.* , please add this line in conf/neo4j.properties
allow_store_upgrade = true
Have fun.

Please take a look at https://github.com/jexp/batch-import
Can be used as starting point

There is nothing available to generically load CSV data into Neo4j because the source and destination data structures are different: CSV data is tabular whereas Neo4j holds graph data.
In order to achieve such an import, you will need to add a separate step to translate your tabular data into some form of graph (e.g. a tree) before it can be loaded into Neo4j. Taking the tree structure further as an example, the page below shows how XML data can be converted into Cypher which may then be directly executed against a Neo4j instance.
http://geoff.nigelsmall.net/xml2graph/
Please feel free to use this tool if it helps (bear in mind it can only deal with small files) but this will of course require you to convert your CSV to XML first.
Cheers
Nigel

there is probably no known CSV importer for neo4j, you must import it yourself:
i usually do it myself via gremlin's g.loadGraphML(); function.
http://docs.neo4j.org/chunked/snapshot/gremlin-plugin.html#rest-api-load-a-sample-graph
i parse my data with some external script into the xml syntax and load the particular xml file. you can view the syntax here:
https://raw.github.com/tinkerpop/gremlin/master/data/graph-example-1.xml
parsing an 100mb file takes few minutes.
in your case what you need to do is a simple bipartite graph with vertices consisting of users and languages, and edges of "speaks". if you know some programming, then create user nodes with parameters id, name | unique language nodes with parameters name | relationships where you need to connect each user with the particular language. note that users can be duplicite whereas languages can't.

I believe your question is too generic. What does your csv file contain? Logical meaning of the contents of a csv file can vary very much. An example of two columns with IDs, which would represent entities connected to each other.
3921 584
831 9891
3841 92
...
In this case you could either write a BatchInserter code snippet which would import it faster, see http://docs.neo4j.org/chunked/milestone/batchinsert.html.
Or you could import using regular GraphDatabaseService with transaction sizes of a couple of thousands inserts for performance. See how to setup and use the graph db at http://docs.neo4j.org/chunked/milestone/tutorials-java-embedded.html.

Related

Facing problem to importing csv file with bulk importer in neo4j

I am trying to load nodes and its relations from csv file using neo4j bulk importer my script like this
neo4j-admin import \
--id-type=string \
--nodes:AGENT="nodes_AGENT_C_20190610.csv" \
--nodes:CUSTOMER="nodes_CUSTOMER_C_20190610.csv" \
--relationships:CASHOUT="relcashoutTest-header.csv,relcashoutTest.csv"
and my csv file like this for relationship files
:TYPE,:START_ID(CUSTOMER),:END_ID(AGENT),TXNID:string,TIMESTAMP:datetime,AMOUNT:int,CHANNEL
Here TYPE indicates the column named RELATIONSHIP
and my relational csv file look like this
CASHOUT,abc,xyz,6C19MX7DXL,2019-03-01T11:02:55,40,charge
CASHOUT,pqr,jkl,6C19MX7E2V,2019-03-01T11:02:57,10,charge
after running my import.sh script I am getting bellow error
unexpected error: Group 'CUSTOMER' not found. Available groups are: []
I have gone through the document but didn't figure it out my mistakes. Any help will be appreciated neo4j version is 3.5.8
The :START_ID and :END_ID fields can take an optional ID space, as in :START_ID(CUSTOMER).
But an ID space is not the same thing as a node label. In order for :START_ID(CUSTOMER) to work, one of your node CSV files (presumably the one for the CUSTOMER label) must specify, in its header, :ID(CUSTOMER) instead of just :ID. Doing so would associate the CUSTOMER ID space with the nodes created by that file, and you should no longer see that specific error.
You may also need to do something similar for the AGENT ID space.
NOTE: If all your nodes have unique values in the :ID field (across CSV files), then you do not need to use ID spaces at all. In that case, your relationship file header can simply use :START_ID and :END_ID without any qualification.

How to create relationships with load CSV from a batch-import file relationships

I have already created the nodes before and I would like to use the relationships file used some time ago during a batch- import, to create relationships using the load CSV method.
This is my relationships CSV file:
You'll need to use LOAD CSV for this (USING PERIODIC COMMIT), although you'll need to watch out for spaces in both the headers (if you use them) and your fields. trim() may help in your fields.
The headers shouldn't have : in them if at all possible.
The biggest obstacle will be dynamically using the type of the relationship from the csv. Currently Cypher does not deal with relationship types dynamically, you'll need an alternate approach. Install APOC Procedures and use apoc.create.relationship() to handle that.

Multiple Nodes per Line in Neo4j Batch Import Tool

Using Neo4j's Batch Import Tool, how can I create multiple nodes from a single row, and then attribute some properties to Node 1 and some to Node 2?
This is an example from 29.3:
movieId:ID,title,year:int,:LABEL
tt0133093,"The Matrix",1999,Movie
tt0234215,"The Matrix Reloaded",2003,Movie;Sequel
tt0242653,"The Matrix Revolutions",2003,Movie;Sequel
Is there a way to make it so title is "movieId.title" and year is its own ID? Then I can abstract that out to multiple nodes.
The import tool (in contrast to LOAD CSV) expects exactly one node per line. So you have to use some preprocessing to make the format fitting your desired graph model.
Typical candidates for this a csvkit or the usual suspects from a unix command line: sed, awk, ...
In your case I'd strip out the title into a separate file for creating the :Title nodes, and create another csv file for the relationships between movies and titles.
You can re-use the same csv file but use two different header files, with different columns used as :ID and columns you don't want for this node as :IGNORED
As the header is independent from the data you can use that approach to pull in the same file several times for different nodes, relationships, etc.
It's also explained here: http://neo4j.com/developer/guide-import-csv/#_super_fast_batch_importer_for_huge_datasets

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.

Importing data from oracle to neo4j using java API

Can u please share any links/sample source code for generating the graph using neo4j from Oracle database tables data .
And my use case is oracle schema table names as Nodes and columns are properties. And also need to genetate graph in tree structure.
Make sure you commit the transaction after creating the nodes with tx.success(), tx.finish().
If you still don't see the nodes, please post your code and/or any exceptions.
Use JDBC to extract your oracle db data. Then use the Java API to build the corresponding nodes :
GraphDatabaseService db;
try(Transaction tx = db.beginTx()){
Node datanode = db.createNode(Labels.TABLENAME);
datanode.setProperty("column name", "column value"); //do this for each column.
tx.success();
}
Also remember to scale your transactions. I tend to use around 1500 creates per transaction and it works fine for me, but you might have to play with it a little bit.
Just do a SELECT * FROM table LIMIT 1000 OFFSET X*1000 with X being the value for how many times you've run the query before. Then keep those 1000 records stored somewhere in a collection or something so you can build your nodes with them. Repeat this until you've handled every record in your database.
Not sure what you mean with "And also need to genetate graph in tree structure.", if you mean you'd like to convert foreign keys into relationships, remember to just index the key and in stead of adding the FK as a property, create a relationship to the original node in stead. You can find it by doing an index lookup. Or you could just create your own little in-memory index with a HashMap. But since you're already storing 1000 sql records in-memory, plus you are building the transaction... you need to be a bit careful with your memory depending on your JVM settings.
You need to code this ETL process yourself. Follow the below
Write your first Neo4j example by following this article.
Understand how to model with graphs.
There are multiple ways of talking to Neo4j using Java. Choose the one that suits your needs.

Resources