arangodb - 一个 AQL 查询中的多个 INSERT:数据修改错误后的访问
问题描述
系统设置
- ArangoDB:v3.4.7
- Java 驱动程序:v5.0.0
问题
我正在尝试在 ArangoDB 的单个查询中执行多个插入。具体如下:
INSERT { _key: @personId, firstName: @firstName, lastName: @lastName, gender: @gender, birthday: @birthday, birthday_day: @birthday_day, birthday_month: @birthday_month, creationDate: @creationDate, locationIP: @locationIP, browserUsed: @browserUsed, email:
@email, speaks: @speaks } INTO Person
INSERT { _from: Person/@personId, _to: Place/@placeId } INTO isLocatedIn
INSERT { _from: Person/@personId, _to: Tag/@tag0Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag1Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag2Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag3Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag4Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag5Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag6Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag7Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag8Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag9Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag10Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag11Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag12Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag13Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag14Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag15Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag16Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag17Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag18Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag19Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag20Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Tag/@tag21Id } INTO hasInterest
INSERT { _from: Person/@personId, _to: Organisation/@studyOrg0Id, classYear: @classYear0 } INTO studyAt
INSERT { _from: Person/@personId, _to: Organisation/@workOrg0Id, classYear: @workFrom0 } INTO workAt
INSERT { _from: Person/@personId, _to: Organisation/@workOrg1Id, classYear: @workFrom1 } INTO workAt
使用以下参数:
Params: {personId=35184372096260, firstName=André, lastName=Atem, gender=female, birthday=501897600000, birthday_day=27, birthday_month=11, creationDate=1347546700674, locationIP=41.205.16.11, browserUsed=Internet Explorer, email=[Andre35184372096260@yahoo.com], speaks=
[en], placeId=1048, tag0Id=285, tag1Id=441, tag2Id=1761, tag3Id=2019, tag4Id=2800, tag5Id=2832, tag6Id=4994, tag7Id=5116, tag8Id=5446, tag9Id=5531, tag10Id=5677, tag11Id=6369, tag12Id=7340, tag13Id=7756, tag14Id=7853, tag15Id=8219, tag16Id=8481, tag17Id=9738, tag18Id=98
68, tag19Id=10410, tag20Id=11121, tag21Id=11211, studyOrg0Id=1980, classYear0=2004, workOrg0Id=205, workFrom0=2006, workOrg1Id=209, workFrom1=2005}
但是当我尝试这样做时,我收到以下错误:
Exception in thread "main" com.arangodb.ArangoDBException: Response: 400, Error: 1579 - AQL: access after data-modification by collection 'Person' (while optimizing ast)
at com.arangodb.internal.util.ResponseUtils.checkError(ResponseUtils.java:53)
at com.arangodb.internal.velocystream.VstCommunication.checkError(VstCommunication.java:146)
at com.arangodb.internal.velocystream.VstCommunicationSync.execute(VstCommunicationSync.java:128)
at com.arangodb.internal.velocystream.VstCommunicationSync.execute(VstCommunicationSync.java:42)
at com.arangodb.internal.velocystream.VstCommunication.execute(VstCommunication.java:129)
at com.arangodb.internal.velocystream.VstProtocol.execute(VstProtocol.java:47)
at com.arangodb.internal.ArangoExecutorSync.execute(ArangoExecutorSync.java:71)
at com.arangodb.internal.ArangoExecutorSync.execute(ArangoExecutorSync.java:53)
at com.arangodb.internal.ArangoDatabaseImpl.query(ArangoDatabaseImpl.java:197)
好像在 ArangoDB 中我不能在一个查询中两次插入到同一个集合中?我是否需要为每个 INSERTS 执行单独的查询?
谢谢你的帮助!
解决方案
使用如下结构重新表述 AQL 查询可以FOR
解决问题:
INSERT { _key: @personId, firstName: @firstName, lastName: @lastName, gender: @gender, birthday: @birthday, birthday_day: @birthday_day, birthday_month: @birthday_month, creationDate: @creationDate, locationIP: @locationIP, browserUsed: @browserUsed, email: @email, speaks: @speaks } INTO Person
LET hasInterestEdges = [
{_from: "Person/35184372096260", _to: "Tag/285"},
{_from: "Person/35184372096260", _to: "Tag/441"},
{_from: "Person/35184372096260", _to: "Tag/1761"},
{_from: "Person/35184372096260", _to: "Tag/2019"},
{_from: "Person/35184372096260", _to: "Tag/2800"},
{_from: "Person/35184372096260", _to: "Tag/2832"},
{_from: "Person/35184372096260", _to: "Tag/4994"},
{_from: "Person/35184372096260", _to: "Tag/5116"},
{_from: "Person/35184372096260", _to: "Tag/5446"},
{_from: "Person/35184372096260", _to: "Tag/5531"},
{_from: "Person/35184372096260", _to: "Tag/5677"},
{_from: "Person/35184372096260", _to: "Tag/6369"},
{_from: "Person/35184372096260", _to: "Tag/7340"},
{_from: "Person/35184372096260", _to: "Tag/7756"},
{_from: "Person/35184372096260", _to: "Tag/7853"},
{_from: "Person/35184372096260", _to: "Tag/8219"},
{_from: "Person/35184372096260", _to: "Tag/8481"},
{_from: "Person/35184372096260", _to: "Tag/9738"},
{_from: "Person/35184372096260", _to: "Tag/9868"},
{_from: "Person/35184372096260", _to: "Tag/10410"},
{_from: "Person/35184372096260", _to: "Tag/11121"},
{_from: "Person/35184372096260", _to: "Tag/11211"}]
FOR hasInterestEdge IN hasInterestEdges INSERT hasInterestEdge INTO hasInterest
LET studyAtEdges = [
{_from: "Person/35184372096260", _to: "Organisation/1980", classYear: 2004}]
FOR studyAtEdge IN studyAtEdges INSERT studyAtEdge INTO studyAt
LET workAtEdges = [
{_from: "Person/35184372096260", _to: "Organisation/205", workFrom: 2006},
{_from: "Person/35184372096260", _to: "Organisation/209", workFrom: 2005}]
FOR workAtEdge IN workAtEdges INSERT workAtEdge INTO workAt
解决方案
使用 java 驱动程序,您可以使用该collection.insertDocuments
命令并传递要插入的文档数组。请参阅此处的文档
在http界面可以使用
POST /_api/document/{collection}
并传递一个带有文档数组的 json,如此处所述(请参阅创建文档部分)
最后,直接在 AQL 中,您通常会使用FOR doc in [arrayOfDocs] insert {<stuff>} into collection
以下是在 AQL 中插入多条记录的示例,该示例直接在 Web GUI 中运行:
let Params =[ {personId:35184372096260, firstName:'André', lastName:'Atem'}, {personId:35184372096261, firstName:'Robert', lastName:'Smith'} ]
for item in Params
insert item into test5
推荐阅读
- python - 如何从非拥有的不和谐服务器检索消息
- spring-boot - 使用 Spring Boot 的密码加密(使用包 org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)失败
- api - 此身份验证流程安全吗?
- docker - Redis sentinel在swarm模式下检测slave问题
- python - 将嵌套 JSON 转换为 CSV
- excel - 填充数据透视表中的空白单元格作为对上述值的引用
- algorithm - postorder 中的递归调用和打印如何工作(二叉树)?
- airflow - 谷歌云作曲家:任务没有在计划的dag运行中执行,但在手动dag运行中执行成功
- python - 使用 scipy.optimize.curve_fit 执行加权线性拟合
- vbscript - 在 wix 中,使用 vbscript,我如何写入日志文件?