Version 1.5 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot.
For up-to-date documentation, see the latest version.
The hugegraph-python-client is a Python client/SDK for HugeGraph Database.
It is used to define graph structures, perform CRUD operations on graph data, manage schemas, and execute Gremlin queries. Both the hugegraph-llm and hugegraph-ml modules depend on this foundational library.
Installation
Install the released package (Stable)
To install the hugegraph-python-client, you can use uv/pip or source code building:
1# uv is optional, you can use pip directly2uv pip install hugegraph-python # Note: may not the latest version, recommend to install from source3# WIP: we will use 'hugegraph-python-client' as the package name soon
Install from Source (Latest Code)
To install from the source, clone the repository and install the required dependencies:
You can use the hugegraph-python-client to define graph structures. Below is an example of how to define a graph:
1frompyhugegraph.clientimportPyHugeClient 2 3# Initialize the client 4# For HugeGraph API version ≥ v3: (Or enable graphspace function) 5# - The 'graphspace' parameter becomes relevant if graphspaces are enabled.(default name is 'DEFAULT') 6# - Otherwise, the graphspace parameter is optional and can be ignored. 7client=PyHugeClient("127.0.0.1","8080",user="admin",pwd="admin",graph="hugegraph",graphspace="DEFAULT") 8 9''''
10Note:
11Could refer to the official REST-API doc of your HugeGraph version for accurate details.
12If some API is not as expected, please submit a issue or contact us.
13''''14schema=client.schema()15schema.propertyKey("name").asText().ifNotExist().create()16schema.propertyKey("birthDate").asText().ifNotExist().create()17schema.vertexLabel("Person").properties("name","birthDate").usePrimaryKeyId().primaryKeys("name").ifNotExist().create()18schema.vertexLabel("Movie").properties("name").usePrimaryKeyId().primaryKeys("name").ifNotExist().create()19schema.edgeLabel("ActedIn").sourceLabel("Person").targetLabel("Movie").ifNotExist().create()2021print(schema.getVertexLabels())22print(schema.getEdgeLabels())23print(schema.getRelations())2425# Init Graph26g=client.graph()27v_al_pacino=g.addVertex("Person",{"name":"Al Pacino","birthDate":"1940-04-25"})28v_robert=g.addVertex("Person",{"name":"Robert De Niro","birthDate":"1943-08-17"})29v_godfather=g.addVertex("Movie",{"name":"The Godfather"})30v_godfather2=g.addVertex("Movie",{"name":"The Godfather Part II"})31v_godfather3=g.addVertex("Movie",{"name":"The Godfather Coda The Death of Michael Corleone"})3233g.addEdge("ActedIn",v_al_pacino.id,v_godfather.id,{})34g.addEdge("ActedIn",v_al_pacino.id,v_godfather2.id,{})35g.addEdge("ActedIn",v_al_pacino.id,v_godfather3.id,{})36g.addEdge("ActedIn",v_robert.id,v_godfather2.id,{})3738res=g.getVertexById(v_al_pacino.id).label39print(res)40g.close()
Schema Management
The hugegraph-python-client provides comprehensive schema management capabilities.
Define Property Keys
1# Define a property key2client.schema().propertyKey('name').dataType('STRING').cardinality('SINGLE').create()
Define Vertex Labels
1# Define a vertex label2client.schema().vertexLabel('person').properties('name','age').primaryKeys('name').create()
Define Edge Labels
1# Define an edge label2client.schema().edgeLabel('knows').sourceLabel('person').targetLabel('person').properties('since').create()
Define Index Labels
1# Define an index label2client.schema().indexLabel('personByName').onV('person').by('name').secondary().create()
CRUD Operations
The client allows you to perform CRUD operations on the graph data. Below are examples of how to create, read, update, and delete vertices and edges:
Create Vertices and Edges
1# Create vertices2v1=client.graph().addVertex('person').property('name','John').property('age',29).create()3v2=client.graph().addVertex('person').property('name','Jane').property('age',25).create()45# Create an edge6client.graph().addEdge(v1,'knows',v2).property('since','2020').create()
Read Vertices and Edges
1# Get a vertex by ID2vertex=client.graph().getVertexById(v1.id)3print(vertex)45# Get an edge by ID6edge=client.graph().getEdgeById(edge.id)7print(edge)
Update Vertices and Edges
1# Update a vertex2client.graph().updateVertex(v1.id).property('age',30).update()34# Update an edge5client.graph().updateEdge(edge.id).property('since','2021').update()
Delete Vertices and Edges
1# Delete a vertex2client.graph().deleteVertex(v1.id)34# Delete an edge5client.graph().deleteEdge(edge.id)
Execute Gremlin Queries
The client also supports executing Gremlin queries:
1# Execute a Gremlin query2g=client.gremlin()3res=g.exec("g.V().limit(5)")4print(res)
Other info is under 🚧 (Welcome to add more docs for it, users could refer java-client-doc for similar usage)
Contributing
Welcome to contribute to hugegraph-python-client. Please see the Guidelines for more information.
Code format: Please run ./style/code_format_and_analysis.sh to format your code before submitting a PR.
Thank you to all the people who already contributed to hugegraph-python-client!
Contact Us
GitHub Issues: Feedback on usage issues and functional requirements (quick response)
↑↓ Navigate↵ Openesc Close
Ask AI
Agree to send your questions to the third-party Kapa service for AI answers based on the documentation.