本节介绍用于管理数据库中图的方法。
showGraph()
从数据库获取全部图。
参数
config: RequestConfig
(可选):请求配置。
返回值
List[GraphSet]
:获取的数据库全部图列表。
# 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()
从数据库获取一个指定的图。
参数
graphName: str
:图名称。config: RequestConfig
(可选):请求配置。
返回值
GraphSet
:获取的图。
# 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()
检查数据库中是否存在一个指定的图。
参数
graphName: str
:图名称。config: RequestConfig
(可选):请求配置。
返回值
bool
:检查结果。
# Checks the existence of a graph named 'miniCircle'
response = Conn.hasGraph("miniCircle")
print(response)
True
createGraph()
在数据库中创建一个图。
参数
graphSet: GraphSet
:待创建的图;name
字段必填,shards
、partitionBy
和description
可选。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
# Creates a graph
graph = GraphSet(
name="testPythonSDK",
shards=[1],
partitionBy="Crc32",
description="testPythonSDK desc"
)
response = Conn.createGraph(graph)
print(response.status.code)
SUCCESS
createGraphIfNotExist()
在数据库中创建一个图,并返回是否数据库中已有同名图存在。
参数
graphSet: GraphSet
:待创建的图;name
字段必填,shards
、partitionBy
和description
可选。config: RequestConfig
(可选):请求配置。
返回值
Dict[bool, Response]
: 图集是否存在,以及请求结果
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()
从数据库中删除一个指定的图。
参数
graphName: str
:图名称。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
# 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()
修改数据库中一个图的名称和描述。
参数
graphName: str
:图名称。alterGraphset: GraphSet
:用于设置新的图name
和/或description
的GraphSet
对象。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
# 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()
清空(删除)图中的指定点或边,或清空整个图。请注意,清空点的同时会删除与点相连的所有边。清空操作仅删除点边,图的schema和属性定义仍保留。
参数
request: TruncateParams
:清空操作的参数;graphName
字段必填,schemaName
和dbType
可选但必须同时设置。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
# 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()
清除图中的无效及冗余数据。有效数据不会受到影响。
参数
graphName: str
:图名称。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
# Compacts the graph 'miniCircle'
response = Conn.compact("miniCircle")
print(response.status.code)
SUCCESS
完整示例
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)