跳转到主要内容

HugeGraph Python 客户端快速上手

hugegraph-python-client 是 Apache HugeGraph 的 Python SDK。它支持管理 Schema、读写图数据、执行 Gremlin 查询、进行 OLTP 实时图遍历以及 提交异步 OLAP 全图计算任务。HugeGraph-LLM 和 HugeGraph-ML 也依赖该客户端进行图数据库交互。

客户端源码位于 hugegraph-ai 仓库的 hugegraph-python-client/ 目录下,导入包名为 pyhugegraph。

环境要求

  • Python >= 3.12(支持 Python 3.12–3.14)。
  • HugeGraph Server 1.5.0 或更高版本(Server 1.7+ 推荐开启 GraphSpace 模式;1.5/1.6 使用传统路由)。
  • pip 或 uv 包管理器。

运行时依赖包括 decorator、requests、urllib3 和 rich。

安装

客户端提供两种安装方式,具体取决于使用的是 PyPI 稳定发行版还是 master 分支最新的开发功能。

说明

版本与功能边界说明: 已发布的 PyPI 发行包(hugegraph-python 1.7.x)为稳定基础版本,以 CamelCase 命名风格为主(带旧版 snake_case 别名)。 本文档演示的现代特性——包括原生 snake_case API、client.olap() 全图计算算法、client.task().wait() 任务轮询等待、 client.close() 连接生命周期管理以及更新后的遍历器签名——均对齐 master 分支最新实现。 如需体验或测试这些新特性,请通过源码方式安装客户端。

PyPI 稳定版 (1.7.x)
1pip install hugegraph-python
2# 或使用 uv:
3# uv pip install hugegraph-python
源码安装 (Master)
1git clone https://github.com/apache/hugegraph-ai.git
2cd hugegraph-ai/hugegraph-python-client
3pip install -e .
Workspace 同步 (uv)
1git clone https://github.com/apache/hugegraph-ai.git
2cd hugegraph-ai
3uv sync --extra python-client
4source .venv/bin/activate

连接与数据写入

 1from pyhugegraph.client import PyHugeClient
 2
 3client = PyHugeClient(
 4    url="http://127.0.0.1:8080",
 5    graph="hugegraph",
 6    user="admin",
 7    pwd="admin",
 8    graphspace=None,
 9)
10
11schema = client.schema()
12schema.property_key("name").as_text().if_not_exist().create()
13schema.property_key("birthDate").as_text().if_not_exist().create()
14schema.vertex_label("Person").properties("name", "birthDate") \
15      .use_primary_key_id().primary_keys("name").if_not_exist().create()
16schema.vertex_label("Movie").properties("name") \
17      .use_primary_key_id().primary_keys("name").if_not_exist().create()
18schema.edge_label("ActedIn").source_label("Person").target_label("Movie") \
19      .if_not_exist().create()
20
21graph = client.graph()
22person = graph.add_vertex(
23    "Person", {"name": "Al Pacino", "birthDate": "1940-04-25"}
24)
25movie = graph.add_vertex("Movie", {"name": "The Godfather"})
26edge = graph.add_edge("ActedIn", person.id, movie.id, {})
27
28print(graph.get_vertex_by_id(person.id))
29print(graph.get_edge_by_id(edge.id))
30
31client.close()

客户端初始化参数

PyHugeClient(url, graph, user, pwd, graphspace=None, timeout=None, *, token=None)

参数类型默认值说明
urlstr必填HugeGraph Server 基础地址。若未携带协议前缀,会自动补齐 http://(例如 127.0.0.1:8080)。
graphstr必填目标图实例名称,为第二个位置参数。
userstr必填用户名,用于 HTTP Basic Auth 认证。
pwdstr必填密码,用于 HTTP Basic Auth 认证。
graphspacestr 或 NoneNoneGraphSpace 名称。为 None 时通过版本探测自动决定路由。
timeoutfloat、tuple[float, float] 或 NoneNone(connect, read) 超时秒数,或统一指定单个浮点数。默认值为 (0.5, 15.0)。
tokenstr 或 NoneNone显式指定 Bearer Token;提供时优先覆盖 Basic Auth 认证。

对于幂等请求,在收到 500、502、504 错误响应时最多重试 3 次(退避系数为 0.1)。异步任务轮询等待期间会自动管理截止时间预算,不额外触发重定向或 HTTP 重试。

服务端版本与 GraphSpace 路由

客户端在初始化阶段自动解析 GraphSpace 路由:

  • 显式传入非空 graphspace 时,直接启用对应的 GraphSpace 路由模式。
  • graphspace=None 时,客户端向 GET {url}/versions 发起探测以获取服务端内核版本。
  • HugeGraph Server 1.7+ 默认启用 GraphSpace 并分配至 DEFAULT。
  • HugeGraph Server 1.5 / 1.6 保持传统无 GraphSpace 路由。
  • 低于 1.5.0 的版本不支持,初始化时会抛出 RuntimeError。
  • 若版本探测失败且未显式指定 GraphSpace,客户端初始化将直接抛出异常,避免错误猜测服务端路由结构。

路由模式决定了服务端请求路径前缀:开启 GraphSpace 时为 /graphspaces/<graphspace>/graphs/<graph>/...,关闭时为 /graphs/<graph>/...。

客户端入口概览

PyHugeClient 实例维护全局共享的 HTTP Session。所有管理器均采用延迟加载并缓存复用:

访问入口管理器类功能范围
client.schema()SchemaManager属性类型、顶点类型、边类型、索引类型管理
client.graph()GraphManager顶点与边增删改查、批量写入、条件分页查询
client.gremlin()GremlinManager执行 Gremlin 查询语句
client.traverser()TraverserManager实时图遍历与路径查询(OLTP)
client.rank()RankManager实时个性化推荐排序(OLTP)
client.olap()OlapManager异步全图计算算法作业(OLAP)
client.task()TaskManager异步任务状态查询、结果轮询等待、任务取消与删除
client.graphs()GraphsManager图元数据查看、配置导出、图列表、清空全图数据
client.graphspaces()GraphspaceManager多图空间(GraphSpace)生命周期管理
client.schema_templates()SchemaTemplateManager可复用 Schema 模板管理
client.auth()AuthManager用户、用户组、目标资源、所属关系与权限管理
client.variable()VariableManager图级别持久化键值变量
client.metrics()MetricsManager服务端监控指标数据采集
client.version()VersionManager服务端及各组件版本信息
client.rest()RestClient底层 REST API 直接调用及扩展端点

常用操作

定义 Schema

Schema 构造器采用链式调用。调用 create() 执行创建,或使用 append()、eliminate()、remove() 修改已有定义:

 1schema = client.schema()
 2
 3# 属性类型:as_text / as_int / as_long / as_float / as_double / as_bool / as_date
 4# 基数模式:value_single / value_list / value_set
 5schema.property_key("age").as_int().value_single().if_not_exist().create()
 6
 7# 顶点类型:use_automatic_id / use_customize_string_id / use_primary_key_id
 8schema.vertex_label("person").properties("name", "age", "city") \
 9      .primary_keys("name").nullable_keys("city").if_not_exist().create()
10
11# 边类型:link() 快捷指定起点与终点顶点类型
12schema.edge_label("knows").link("person", "person").multi_times() \
13      .properties("date", "city").sort_keys("date").nullable_keys("city") \
14      .if_not_exist().create()
15
16# 索引类型:on_v / on_e,支持 secondary / range / search / shard / unique
17schema.index_label("personByCity").on_v("person").by("city") \
18      .secondary().if_not_exist().create()

查询 Schema

 1schema = client.schema()
 2print(schema.get_schema())            # 获取全图 Schema 结构字典
 3print(schema.get_property_keys())
 4print(schema.get_vertex_labels())
 5print(schema.get_edge_labels())
 6print(schema.get_index_labels())
 7
 8# 查询单个 Schema 定义
 9print(schema.get_property_key("name"))
10print(schema.get_vertex_label("person"))
11print(schema.get_edge_label("knows"))
12print(schema.get_index_label("personByCity"))
13
14# 边关联关系概览,格式形如 "Person--ActedIn-->Movie"
15print(schema.get_relations())

增删改查图数据

图数据 API 直接接收 Python 属性字典:

1graph = client.graph()
2graph.append_vertex(person.id, {"birthDate": "1940-04-25"})    # 追加属性
3graph.eliminate_vertex(person.id, {"birthDate": "1940-04-25"}) # 移除属性
4graph.append_edge(edge.id, {"city": "Beijing"})
5graph.eliminate_edge(edge.id, {"city": "Beijing"})
6graph.remove_edge_by_id(edge.id)
7graph.remove_vertex_by_id(person.id)

add_vertex 返回包含 id、label、type 与 properties 的 VertexData 实例。 add_edge 返回包含 id、label、type、outV、outVLabel、inV、inVLabel 与 properties 的 EdgeData 实例。

传入客户端的顶点 ID 可以是字符串、整数或 uuid.UUID 对象。布尔类型会被拦截拒绝,整数必须位于 Java 64 位有符号整型范围内。

批量导入数据

add_vertices 接收 (label, properties) 元组列表,add_edges 接收 (label, out_id, in_id, out_label, in_label, properties) 元组列表:

1graph = client.graph()
2vertices = graph.add_vertices([
3    ("person", {"name": "Alice", "age": 20}),
4    ("person", {"name": "Bob", "age": 23}),
5])
6edges = graph.add_edges([
7    ("knows", vertices[0].id, vertices[1].id, "person", "person", {"date": "2012-01-10"}),
8])

分页与条件查询

 1graph = client.graph()
 2
 3# 分页迭代查询:传入上一页返回的 next_page token 即可继续拉取下一页
 4vertices, next_page = graph.get_vertex_by_page("person", limit=10)
 5vertices, next_page = graph.get_vertex_by_page("person", limit=10, page=next_page)
 6
 7# 服务端属性条件过滤
 8older = graph.get_vertex_by_condition("person", properties={"age": "P.gt(29)"})
 9
10# 边分页查询(指定 vertex_id 时必须同时指定 direction 方向)
11edges, next_page = graph.get_edge_by_page(label="knows", limit=10)
12edges, next_page = graph.get_edge_by_page(vertex_id=person.id, direction="OUT", limit=10)
13
14# 根据 ID 列表批量查询顶点与边
15graph.get_vertices_by_id([v1.id, v2.id])
16graph.get_edges_by_id([e1.id, e2.id])

执行 Gremlin 查询

1gremlin = client.gremlin()
2result = gremlin.exec("g.V().limit(5)")
3print(result)

exec() 会根据图实例名称及解析出的 GraphSpace 自动绑定 graph 与 g 别名,并解析返回服务端响应中的 result 数据内容。

实时图遍历(OLTP)

client.traverser() 封装了服务端实时图遍历接口,支持多跳邻居、最短路径、共同邻居与相似度计算:

 1traverser = client.traverser()
 2
 3traverser.k_out(marko_id, 2)
 4traverser.k_neighbor(marko_id, 2)
 5traverser.same_neighbors(marko_id, josh_id)
 6traverser.jaccard_similarity(marko_id, josh_id)
 7traverser.shortest_path(marko_id, ripple_id, 3)
 8traverser.all_shortest_paths(marko_id, ripple_id, 3)
 9traverser.weighted_shortest_path(marko_id, ripple_id, "weight", 3)
10traverser.single_source_shortest_path(marko_id, 2)
11traverser.multi_node_shortest_path([marko_id, josh_id], max_depth=2)
12traverser.paths(marko_id, josh_id, 2)
13traverser.crosspoints(marko_id, josh_id, 2)
14traverser.rings(marko_id, 3)
15traverser.rays(marko_id, 2)
16traverser.vertices(marko_id)
17traverser.edges(edge_id)

POST 方式的遍历接口支持复杂请求体:advanced_paths、customized_paths、template_paths、customized_crosspoints 以及 fusiform_similarity。

OLAP 全图计算任务

对于全图级别的重型计算算法(如 PageRank、Louvain 社区发现、中心度计算等),请使用 client.olap()。这些作业在服务端以异步任务方式运行并通过 Task ID 进行跟踪管理:

 1# 1. 提交异步计算任务
 2receipt = client.olap().count_vertex()
 3task_id = receipt["task_id"]
 4
 5# 2. 轮询等待任务完成并获取计算结果
 6task = client.task().wait(task_id, timeout=300, poll_interval=1)
 7result = client.task().result(task)
 8print(result)
 9
10# 也可以直接使用 run() 一步完成提交、等待与结果解析:
11result = client.olap().run("count_vertex", timeout=60)

支持的 OLAP 算法

服务端提供了 17 个唯一的全图计算算法名称:

算法类别具体算法名称
图统计count_edge、count_vertex、subgraph_stat、triangle_count、rings
中心度betweenness_centrality、closeness_centrality、degree_centrality、eigenvector_centrality、stress_centrality
社区与聚类cluster_coefficient、k_core、louvain、lpa、weak_connected_component
排序与相似度page_rank、fusiform_similarity
提示

类似 page_rank 的算法会将计算出的权重属性回写到图中。在要求显式 Schema 声明的环境中,执行 PageRank 前需在参与计算的顶点类型上提前声明名为 r_rank 的可空 DOUBLE 属性。

任务生命周期管理

TaskManager 提供了对异步任务的完整管控能力:

 1task = client.task()
 2
 3# 查询任务历史列表
 4task.list_tasks(status="success", limit=10)
 5
 6# 轮询等待指定任务完成
 7record = task.wait(task_id, timeout=300, poll_interval=1)
 8
 9# 解码任务执行结果(只对序列化 JSON 解析一次)
10result = task.result(record)
11
12# 取消或删除任务
13task.cancel_task(task_id)
14task.delete_task(task_id, force=False)
  • wait(id, timeout=300, poll_interval=1) 轮询期间仅查询任务状态而不拉取大体积结果,完成后再统一获取最终产出。
  • TaskTimeoutError 仅代表客户端轮询等待超时,服务端后台运行的任务作业不会被终止。
  • 如需强行终止服务端任务,请显式调用 cancel_task(id)。

图变量、监控与元数据

 1# 图变量管理
 2variable = client.variable()
 3variable.set("owner", "mary")
 4print(variable.get("owner"))
 5print(variable.all())
 6variable.remove("owner")
 7
 8# 监控指标数据
 9metrics = client.metrics()
10metrics.get_all_basic_metrics()
11metrics.get_system_metrics()
12metrics.get_backend_metrics()
13
14# 图管理与版本
15graphs = client.graphs()
16print(graphs.list())
17print(client.version().version())

认证与权限控制

PyHugeClient 同时支持 HTTP Basic Auth 与 Bearer Token 认证:

1# 将会话切换为 Bearer Token 认证
2client.use_token("your-jwt-token")
3
4# 恢复为初始化配置的 HTTP Basic Auth 凭据
5client.use_token(None)

AuthManager 支持在 GraphSpace 隔离下管理用户、用户组、目标资源、所属关系以及操作权限:

 1auth = client.auth()
 2
 3user = auth.create_user("test_user", "password")
 4auth.modify_user(user["id"], user_email="hugegraph@apache.org")
 5auth.get_user(user["id"])
 6auth.list_users(limit=10)
 7
 8group = auth.create_group("test_group", "read only")
 9auth.modify_group(group["id"], group_description="updated")
10auth.list_groups()
11
12target = auth.create_target("target1", "hugegraph", "127.0.0.1:8080", [])
13auth.update_target(target["id"], "target1", "hugegraph", "127.0.0.1:8080", [])
14auth.list_targets()
15
16belong = auth.create_belong(user["id"], group["id"])
17auth.update_belong(belong["id"], "description")
18auth.list_belongs()
19
20access = auth.grant_accesses(group["id"], target["id"], "READ")
21auth.modify_accesses(access["id"], "description")
22auth.list_accesses()
23
24# 按照依赖反向顺序清理资源
25auth.revoke_accesses(access["id"])
26auth.delete_belong(belong["id"])
27auth.delete_target(target["id"])
28auth.delete_group(group["id"])
29auth.delete_user(user["id"])

方法命名规范与迁移说明

  • 推荐规范:新代码请统一使用 snake_case 方法名(如 add_vertex、property_key 等)。
  • 兼容保证:现有的公开 CamelCase 方法(如 addVertex、propertyKey 等)作为直接别名持续得到兼容支持。
  • 客户端生命周期:结束调用后请统一通过 client.close() 释放连接池会话资源,无需单独关闭单个 manager。
  • 返回契约:
    • 传统快捷接口 get_all_graphs()、get_version() 和 get_graph_info() 保持返回 JSON 字符串以维护向前兼容。
    • 标准现代接口 client.graphs().list()、client.version().version() 和 client.graphs().get(graph_name) 直接返回已解析的 Python 字典。
    • 带权最短路径与单源最短路径方法不再支持 max_depth 参数(服务端接口不支持);请使用 capacity、max_degree 和 skip_degree 参数进行限制。

底层 REST API 直接调用

对于批量原生接口调用或高级管理器尚未封装的扩展接口,可通过 client.rest() 直接访问完整 REST 接口面:

1rest = client.rest()
2print(rest.vertex.list(label="person", offset=0, limit=20))
3print(rest.task.list(status="success", page="", limit=20))

下表列出与服务端完全对齐的 194 个底层 REST 端点:

Python REST entryMethodPathQuery parameters
rest.access.create()POST/graphspaces/{graphspace}/auth/accesses—
rest.access.delete()DELETE/graphspaces/{graphspace}/auth/accesses/{id}—
rest.access.get()GET/graphspaces/{graphspace}/auth/accesses/{id}—
rest.access.list()GET/graphspaces/{graphspace}/auth/accessesgroup, target, limit
rest.access.update()PUT/graphspaces/{graphspace}/auth/accesses/{id}—
rest.adamic_adar.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/adamicadarvertex, other, direction, label, max_degree, limit
rest.algorithm.post()POST/graphspaces/{graphspace}/graphs/{graph}/jobs/algorithm/{name}—
rest.all_shortest_paths.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/allshortestpathssource, target, direction, label, max_depth, max_degree, skip_degree, with_vertex, with_edge, capacity
rest.arthas.start_arthas()PUT/arthas—
rest.belong.create()POST/graphspaces/{graphspace}/auth/belongs—
rest.belong.delete()DELETE/graphspaces/{graphspace}/auth/belongs/{id}—
rest.belong.get()GET/graphspaces/{graphspace}/auth/belongs/{id}—
rest.belong.list()GET/graphspaces/{graphspace}/auth/belongsuser, group, limit
rest.belong.update()PUT/graphspaces/{graphspace}/auth/belongs/{id}—
rest.computer.post()POST/graphspaces/{graphspace}/graphs/{graph}/jobs/computer/{name}—
rest.count.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/count—
rest.crosspoints.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/crosspointssource, target, direction, label, max_depth, max_degree, capacity, limit
rest.customized_crosspoints.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/customizedcrosspoints—
rest.customized_paths.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/customizedpaths—
rest.cypher.post()POST/graphspaces/{graphspace}/graphs/{graph}/cypher—
rest.cypher.query()GET/graphspaces/{graphspace}/graphs/{graph}/cyphercypher
rest.edge.create()POST/graphspaces/{graphspace}/graphs/{graph}/graph/edges—
rest.edge.create_batch()POST/graphspaces/{graphspace}/graphs/{graph}/graph/edges/batchcheck_vertex
rest.edge.delete()DELETE/graphspaces/{graphspace}/graphs/{graph}/graph/edges/{id}label
rest.edge.get()GET/graphspaces/{graphspace}/graphs/{graph}/graph/edges/{id}—
rest.edge.list()GET/graphspaces/{graphspace}/graphs/{graph}/graph/edgesvertex_id, direction, label, properties, keep_start_p, offset, page, limit
rest.edge.update_batch()PUT/graphspaces/{graphspace}/graphs/{graph}/graph/edges/batch—
rest.edge.update_by_id()PUT/graphspaces/{graphspace}/graphs/{graph}/graph/edges/{id}action
rest.edge_existence.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/edgeexistsource, target, label, sort_values, limit
rest.edge_label.create()POST/graphspaces/{graphspace}/graphs/{graph}/schema/edgelabels—
rest.edge_label.delete()DELETE/graphspaces/{graphspace}/graphs/{graph}/schema/edgelabels/{name}—
rest.edge_label.get()GET/graphspaces/{graphspace}/graphs/{graph}/schema/edgelabels/{name}—
rest.edge_label.list()GET/graphspaces/{graphspace}/graphs/{graph}/schema/edgelabelsnames
rest.edge_label.update()PUT/graphspaces/{graphspace}/graphs/{graph}/schema/edgelabels/{name}action
rest.edges.list()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/edgesids
rest.edges.scan()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/edges/scanstart, end, page, page_limit
rest.edges.shards()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/edges/shardssplit_size
rest.exception_trace.get()GET/exception/trace—
rest.exception_trace.trace()PUT/exception/trace—
rest.fusiform_similarity.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/fusiformsimilarity—
rest.graph_space.check_default_role()GET/graphspaces/{graphspace}/roleuser, role, graph
rest.graph_space.create()POST/graphspaces—
rest.graph_space.delete()DELETE/graphspaces/{name}—
rest.graph_space.delete_default_role()DELETE/graphspaces/{graphspace}/roleuser, role, graph
rest.graph_space.get()GET/graphspaces/{graphspace}—
rest.graph_space.list()GET/graphspaces—
rest.graph_space.list_profile()GET/graphspaces/profileprefix
rest.graph_space.manage()PUT/graphspaces/{name}—
rest.graph_space.set_default_role()POST/graphspaces/{graphspace}/role—
rest.graph_space_group.create()POST/graphspaces/{graphspace}/auth/groups—
rest.graph_space_group.delete()DELETE/graphspaces/{graphspace}/auth/groups/{id}—
rest.graph_space_group.get()GET/graphspaces/{graphspace}/auth/groups/{id}—
rest.graph_space_group.list()GET/graphspaces/{graphspace}/auth/groupslimit
rest.graph_space_group.update()PUT/graphspaces/{graphspace}/auth/groups/{id}—
rest.graphs.clear()DELETE/graphspaces/{graphspace}/graphs/{name}/clearconfirm_message
rest.graphs.clone_schema()POST/graphspaces/{graphspace}/graphs/{name}/schema-clonesource
rest.graphs.compact()PUT/graphspaces/{graphspace}/graphs/{name}/compact—
rest.graphs.create()POST/graphspaces/{graphspace}/graphs/{name}clone_graph_name
rest.graphs.create_by_text()POST/graphspaces/{graphspace}/graphs/{name}clone_graph_name
rest.graphs.create_snapshot()PUT/graphspaces/{graphspace}/graphs/{name}/snapshot_create—
rest.graphs.drop()DELETE/graphspaces/{graphspace}/graphs/{name}confirm_message
rest.graphs.get()GET/graphspaces/{graphspace}/graphs/{name}—
rest.graphs.get_conf()GET/graphspaces/{graphspace}/graphs/{name}/conf—
rest.graphs.get_default()GET/graphspaces/{graphspace}/graphs/default—
rest.graphs.graph_read_mode_get()GET/graphspaces/{graphspace}/graphs/{name}/graph_read_mode—
rest.graphs.graph_read_mode_set()PUT/graphspaces/{graphspace}/graphs/{name}/graph_read_mode—
rest.graphs.list()GET/graphspaces/{graphspace}/graphs—
rest.graphs.list_profile()GET/graphspaces/{graphspace}/graphs/profileprefix
rest.graphs.loading_mode_get()GET/graphspaces/{graphspace}/graphs/{name}/mode/owners—
rest.graphs.loading_mode_set()PUT/graphspaces/{graphspace}/graphs/{name}/mode/owners—
rest.graphs.manage()PUT/graphspaces/{graphspace}/graphs/{name}—
rest.graphs.mode_get()GET/graphspaces/{graphspace}/graphs/{name}/mode—
rest.graphs.mode_set()PUT/graphspaces/{graphspace}/graphs/{name}/mode—
rest.graphs.reload()PUT/graphspaces/{graphspace}/graphs/manage—
rest.graphs.resume_snapshot()PUT/graphspaces/{graphspace}/graphs/{name}/snapshot_resume—
rest.graphs.set_default()POST/graphspaces/{graphspace}/graphs/{name}/default—
rest.graphs.unset_default()DELETE/graphspaces/{graphspace}/graphs/{name}/default—
rest.gremlin.get()GET/gremlingremlin, language; additional query map
rest.gremlin.post()POST/gremlin—
rest.gremlin_job.post()POST/graphspaces/{graphspace}/graphs/{graph}/jobs/gremlin—
rest.group.create()POST/auth/groups—
rest.group.delete()DELETE/auth/groups/{id}—
rest.group.get()GET/auth/groups/{id}—
rest.group.list()GET/auth/groupslimit
rest.group.update()PUT/auth/groups/{id}—
rest.index_label.create()POST/graphspaces/{graphspace}/graphs/{graph}/schema/indexlabels—
rest.index_label.delete()DELETE/graphspaces/{graphspace}/graphs/{graph}/schema/indexlabels/{name}—
rest.index_label.get()GET/graphspaces/{graphspace}/graphs/{graph}/schema/indexlabels/{name}—
rest.index_label.list()GET/graphspaces/{graphspace}/graphs/{graph}/schema/indexlabelsnames
rest.index_label.update()PUT/graphspaces/{graphspace}/graphs/{graph}/schema/indexlabels/{name}action
rest.jaccard_similarity.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/jaccardsimilarityvertex, other, direction, label, max_degree
rest.jaccard_similarity.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/jaccardsimilarity—
rest.kneighbor.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/kneighborsource, direction, label, max_depth, count_only, max_degree, limit
rest.kneighbor.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/kneighbor—
rest.kout.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/koutsource, direction, label, max_depth, nearest, count_only, max_degree, capacity, limit
rest.kout.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/kout—
rest.login.login()POST/auth/login—
rest.login.logout()DELETE/auth/logout—
rest.login.verify_token()GET/auth/verify—
rest.manager.check_default_role()GET/graphspaces/{graphspace}/auth/managers/defaultrole, graph
rest.manager.check_role()GET/graphspaces/{graphspace}/auth/managers/checktype
rest.manager.create_manager()POST/graphspaces/{graphspace}/auth/managers—
rest.manager.delete()DELETE/graphspaces/{graphspace}/auth/managersuser, type
rest.manager.get_roles_in_gs()GET/graphspaces/{graphspace}/auth/managers/roleuser
rest.manager.list()GET/graphspaces/{graphspace}/auth/managerstype
rest.metrics.all()GET/metricstype
rest.metrics.backend()GET/metrics/backend—
rest.metrics.counters()GET/metrics/counters—
rest.metrics.gauges()GET/metrics/gauges—
rest.metrics.histograms()GET/metrics/histograms—
rest.metrics.meters()GET/metrics/meters—
rest.metrics.statistics()GET/metrics/statisticstype
rest.metrics.system()GET/metrics/system—
rest.metrics.timers()GET/metrics/timers—
rest.multi_node_shortest_path.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/multinodeshortestpath—
rest.neighbor_rank.neighbor_rank()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/neighborrank—
rest.paths.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/pathssource, target, direction, label, max_depth, max_degree, capacity, limit
rest.paths.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/paths—
rest.personal_rank.personal_rank()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/personalrank—
rest.profile.get_profile()GET/—
rest.profile.show_all_apis()GET/apis—
rest.project.create()POST/graphspaces/{graphspace}/auth/projects—
rest.project.delete()DELETE/graphspaces/{graphspace}/auth/projects/{id}—
rest.project.get()GET/graphspaces/{graphspace}/auth/projects/{id}—
rest.project.list()GET/graphspaces/{graphspace}/auth/projectslimit
rest.project.update()PUT/graphspaces/{graphspace}/auth/projects/{id}action
rest.property_key.create()POST/graphspaces/{graphspace}/graphs/{graph}/schema/propertykeys—
rest.property_key.delete()DELETE/graphspaces/{graphspace}/graphs/{graph}/schema/propertykeys/{name}—
rest.property_key.get()GET/graphspaces/{graphspace}/graphs/{graph}/schema/propertykeys/{name}—
rest.property_key.list()GET/graphspaces/{graphspace}/graphs/{graph}/schema/propertykeysnames
rest.property_key.update()PUT/graphspaces/{graphspace}/graphs/{graph}/schema/propertykeys/{name}action
rest.raft.add_peer()POST/graphspaces/{graphspace}/graphs/{graph}/raft/add_peergroup, endpoint
rest.raft.get_leader()GET/graphspaces/{graphspace}/graphs/{graph}/raft/get_leadergroup
rest.raft.list_peers()GET/graphspaces/{graphspace}/graphs/{graph}/raft/list_peersgroup
rest.raft.remove_peer()POST/graphspaces/{graphspace}/graphs/{graph}/raft/remove_peergroup, endpoint
rest.raft.set_leader()POST/graphspaces/{graphspace}/graphs/{graph}/raft/set_leadergroup, endpoint
rest.raft.transfer_leader()POST/graphspaces/{graphspace}/graphs/{graph}/raft/transfer_leadergroup, endpoint
rest.rays.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/rayssource, direction, label, max_depth, max_degree, capacity, limit, with_vertex, with_edge
rest.rebuild.edge_label_rebuild()PUT/graphspaces/{graphspace}/graphs/{graph}/jobs/rebuild/edgelabels/{name}—
rest.rebuild.index_label_rebuild()PUT/graphspaces/{graphspace}/graphs/{graph}/jobs/rebuild/indexlabels/{name}—
rest.rebuild.vertex_label_rebuild()PUT/graphspaces/{graphspace}/graphs/{graph}/jobs/rebuild/vertexlabels/{name}—
rest.resource_allocation.create()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/resourceallocationvertex, other, direction, label, max_degree, limit
rest.rings.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/ringssource, direction, label, max_depth, source_in_ring, max_degree, capacity, limit, with_vertex, with_edge
rest.same_neighbors.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/sameneighborsvertex, other, direction, label, max_degree, limit
rest.same_neighbors.same_neighbors()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/sameneighbors—
rest.same_neighbors_batch.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/sameneighborsbatch—
rest.schema.list()GET/graphspaces/{graphspace}/graphs/{graph}/schema—
rest.schema_template.create()POST/graphspaces/{graphspace}/schematemplates—
rest.schema_template.delete()DELETE/graphspaces/{graphspace}/schematemplates/{name}—
rest.schema_template.get()GET/graphspaces/{graphspace}/schematemplates/{name}—
rest.schema_template.list()GET/graphspaces/{graphspace}/schematemplates—
rest.schema_template.update()PUT/graphspaces/{graphspace}/schematemplates/{name}—
rest.shortest_path.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/shortestpathsource, target, direction, label, max_depth, max_degree, skip_degree, with_vertex, with_edge, capacity
rest.single_source_shortest_path.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/singlesourceshortestpathsource, direction, label, weight, max_degree, skip_degree, with_vertex, with_edge, capacity, limit
rest.target.create()POST/graphspaces/{graphspace}/auth/targets—
rest.target.delete()DELETE/graphspaces/{graphspace}/auth/targets/{id}—
rest.target.get()GET/graphspaces/{graphspace}/auth/targets/{id}—
rest.target.list()GET/graphspaces/{graphspace}/auth/targetslimit
rest.target.update()PUT/graphspaces/{graphspace}/auth/targets/{id}—
rest.task.delete()DELETE/graphspaces/{graphspace}/graphs/{graph}/tasks/{id}force
rest.task.get()GET/graphspaces/{graphspace}/graphs/{graph}/tasks/{id}with_result
rest.task.list()GET/graphspaces/{graphspace}/graphs/{graph}/tasksstatus, ids, limit, page
rest.task.update()PUT/graphspaces/{graphspace}/graphs/{graph}/tasks/{id}action
rest.template_paths.post()POST/graphspaces/{graphspace}/graphs/{graph}/traversers/templatepaths—
rest.user.create()POST/graphspaces/{graphspace}/auth/users—
rest.user.delete()DELETE/graphspaces/{graphspace}/auth/users/{id}—
rest.user.get()GET/graphspaces/{graphspace}/auth/users/{id}—
rest.user.list()GET/graphspaces/{graphspace}/auth/usersname, limit
rest.user.role()GET/graphspaces/{graphspace}/auth/users/{id}/role—
rest.user.update()PUT/graphspaces/{graphspace}/auth/users/{id}—
rest.variables.delete()DELETE/graphspaces/{graphspace}/graphs/{graph}/variables/{key}—
rest.variables.get()GET/graphspaces/{graphspace}/graphs/{graph}/variables/{key}—
rest.variables.list()GET/graphspaces/{graphspace}/graphs/{graph}/variables—
rest.variables.update()PUT/graphspaces/{graphspace}/graphs/{graph}/variables/{key}—
rest.version.list()GET/versions—
rest.vertex.create()POST/graphspaces/{graphspace}/graphs/{graph}/graph/vertices—
rest.vertex.create_batch()POST/graphspaces/{graphspace}/graphs/{graph}/graph/vertices/batch—
rest.vertex.delete()DELETE/graphspaces/{graphspace}/graphs/{graph}/graph/vertices/{id}label
rest.vertex.get()GET/graphspaces/{graphspace}/graphs/{graph}/graph/vertices/{id}—
rest.vertex.list()GET/graphspaces/{graphspace}/graphs/{graph}/graph/verticeslabel, properties, keep_start_p, offset, page, limit
rest.vertex.update_batch()PUT/graphspaces/{graphspace}/graphs/{graph}/graph/vertices/batch—
rest.vertex.update_by_id()PUT/graphspaces/{graphspace}/graphs/{graph}/graph/vertices/{id}action
rest.vertex_label.create()POST/graphspaces/{graphspace}/graphs/{graph}/schema/vertexlabels—
rest.vertex_label.delete()DELETE/graphspaces/{graphspace}/graphs/{graph}/schema/vertexlabels/{name}—
rest.vertex_label.get()GET/graphspaces/{graphspace}/graphs/{graph}/schema/vertexlabels/{name}—
rest.vertex_label.list()GET/graphspaces/{graphspace}/graphs/{graph}/schema/vertexlabelsnames
rest.vertex_label.update()PUT/graphspaces/{graphspace}/graphs/{graph}/schema/vertexlabels/{name}action
rest.vertices.list()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/verticesids
rest.vertices.scan()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/vertices/scanstart, end, page, page_limit
rest.vertices.shards()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/vertices/shardssplit_size
rest.weighted_shortest_path.get()GET/graphspaces/{graphspace}/graphs/{graph}/traversers/weightedshortestpathsource, target, direction, label, weight, max_degree, skip_degree, with_vertex, with_edge, capacity
rest.white_ip_list.list()GET/whiteiplist—
rest.white_ip_list.update_status()PUT/whiteipliststatus
rest.white_ip_list.update_white_ips()POST/whiteiplist—

异常处理

常见异常定义位于 pyhugegraph.utils.exceptions:

异常类型触发场景
NotAuthorizedError服务端返回 HTTP 401 未认证
NotFoundError服务端返回 HTTP 404 资源未找到
ServerError服务端返回非 2xx 响应(附带服务端错误详情)
ResponseParseError响应内容无法反序列化为预期格式
ServiceUnavailableError服务端暂时不可用
InvalidParameterError, CreateError, RemoveError, UpdateError, DataFormatError客户端本地参数或格式校验失败

敏感参数(密码、Token、密钥等)在日志输出中会自动脱敏处理。

1from pyhugegraph.utils.exceptions import NotFoundError
2
3try:
4    graph.get_vertex_by_id("missing-id")
5except NotFoundError:
6    print("未找到指定顶点")

开发与测试验证

在 HugeGraph-AI 仓库根目录下执行代码规范与测试套件:

  1. 代码格式化与静态检查:

    1uv run --extra dev ruff format --check .
    2uv run --extra dev ruff check .
    3uv run --extra dev ty check hugegraph-python-client/src/pyhugegraph
  2. 运行单元测试与契约测试:

    1uv run pytest hugegraph-python-client/src/tests -m "unit or contract"
  3. 针对正在运行的 HugeGraph Server 执行集成测试:

    1HUGEGRAPH_URL=http://127.0.0.1:8080 \
    2HUGEGRAPH_GRAPH=hugegraph \
    3HUGEGRAPH_USER=admin \
    4HUGEGRAPH_PASSWORD=admin \
    5uv run pytest hugegraph-python-client/src/tests -m "integration and hugegraph"

主线 CI 默认使用 hugegraph/hugegraph:latest 镜像进行集成验证,并对 1.7.0 执行兼容回归。源码与测试位于 hugegraph-python-client/src/pyhugegraph/ 和 hugegraph-python-client/src/tests/。

表格阅读