Search code examples
neo4jcypherload-csv

Neo4J ClientError.Statement.SyntaxError


I am getting Neo.ClientError.Statement.SyntaxError while loading data from CSV file.

Neo.ClientError.Statement.SyntaxError: Invalid input 'h': expected 'i/I' (line 5, column 3 (offset: 189)) "Merge (Zip_Code:Zip_Code {code: row.zip_cd,type:'location'})"

Here is my Query:

 Using Periodic Commit
LOAD CSV WITH HEADERS FROM "file:///DOL_data_whd_whisard_reduced.csv" AS row
Merge (State_Code:State_Code {code: row.st_cd})
    where not row.st_cd is null

Merge (Zip_Code:Zip_Code {code: row.zip_cd,type:'location'})
    where not row.zip_cd is null
Merge (Zip_Code)-[:located_in]->(State_Code)

There are some blank records in the csv and hence I have used not null but this is giving me error as:

Can anyone help me out of it?


Solution

  • You are getting an error because you are using WHERE with MERGE clause. WHERE can not be used with MERGE.

    You can modify your query to remove the syntax error as follows:

    USING PERIODIC COMMIT
    LOAD CSV WITH HEADERS FROM "file:///DOL_data_whd_whisard_reduced.csv" AS row
    WITH row
    WHERE NOT row.st_cd IS NULL AND NOT row.zip_cd IS NULL
    MERGE (state_code:State_Code {code: row.st_cd})
    MERGE (zip_code:Zip_Code {code: row.zip_cd, type:'location'})
    MERGE (zip_code)-[:located_in]->(state_code)
    

    NOTE:

    1. This will skip the record if one of st_cd or zip_cd is NULL.
    2. It's not recommended to use more than one MERGE in a single query, consider writing 3 separate queries for this.

    Recommended method:

    Load State codes:

    USING PERIODIC COMMIT
    LOAD CSV WITH HEADERS FROM "file:///DOL_data_whd_whisard_reduced.csv" AS row
    WITH row
    WHERE NOT row.st_cd IS NULL 
    MERGE (state_code:State_Code {code: row.st_cd})
    

    Load Zip codes:

    USING PERIODIC COMMIT
    LOAD CSV WITH HEADERS FROM "file:///DOL_data_whd_whisard_reduced.csv" AS row
    WITH row
    WHERE NOT row.zip_cd IS NULL
    MERGE (zip_code:Zip_Code {code: row.zip_cd, type:'location'})
    

    Create State-Zip relationships:

    USING PERIODIC COMMIT
    LOAD CSV WITH HEADERS FROM "file:///DOL_data_whd_whisard_reduced.csv" AS row
    WITH row
    WHERE NOT row.st_cd IS NULL AND NOT row.zip_cd IS NULL
    MATCH (state_code:State_Code {code: row.st_cd})
    MATCH (zip_code:Zip_Code {code: row.zip_cd, type:'location'})
    MERGE (zip_code)-[:located_in]->(state_code)