Search code examples
scalacsvneo4jgraph-databases

How to use periodic commit from scala to load csv file to neo4j?


I am trying to create a graph by loading a CSV file to neo4j from scala code.

I'm using a periodic commit which is resulting in the following error:

Exception in thread "main" org.neo4j.driver.v1.exceptions.ClientException: 
Executing queries that use periodic commit in an open transaction is not possible.

Any help on why I get this error and how to avoid it? I am using periodic commit as I would be loading over 100k nodes. Is there any alternative to periodic commit?

Code:

def createGraph(createStmt: ArrayBuffer[String]): Unit = {
val session = driver.session()
val greeting: String = session.writeTransaction(new TransactionWork[String]() {
  override def execute(transaction: Transaction): String = {
    var result1: StatementResult = transaction.run("USING PERIODIC COMMIT\n"+
                                                  "LOAD CSV WITH HEADERS FROM \"file:///path/to/file.csv\" AS csvLine\n"+
                                                  "CREATE (p:Person {id: toInt(csvLine.id), name: csvLine.name})")

    "Graph Created"
  }
})

}


Solution

  • The driver docs on transactions suggest state that "Auto-commit transactions are the only way to execute USING PERIODIC COMMIT Cypher statements."

    Auto-commits are executed as session.run(...), without starting a new transaction with session.writeTransaction(...).