A Python SDK for Apache HugeGraph
Project description
hugegraph-python-client
The hugegraph-python-client is a Python client(SDK( for HugeGraph.
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
To install the hugegraph-python-client, you can use pip:
pip3 install hugegraph-python
Install from Source (Latest Code)
To install from the source, clone the repository and install the required dependencies:
git clone https://github.com/apache/incubator-hugegraph-ai.git
cd path/to/hugegraph-python-client
# Normal install
pip install .
# (Optional) install the devel version
pip install -e .
Usage
Defining Graph Structures
You can use the hugegraph-python-client to define graph structures. Below is an example of how to define a graph:
from pyhugegraph.client import PyHugeClient
# Initialize the client
# For HugeGraph API version ≥ v3: (Or enable graphspace function)
# - The 'graphspace' parameter becomes relevant if graphspaces are enabled.(default name is 'DEFAULT')
# - Otherwise, the graphspace parameter is optional and can be ignored.
client = PyHugeClient("127.0.0.1", "8080", user="admin", pwd="admin", graph="hugegraph", graphspace="DEFAULT")
""""
Note:
Could refer to the official REST-API doc of your HugeGraph version for accurate details.
If some API is not as expected, please submit a issue or contact us.
"""
schema = client.schema()
schema.propertyKey("name").asText().ifNotExist().create()
schema.propertyKey("birthDate").asText().ifNotExist().create()
schema.vertexLabel("Person").properties("name", "birthDate").usePrimaryKeyId().primaryKeys("name").ifNotExist().create()
schema.vertexLabel("Movie").properties("name").usePrimaryKeyId().primaryKeys("name").ifNotExist().create()
schema.edgeLabel("ActedIn").sourceLabel("Person").targetLabel("Movie").ifNotExist().create()
print(schema.getVertexLabels())
print(schema.getEdgeLabels())
print(schema.getRelations())
# Init Graph
g = client.graph()
g.addVertex("Person", {"name": "Al Pacino", "birthDate": "1940-04-25"})
g.addVertex("Person", {"name": "Robert De Niro", "birthDate": "1943-08-17"})
g.addVertex("Movie", {"name": "The Godfather"})
g.addVertex("Movie", {"name": "The Godfather Part II"})
g.addVertex("Movie", {"name": "The Godfather Coda The Death of Michael Corleone"})
g.addEdge("ActedIn", "12:Al Pacino", "13:The Godfather", {})
g.addEdge("ActedIn", "12:Al Pacino", "13:The Godfather Part II", {})
g.addEdge("ActedIn", "12:Al Pacino", "13:The Godfather Coda The Death of Michael Corleone", {})
g.addEdge("ActedIn", "12:Robert De Niro", "13:The Godfather Part II", {})
res = g.getVertexById("12:Al Pacino").label
print(res)
g.close()
Schema Management
The hugegraph-python-client provides comprehensive schema management capabilities.
Define Property Keys
# Define a property key
client.schema().propertyKey('name').dataType('STRING').cardinality('SINGLE').create()
Define Vertex Labels
# Define a vertex label
client.schema().vertexLabel('person').properties('name', 'age').primaryKeys('name').create()
Define Edge Labels
# Define an edge label
client.schema().edgeLabel('knows').sourceLabel('person').targetLabel('person').properties('since').create()
Define Index Labels
# Define an index label
client.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
# Create vertices
v1 = client.graph().addVertex('person').property('name', 'John').property('age', 29).create()
v2 = client.graph().addVertex('person').property('name', 'Jane').property('age', 25).create()
# Create an edge
client.graph().addEdge(v1, 'knows', v2).property('since', '2020').create()
Read Vertices and Edges
# Get a vertex by ID
vertex = client.graph().getVertexById(v1.id)
print(vertex)
# Get an edge by ID
edge = client.graph().getEdgeById(edge.id)
print(edge)
Update Vertices and Edges
# Update a vertex
client.graph().updateVertex(v1.id).property('age', 30).update()
# Update an edge
client.graph().updateEdge(edge.id).property('since', '2021').update()
Delete Vertices and Edges
# Delete a vertex
client.graph().deleteVertex(v1.id)
# Delete an edge
client.graph().deleteEdge(edge.id)
Execute Gremlin Queries
The client also supports executing Gremlin queries:
# Execute a Gremlin query
g = client.gremlin()
res = g.exec("g.V().limit(5)")
print(res)
Other info are 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.shto 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)
- Feedback Email: dev@hugegraph.apache.org (subscriber only)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hugegraph_python-1.5.0.tar.gz.
File metadata
- Download URL: hugegraph_python-1.5.0.tar.gz
- Upload date:
- Size: 33.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d62b86a78b84fff86cd2ae87e44620a6c26bbe89aee8311f00e9812a0f2eb22
|
|
| MD5 |
186b467eb377fd3cb887763a1668bd9b
|
|
| BLAKE2b-256 |
0930cb7268eaaa637c2f28e620d7f912cb04cc7951da3cb833bc33c95ec62efd
|
File details
Details for the file hugegraph_python-1.5.0-py3-none-any.whl.
File metadata
- Download URL: hugegraph_python-1.5.0-py3-none-any.whl
- Upload date:
- Size: 72.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ea0a672e42181be51158709e30722bca65fa5df951ed6d664a947ef90f8b54f
|
|
| MD5 |
b2aaf0cc79c8ab35e821b62cc4cd5bae
|
|
| BLAKE2b-256 |
833cc47be5ca2aa2e304776989b8a6951dcb5f3e05967febca11d320c74ddf94
|