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

Gremlin控制台加载CSV报错‘Mapping for code not found’及相关疑问

问题解决方案

1. CSV加载报错修复

报错里的´╗┐code说明你的CSV文件是UTF-8带BOM格式,导致Commons CSV解析表头时把BOM字符当成了表头的一部分,所以找不到code这个键。

你可以通过两种方式解决:

  • 方式一:修改代码处理BOM
    替换文件读取逻辑,用InputStreamReader指定UTF-8编码自动处理BOM:
:install org.apache.commons commons-csv 1.5
import org.apache.commons.csv.CSVFormat
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets

g = TinkerGraph.open().traversal()
// 指定UTF-8编码读取,自动跳过BOM
fileReader = new InputStreamReader(new FileInputStream('C:/airports.csv'), StandardCharsets.UTF_8)
records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(fileReader)
records.each{
        code=it.get('code');
        desc=it.get('desc');
        g.V().has('code', code).fold().coalesce(
            unfold(),
            addV('airport').property('code', code).property('desc',desc)
        ).iterate()
}
g.V().count()
  • 方式二:修改CSV文件格式
    用文本编辑器(如Notepad++)将CSV转换为UTF-8无BOM格式,再用原代码读取。

2. Python读取远程图数据

可以的。只要将TinkerGraph部署到Gremlin Server,或在控制台配置远程连接指向Gremlin Server,Python就能通过gremlinpython库读取图数据:

  1. 确保Gremlin Server正常运行,且配置文件包含你的TinkerGraph实例(或把控制台中的图数据持久化到服务器)
  2. Python安装依赖:pip install gremlinpython
  3. 编写连接代码示例:
from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

# 连接本地Gremlin Server(默认端口8182)
g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))

# 读取机场数据示例
airport_list = g.V().hasLabel('airport').values('code', 'desc').toList()
print(airport_list)

内容的提问来源于stack exchange,提问作者Aureon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 05:37:10