You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Neo4j导入时将整数转为字符串?Cypher报错解决

解决Cypher导入CSV时整数转字符串的问题

核心错误原因

  1. 语法错误:第一条语句的CREATE子句中,属性映射缺少开头的{,正确格式应为(:Address {...})。
  2. 映射键类型错误:使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 14:25:11