This section introduces methods for managing graphs in the database.
showGraph()
Retrieves all graphs from the database.
Parameters
config: RequestConfig
(Optional): Request configuration.
Returns
List[GraphSet]
: The list of all graphs in the database.
# Retrieves all graphs and prints the names of those with over 2000 edges
graphs = Conn.showGraph()
for graph in graphs:
if graph.totalEdges > 2000:
print(graph.name)
Display_Ad_Click
ERP_DATA2
wikiKG
getGraph()
Retrieves a specified graph from the database.
Parameters
graphName: str
: Name of the graph.config: RequestConfig
(Optional): Request configuration.
Returns
GraphSet
: The retrieved graph.
# Retrieves the graph named 'miniCircle'
graph = Conn.getGraph("miniCircle")
print(graph.toJSON())
{"id": "444", "name": "miniCircle", "totalNodes": 304, "totalEdges": 1961, "shards": ["1"], "partitionBy": "CityHash64", "status": "NORMAL", "description": "", "slotNum": 256}
hasGraph()
Checks the existence of a specified graph in the database.
Parameters
graphName: str
: Name of the graph.config: RequestConfig
(Optional): Request configuration.
Returns
bool
: Check result.
# Checks the existence of a graph named 'miniCircle'
response = Conn.hasGraph("miniCircle")
print(response)
True
createGraph()
Creates a graph in the database.
Parameters
graphSet: GraphSet
: The graph to be created; the fieldname
is mandatory,shards
,partitionBy
anddescription
are optional.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
# Creates a graph
graph = GraphSet(
name="testPythonSDK",
shards=[1],
partitionBy="Crc32",
description="testPythonSDK desc"
)
response = Conn.createGraph(graph)
print(response.status.code)
SUCCESS
createGraphIfNotExist()
Creates a graph in the database and returns whether a graph with the same name already exists.
Parameters
graphSet: GraphSet
: The graph to be created; the fieldname
is mandatory,shards
,partitionBy
anddescription
are optional.config: RequestConfig
(Optional): Request configuration.
Returns
Dict[bool, Response]
: Whether the graph already exists, and response of the request.
graph = GraphSet(
name="testPythonSDK",
shards=[1],
partitionBy="Crc32",
description="testPythonSDK desc"
)
result = Conn.createGraphIfNotExist(graph)
boolValue = result.get('exist')
response = result.get('response')
print("Does the graph already exist?", boolValue)
if response.status is None:
print("Graph creation status: No response")
else:
print("Graph creation status:", response.status.code)
print("----- Creates the graph again -----")
result_1 = Conn.createGraphIfNotExist(graph)
boolValue_1 = result_1.get('exist')
response_1 = result_1.get('response')
print("Does the graph already exist?", boolValue_1)
if response_1.status is None:
print("Graph creation status: No response")
else:
print("Graph creation status:", response_1.status.code)
Does the graph already exist? False
Graph creation status: SUCCESS
----- Creates the graph again -----
Does the graph already exist? True
Graph creation status: No response
dropGraph()
Deletes a specified graph from the database.
Parameters
graphName: str
: Name of the graph.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Reponse of the request.
# Creates a graph and then drops it
graph = GraphSet(
name="testPythonSDK",
shards=[1],
partitionBy="Crc32",
description="testPythonSDK desc"
)
response1 = Conn.createGraph(graph)
print("Graph creation:", response1.status.code)
response2 = Conn.dropGraph("testPythonSDK")
print("Graph deletion:", response2.status.code)
Graph creation: SUCCESS
Graph deletion: SUCCESS
alterGraph()
Alters the name and description of a graph in the database.
Parameters
graphName: str
: Name of the graph.alterGraphset: GraphSet
: AGraphSet
object used to set the newname
and/ordescription
for the graph.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
# Alters the name and description of the graph 'testPythonSDK'
newGraphInfo = GraphSet(name='newGraph', description="a new graph")
response = Conn.alterGraph("testPythonSDK", newGraphInfo)
print(response.status.code)
SUCCESS
truncate()
Truncates (Deletes) the specified nodes or edges in a graph or truncates the entire graph. Note that truncating nodes will cause the deletion of edges attached to those affected nodes. The truncating operation retains the definition of schemas and properties in the graph.
Parameters
request: TruncateParams
: The truncate parameters; the fieldgraphName
is mandatory,schemaName
anddbType
are optional while they must be set together.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
# Truncates User nodes in 'myGraph'
param1 = TruncateParams(graphName="myGraph", schemaName="User", dbType=DBType.DBNODE)
response1 = Conn.truncate(param1)
print(response1.status.code)
# Truncates all edges in the 'myGraph'
param2 = TruncateParams(graphName="myGraph", schemaName="*", dbType=DBType.DBEDGE)
response2 = Conn.truncate(param2)
print(response2.status.code)
# Truncates 'myGraph'
param3 = TruncateParams(graphName="myGraph")
response3 = Conn.truncate(param3)
print(response3.status.code)
SUCCESS
SUCCESS
SUCCESS
compact()
Clears invalid and redundant data for a graph. Valid data will not be affected.
Parameters
graphName: str
: Name of the graph.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
# Compacts the graph 'miniCircle'
response = Conn.compact("miniCircle")
print(response.status.code)
SUCCESS
Full Example
from ultipa import UltipaConfig, Connection, GraphSet
ultipaConfig = UltipaConfig()
# URI example: ultipaConfig.hosts = ["https://mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
ultipaConfig.hosts = ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"]
ultipaConfig.username = "<username>"
ultipaConfig.password = "<password>"
Conn = Connection.NewConnection(defaultConfig=ultipaConfig)
# Creates a graph
graph = GraphSet(
name="testPythonSDK",
shards=[1],
partitionBy="Crc32",
description="testPythonSDK desc"
)
response = Conn.createGraph(graph)
print(response.status.code)