如何在Neo4j导入时将整数转为字符串?Cypher报错解决
解决Cypher导入CSV时整数转字符串的问题
核心错误原因
- 语法错误:第一条语句的
CREATE子句中,属性映射缺少开头的{,正确格式应为(:Address {...})。 - 映射键类型错误:使用
LOAD CSV WITH HEADERS时,line是按CSV表头字符串作为键的映射(map),无法通过数字索引line[0]访问列值——这正是报错“map key must be given as String, but was Integer”的根源。
正确解决方案
步骤1:确认CSV表头
假设你的CSV表头为:node_id,address,name,countries,countries_codes,sourceID,valid_until,note,需通过line.列名的方式访问对应列值。
步骤2:正确的Cypher语句
LOAD CSV WITH HEADERS FROM 'file:///nodes-addresses.csv' AS line CREATE (:Address { node_id: toString(toInteger(line.node_id)), address: line.address, name: line.name, countries: line.countries, countries_codes: line.countries_codes, sourceID: line.sourceID, valid_until: line.valid_until, note: line.note })
说明
toInteger(line.node_id):先将CSV中字符串形式的数字转为整数(确保数值类型正确);toString(...):再将整数转为字符串类型存入Neo4j;- 若CSV中
node_id本身就是字符串格式(如带前缀的ID),可直接使用line.node_id,无需额外转换。
内容的提问来源于stack exchange,提问作者ZM.TSS
相关产品推荐
相关产品推荐

