cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

session.run(LOAD CSV) not working

When using LOAD CSV function with session.run() to execute cypher statements into Neo4J it does'nt return anything. Have tried removing LOAD CSV and it works perfectly fine in creating nodes.

This is the code:

import fs from "fs";
import path from "path";
const {createWriteStream} = require("fs");
const {GraphQLUpload} = require("apollo-server");
import {session} from "./index.js"

/*
 * Check for GRAPHQL_SCHEMA environment variable to specify schema file
 * fallback to schema.graphql if GRAPHQL_SCHEMA environment variable is not set
 */
const files = [];
export const typeDefs = fs
  .readFileSync(
    process.env.GRAPHQL_SCHEMA || path.join(__dirname, "schema.graphql")
  )
  .toString("utf-8");

export const resolvers = {
//  Upload: GraphQLUpload,
  Query: {
    files: () => files
  },
  Mutation: {
    uploadFile: async (_, {file}) => {
      const {createReadStream, filename } = await file;

      await new Promise(res =>
        createReadStream()
          .pipe(createWriteStream(path.join(__dirname, "./uploads", filename)))
          .on("close", res)
        );
        session.run("LOAD CSV WITH HEADERS FROM 'file:///Connections.csv' AS csvLine CREATE(n)"); // This does not work with the LOAD CSV (Works with only CREATE(n))
        return true;
    }
  }
};
1 ACCEPTED SOLUTION

In your example number of column headers and there row count value mismatch.

However in case you have following data set with email as unique property
First Name,Last Name,Email Address,Company,Position,Connected On
Kasper,Knudsen,kasper@gmail.com,ABBA The Museum,Customer Service,12 May 2020

then you can code as
"LOAD CSV WITH HEADERS FROM 'file:///Connections.csv' AS csvLine CREATE(p:Person{emailid:csvLine.Email Address})
Set p.firstName=csvLine.First Name,
p.lastName=csvLine.Last Name,
p.company=csvLine.Company,
p.position=csvLine.Posttion,
p.connectedOn=csvLine.Connected On"

Make sure every column header /property should be prefixed and suffixed by back tick if they have space in between. Also right now date is loaded as string to store it as date you need to type cast

View solution in original post

5 REPLIES 5

intouch_vivek
Graph Steward

Hi @kasperknudz,

Cypher script embedded in your code is not proper
"LOAD CSV WITH HEADERS FROM 'file:///Connections.csv' AS csvLine CREATE(n)"

In the script you are saying to load csv file name Connections.csv alias csvLine and then you are not using csvline into code and merely creating blank node .

Basically csvLine is the second row of the csv file (first row is Headers). For an example if your file contains data like below
ID,Name
123,Vivek
234,Kasper

Where Id is considered as unique and you are trying to Create Nodes with Label as Person then try below

"LOAD CSV WITH HEADERS FROM 'file:///Connections.csv' AS csvLine CREATE(p:Person{id:csvLine.ID}) Set p.name=csvLine.Name"

Thanks a lot for the response! That will be very good to know for later on.

Though the problem right now is that with this code we don't even get a blank node in Neo4j desktop.. It only creates a blank node when we remove the CSV IMPORT.

so basically "CREATE(n)" gives us an empty node, but with CSV IMPORT we get nothing.

Also here is an example of how the CSV is built:

First Name,Last Name,Email Address,Company,Position,Connected On
Kasper,Knudsen,ABBA The Museum,Customer Service,12 May 2020

In your example number of column headers and there row count value mismatch.

However in case you have following data set with email as unique property
First Name,Last Name,Email Address,Company,Position,Connected On
Kasper,Knudsen,kasper@gmail.com,ABBA The Museum,Customer Service,12 May 2020

then you can code as
"LOAD CSV WITH HEADERS FROM 'file:///Connections.csv' AS csvLine CREATE(p:Person{emailid:csvLine.Email Address})
Set p.firstName=csvLine.First Name,
p.lastName=csvLine.Last Name,
p.company=csvLine.Company,
p.position=csvLine.Posttion,
p.connectedOn=csvLine.Connected On"

Make sure every column header /property should be prefixed and suffixed by back tick if they have space in between. Also right now date is loaded as string to store it as date you need to type cast

Thanks a lot, will do this!