Search code examples
csvneo4jloadlabelcypher

Labels on Nodes and Relationships from a CSV file


I have problem when i want to add a label on a Node or to a Relatioship.

I do this in Neo4j with Cypher:

LOAD CSV WITH HEADERS FROM "file:c:/Users/Test/test.csv" AS line
CREATE (n:line.FROM)

and i get this error:

    Invalid input '.': expected an identifier character, whitespace, NodeLabel, a property map, ')' or a relationship pattern (line 2, column 15 (offset: 99))
"CREATE (n:line.FROM)"

If there is not a possible way of doing this with the Cypher Language, can you recommend me an other way to do my job? It is very important to find a solution on this problem even with a Cypher solution or any Java thing to do this job...


Solution

  • Thank you all for your answers but none of them helped me to solve my problem.

    I found a solution to do exactly what i wanted. The solution was the Neo4jImporter tool (Link from official manual: Neo4jImporter tool Manual ) and not Cypher language nor Java.

    So here is an example of what i have done and worked for me

    A test.csv file contains the "PropertyTest" and ":LABEL". Firstly it creates one node with the label "TEST" and after the creation it adds the "proptest" property on the "TEST" node. So to add a Label on your node you use :LABEL and to add a Property on the same node you add any name you want as a header in .csv file.

    Example of test.csv file:

    PropertyTest,:LABEL
    proptest,TEST
    

    For windows i've done the Neo4jImport.bat command as it is described in the manual page of Neo4j.You can found the Neo4jImport.bat in Windows at "C:\Program Files\Neo4j Community\bin" and you run it from command line (cmd).

    In details i opened the cmd, i followed the path to Neo4jImport.bat and finaly i wrote:

    Neo4jImport.bat --into path-to-save-your-neo4j-database --nodes path-to-your-csv\test.csv 
    --delimiter ","
    

    The default delimiter of Neo4jImporter is the "," but you can change it. For example if your information in .csv file is seperated with tab you can do the following:

    Neo4jImport.bat --into path-to-save-your-neo4j-database --nodes path-to-your-csv\test.csv 
    --delimiter "TAB"
    

    That was the way that i loaded dynamically a whole model of almost 2.000 nodes with different Labels and Properties.

    Keep in mind from the manual that you can add as many labels and as many properties you want on a node by adding to your csv more headers

    Example of two Labels in a node:

    PropertyTest,:LABEL,:LABEL
    proptest,TEST,SECOND_LABEL
    

    Example of Neo4jImport.bat for two Labels and comma seperated CSV file:

    Neo4jImport.bat --into path-to-save-your-neo4j-database --nodes path-to-your-csv\test.csv 
    --delimiter ","
    

    I hope that you will find it useful to this certain problem of Labels from .csv files and please read the official manual, it helped me a lot to find a solution for my problem.