简体   繁体   English

在 Python 中,如何为 arangodb 创建边缘集合和文档?

[英]In Python, how do I create an edge collection and document for an arangodb?

I'm trying to write a python script to create an edge collection and insert edges into my arango database.我正在尝试编写一个 python 脚本来创建一个边集合并将边插入到我的 arango 数据库中。 The ArangoDB documentation includes how to create a document collection and a document in that collection: ArangoDB 文档包括如何创建文档集合和该集合中的文档:

UsersCollection= db.createCollection(name = "Users") doc = usersCollection.createDocument() doc["name"] = "Tesla" doc.save()

However, when I try to adapt this to creating an edge collection or adding an edge to an edge collection, it doesn't work.但是,当我尝试将其调整为创建边缘集合或将边缘添加到边缘集合时,它不起作用。 The first step doesn't allow for changing the collection to any type other than document.第一步不允许将集合更改为文档以外的任何类型。 Since that's the first step to selecting a collection to reference, I haven't actually gotten to the step of trying to add to an edge collection that already exists.由于这是选择要引用的集合的第一步,因此我实际上还没有到尝试添加到已经存在的边缘集合的步骤。

Is this possible?这可能吗?

Update更新

I ended up going about this in a different way by using the API.我最终通过使用 API 以不同的方式解决了这个问题。 My issue was that I was trying to do a bulk import of NSJ of my edges.我的问题是我试图批量导入边缘的 NSJ。 When I attempted to do an import as you would a document collection, it would create a document collection followed by related issues of import an edge to a document collection.当我尝试像导入文档集合一样进行导入时,它会创建一个文档集合,然后是将边缘导入文档集合的相关问题。 To fix this, I did the follow:为了解决这个问题,我做了以下事情:

Step 1: Manually created my collection with my edge collection name within the db interface.第 1 步:在 db 界面中使用我的边缘集合名称手动创建我的集合。

Step 2: I then wrote this code, passed my NSF edges as f and my edge collection name as name .第 2 步:然后我编写了这段代码,将我的 NSF 边作为f传递,将我的边集合名称作为name传递。

def upload_Collection(f, name):
   filez = open(f).read()
   url = "/_api/import?type=auto&collection=%s&createCollection=false&overwrite=true&waitForSync=false&complete=false&details=true" % (
    name)
   r = requests.post(url, data=filez)
   return r.text

Sorry it took me so long to get back to this!对不起,我花了这么长时间才回到这个! I hope this is help to others!我希望这对其他人有帮助!

Notes笔记

I chose not use the ArangoDB python driver because, honestly, I found it confusing to actually implement.我选择不使用 ArangoDB python 驱动程序,因为老实说,我发现实际实现很混乱。 Thanks for the suggestion anyway, @dothebart !无论如何,感谢您的建议,@dothebart!

I ended up going about this in a different way by using the API. 我最终使用API​​以另一种方式进行了研究。 My issue was that I was trying to do a bulk import of NSJ of my edges. 我的问题是我试图大量导入NSJ。 When I attempted to do an import as you would a document collection, it would create a document collection followed by related issues of import an edge to a document collection. 当我尝试像导入文档集合一样进行导入时,它将创建一个文档集合,然后是将边导入文档集合的相关问题。 To fix this, I did the follow: 为了解决这个问题,我做了以下工作:

Step 1: Manually created my collection with my edge collection name within the db interface. 步骤1:在db界面中使用边缘集合名称手动创建我的集合。

Step 2: I then wrote this code, passed my NSF edges as f and my edge collection name as name . 步骤2:然后,我编写了这段代码,将NSF边传递为f ,将边集合名称传递为name

def upload_Collection(f, name):
   filez = open(f).read()
   url = "/_api/import?type=auto&collection=%s&createCollection=false&overwrite=true&waitForSync=false&complete=false&details=true" % (
    name)
   r = requests.post(url, data=filez)
   return r.text

Sorry it took me so long to get back to this! 抱歉,我花了很长时间才回到这个! I hope this is help to others! 希望对别人有帮助!

There is available className='Edges' option in the createCollection constructor such as createCollection构造函数中有可用的className='Edges'选项,例如

from pyArango.connection import *

conn = Connection(username="root", password="") # set your password
db = conn.createDatabase(name="TestDB")

users = db.createCollection(name="User")
edges = db.createCollection(name="edges", className='Edges')

users.createDocument({'_key':u1, 'name':'Alice'}).save()
users.createDocument({'_key':u2, 'name':'Bob'}).save()
edges.createDocument({'_from':'User/u1', '_to':'User/u2'}).save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM