From 9ac232045454b07755a314a482771182884ae6af Mon Sep 17 00:00:00 2001 From: Anqi <16240361+Nicole00@users.noreply.github.com> Date: Thu, 30 Apr 2026 16:19:17 +0800 Subject: [PATCH] add example for refactored resultset --- example/GraphClientResultSetExample.py | 109 +++++++++++++++++++++++++ scripts/decode_test.py | 6 +- 2 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 example/GraphClientResultSetExample.py diff --git a/example/GraphClientResultSetExample.py b/example/GraphClientResultSetExample.py new file mode 100644 index 00000000..aa55911d --- /dev/null +++ b/example/GraphClientResultSetExample.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +"""Python example that mirrors Java GraphClientExample ResultSet usage. + +Run with: + PYTHONPATH=src python example/GraphClientResultSetExample.py +""" + +from __future__ import annotations + +import os +from typing import Iterable + +from nebulagraph_python import NebulaClient +from nebulagraph_python.py_data_types import Edge, Node +from nebulagraph_python.result_set import ResultSet +from nebulagraph_python.value_wrapper import ValueWrapper + + +def format_value(value: ValueWrapper) -> str: + """Format ValueWrapper similarly to Java GraphClientExample.resolve().""" + if value.is_null(): + return "" + if value.is_int(): + return str(value.as_int()) + if value.is_long(): + return str(value.as_long()) + if value.is_bool(): + return str(value.as_bool()) + if value.is_double(): + return str(value.as_double()) + if value.is_string(): + return value.as_string() + if value.is_date(): + return str(value.as_date()) + if value.is_local_time(): + return str(value.as_local_time()) + if value.is_zoned_time(): + return str(value.as_zoned_time()) + if value.is_local_datetime(): + return str(value.as_local_datetime()) + if value.is_zoned_datetime(): + return str(value.as_zoned_datetime()) + if value.is_list(): + return str(value.as_list()) + if value.is_record(): + return str(value.as_record()) + if value.is_node(): + node: Node = value.as_node() + _ = node.get_id(), node.get_type(), node.get_properties() + return str(node) + if value.is_edge(): + edge: Edge = value.as_edge() + _ = edge.get_src_id(), edge.get_dst_id(), edge.get_properties() + return str(edge) + return str(value.cast()) + + +def print_rows(result: ResultSet) -> None: + if not result.is_succeeded: + print(f"query failed: {result.status_message}") + return + + print(f"query result row size: {result.size}") + print(f"query latency: {result.latency_us} us") + print(f"result columns: {result.column_names}") + + # while (resultSet.hasNext()) { Record record = resultSet.next(); ... } + while result.has_next(): + record = result.next() + formatted = [f"{format_value(v):>15}" for v in record.values()] + print(" | ".join(formatted)) + + +def run_queries(client: NebulaClient, queries: Iterable[str]) -> None: + for query in queries: + print(f"\n=== Execute ===\n{query}") + result = client.execute(query) + print_rows(result) + + +def main() -> None: + host = os.getenv("NEBULA_HOST", "127.0.0.1:9669") + user = os.getenv("NEBULA_USER", "root") + password = os.getenv("NEBULA_PASSWORD", "nebula") + + client = NebulaClient( + addresses=host, + user_name=user, + password=password, + connect_timeout_ms=1000, + request_timeout_ms=3000, + ) + + try: + # Keep queries close to GraphClientExample.java's query() section. + run_queries( + client, + [ + "USE nba MATCH (v:player) RETURN v.id, v.name, v.score, v.gender, v.rate", + "USE nba MATCH ()-[e:follow]->() RETURN e.followness, e.likeness", + ], + ) + finally: + client.close() + + +if __name__ == "__main__": + main() + diff --git a/scripts/decode_test.py b/scripts/decode_test.py index b0ac3de6..dbe09cc9 100644 --- a/scripts/decode_test.py +++ b/scripts/decode_test.py @@ -8,9 +8,9 @@ def sync_client_example(): # Note: NebulaClient automatically establishes connection in __init__ # and inherits from NebulaBaseExecutor, so it can use execute_py() directly client = NebulaClient( - addresses="192.168.8.6:3820", + addresses="127.0.0.1:9669", user_name="root", - password="NebulaGraph01", + password="nebula", connect_timeout_ms=30000, request_timeout_ms=300000 ) @@ -28,7 +28,7 @@ def sync_client_example(): for i in range(runs): start_us = time.time_ns() // 1000 - result = client.execute_with_timeout("use sf100_nicole match(v:Comment) return v limit 10000", 50000) + result = client.execute_with_timeout("use sf100 match(v:Comment) return v limit 10000", 50000) res_us = time.time_ns() // 1000 for row in result: pass