Arangodb 如何使用现有集合(文档和边)创建图形
我正在使用pyArango Python包创建几个集合(文档(顶点)和边)。如何使用现有顶点和边以编程方式创建图形?我知道如何使用ArangoDB web界面“AddGraph”创建一个图形,但由于有大量集合,这需要一项繁琐的任务 在从WebGui检查网络流之后,我发现AddGraph本质上是发送一个json post请求,其中Arangodb 如何使用现有集合(文档和边)创建图形,arangodb,Arangodb,我正在使用pyArango Python包创建几个集合(文档(顶点)和边)。如何使用现有顶点和边以编程方式创建图形?我知道如何使用ArangoDB web界面“AddGraph”创建一个图形,但由于有大量集合,这需要一项繁琐的任务 在从WebGui检查网络流之后,我发现AddGraph本质上是发送一个json post请求,其中Edge和Document连接名称作为字符串输入。因此,阻止我们使用现有边缘和文档的是pyarango中构建的默认验证 def _checkCollectionList(
Edge
和Document
连接名称作为字符串输入。因此,阻止我们使用现有边缘和文档的是pyarango中构建的默认验证
def _checkCollectionList(lst):
for colName in lst:
if not COL.isCollection(colName):
raise ValueError("'%s' is not a defined Collection" % colName)
graphClass = GR.getGraphClass(name)
ed = []
for e in graphClass._edgeDefinitions:
if not COL.isEdgeCollection(e.edgesCollection):
raise ValueError("'%s' is not a defined Edge Collection" % e.edgesCollection)
_checkCollectionList(e.fromCollections)
_checkCollectionList(e.toCollections)
ed.append(e.toJson())
_checkCollectionList(graphClass._orphanedCollections)
为了克服这个问题,我们只需要在创建图之前先实现类。例如,我有两个文档集合人员和部门,我想创建一个图表:
- 人->知识->人
- 人员->所属->部门
from pyArango.collection import Collection, Field
from pyArango.graph import Graph, EdgeDefinition
class knowns(Edges):
pass
class belongs_to(Edges):
pass
class people(Collection):
pass
class department(Collection):
pass
# Here's how you define a graph
class MyGraph(Graph) :
_edgeDefinitions = [EdgeDefinition('knowns', ['people'], ['people']),
EdgeDefinition('belongs_to', ['people'], ['department'])]
_orphanedCollections = []
theGraph = arrango_db.createGraph(name="MyGraph", createCollections=False)
请注意,类名与我传递到图形创建中的字符串完全相同。现在,我们在ArangoDB中创建了图形:
另一个解决方案是使用
类型动态创建类,该解决方案在您已经定义了节点和边时非常有用:
from pyArango.graph import Graph, EdgeDefinition
from pyArango.collection import Collection, Edges
# generate the class for "knowns", inheriting class "Edges", etc.
_ = type("knowns", (Edges,), {})
_ = type("belongs_to", (Edges,), {})
_ = type("people", (Collection,), {})
_ = type("department", (Collection,), {})
# finally generate the class for the graph
_ = type(attribute+'Graph', (Graph,),
{"_edgeDefinitions" : [EdgeDefinition('knowns', ['people'], ['people']),
EdgeDefinition('belongs_to', ['people'], ['department'])],
"_orphanedCollections" : []})
db.createGraph(name=MyGraph, createCollections=False)
嗨,你明白了吗?我面临着同样的问题,但却没有发现任何有用的东西。。。