使用Neo4j导入CSV文件时遭遇Neo.ClientError.Statement.TypeError错误求助
Got it, let's break down exactly what's causing this error and how to fix it quickly.
What's the Root Cause?
The error Neo.ClientError.Statement.TypeError: Expected Long(0) to be a org.neo4j.values.storable.TextValue happens because you're mixing up two modes of LOAD CSV:
- When you use
LOAD CSV WITH HEADERS, therowvariable is a key-value object where keys are your CSV's header names (strings), not numeric indexes like0,1, etc. - When you try to access
row[0], Neo4j interprets the number0as a key it needs to look up in the row object. Since it expects keys to be text (strings) but you're passing a number (Long), it throws the type mismatch error.
Step-by-Step Fixes
1. First, Check Your CSV's Headers
Open up datafile.csv and look at the first row (the header row). For example, if your CSV looks like this:
user_id,team_id,session_id,timestamp 1,100,5001,2024-06-01T09:15:00 2,100,5002,2024-06-01T09:20:00
Your header names are user_id, team_id, session_id, and timestamp.
2. Update Your Cypher Query to Use Header Names
Replace the numeric indexes (row[0], row[1], etc.) with the actual header names from your CSV. Here's the corrected query using the example headers above:
LOAD CSV WITH HEADERS FROM "file:///datafile.csv" AS row MERGE (u:User {id: toInt(row.user_id)}) MERGE (t:Team {id: toInt(row.team_id)}) MERGE (c:TeamChatSession {id: toInt(row.session_id)}) MERGE (u)-[:CreatesSession{timeStamp: row.timestamp}]->(c) MERGE (c)-[:OwnedBy{timeStamp: row.timestamp}]->(t)
If your headers have spaces or special characters, use the bracket notation instead: row["User ID"].
3. Alternative: If Your CSV Has No Headers
If your CSV doesn't have a header row at all, remove the WITH HEADERS clause from your query. This lets you use numeric indexes like row[0] without type issues:
LOAD CSV FROM "file:///datafile.csv" AS row MERGE (u:User {id: toInt(row[0])}) MERGE (t:Team {id: toInt(row[1])}) MERGE (c:TeamChatSession {id: toInt(row[2])}) MERGE (u)-[:CreatesSession{timeStamp: row[3]}]->(c) MERGE (c)-[:OwnedBy{timeStamp: row[3]}]->(t)
Quick Validation Tip
Before running the full import, test with a small subset of your data using LIMIT to make sure the field access works:
LOAD CSV WITH HEADERS FROM "file:///datafile.csv" AS row LIMIT 5 RETURN row.user_id, row.team_id, row.session_id, row.timestamp
内容的提问来源于stack exchange,提问作者De Gninou

