gpt4 book ai didi

azure-cosmosdb - 将 Documents 和 Graph 存储在同一个 DocumentDb 集合中

转载 作者:行者123 更新时间:2023-11-30 23:58:51 24 4
gpt4 key购买 nike

所以我正在试验 CosmosDb 并希望使用 Gremlin API 进行顶点和边缘查询。我有许多实体之间会有多个边缘连接,例如“知道”、“信任”、“喜欢”等

然而,我确实希望将复杂的文档作为顶点,因此我没有使用具有许多属性的文本查询的 gremlin 语法(这看起来很麻烦),而是使用 documentDB API 插入了文档。问题是一旦添加了文档(它正确地返回查询并显示在 SQL 查询中),我就不能在没有错误的情况下进行任何 g.V() 调用:

System.InvalidCastException: Invalid cast from 'System.String' to 'Newtonsoft.Json.Linq.JObject'.

我也无法向这些顶点添加边。

所以我的问题是,我是否试图以错误的方式做到这一点?我的复杂对象是否应该保存在一个单独的集合中,然后将它们的 id 镜像到新集合中的顶点上?

不能在同一个集合上使用 Gremlin API 和 Document API 吗?

感谢您的任何建议。

理查德

Oliver 的更新

这是一个简单的测试文档,它使用 documentdb 库创建得很好,然后在我在数据资源管理器中执行 SQL Select * from c 时显示:
 {
"id": "beea4756-2a25-4e2e-a7d9-457ad03dd56d",
"title": "Test item",
"description": "Test description",
"purchaseUrl": "http://www.test.co.uk",
"imageUrls": [
"http://www.test.com",
"http://www.test2.com"
],
"label": "item",
"created": "2017-10-09T12:41:49.0201322Z",
"modified": "2017-10-09T12:41:49.0203021Z",
"createdById": "TestUserId",
"modifiedById": "TestUserId",
"_rid": "jXNCAKbsIQABAAAAAAAAAA==",
"_self": "dbs/jXNCAA==/colls/jXNCAKbsIQA=/docs/jXNCAKbsIQABAAAAAAAAAA==/",
"_etag": "\"00000629-0000-0000-0000-59db6e8d0000\"",
"_attachments": "attachments/",
"_ts": 1507552909
}

这是我在 Azure 门户的图形资源管理器中使用 g.V() 时得到的错误(或者在我的代码中尝试任何 gremlin 查询或边创建时):
    System.InvalidCastException: Invalid cast from 'System.String' to 'Newtonsoft.Json.Linq.JObject'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
at Newtonsoft.Json.Linq.Extensions.<Convert>d__14`2.MoveNext()
at Microsoft.Azure.Graphs.Runtime.VertexPropertyField.Replace(JProperty jproperty)
at Microsoft.Azure.Graphs.Runtime.VertexField..ctor(GraphCommand graphCommand, JObject vertexObject)
at Microsoft.Azure.Graphs.Common.VertexObjectCache.AddOrUpdateVertexField(VertexCompositeKey vertexKey, JObject vertexObject)
at Microsoft.Azure.Graphs.Runtime.DocumentDBRuntime.<GetVertices>d__1.MoveNext()
at Microsoft.Azure.Graphs.Runtime.Operators.FetchNodeOperator.<ComputeNextRecordAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Graphs.Runtime.Operators.GraphExecutionOperator.<NextAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Graphs.Runtime.Operators.ProjectOperator.<ComputeNextRecordAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Graphs.Runtime.Operators.GraphExecutionOperator.<NextAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Graphs.GraphTraversal.GraphTraversalIterator.<CurrentOperatorNextAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Graphs.GraphTraversal.GraphTraversalIterator.<MoveNextAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Graphs.GraphTraversal.<MoveNextAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Graphs.GremlinDocumentQuery`1.<ExecuteNextAsync>d__15`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Graphs.GremlinDocumentQuery`1.<ExecuteNextAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Portal.PlatformProxy.Graph.Controllers.GremlinQueriesController.<SubmitQueryAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Portal.PlatformProxy.Graph.Controllers.GremlinQueriesController.<Post>d__0.MoveNext()

最佳答案

可以在同一集合中混合和匹配 Graph 和 Document API。但是,它仅在您以图形文档预期的 GraphSON 格式插入顶点和边时才有效。如果您测试为边和顶点运行一些 gremlin 插入,然后使用文档 API 检查集合中文档的格式,您将看到预期的格式是什么样的。

在我的工作中,我们构建了一个自定义 ORM,它采用 C# POCO 来表示我们的顶点和边,并在使用 Document API 进行实际插入之前使用反射构建适当的结构。这样你就可以两全其美。

您在此处提到的另一种选择是拥有一个用于表示纯关系的集合和另一个用于存储实际文档的集合,这也可能是一个可行的解决方案,具体取决于您正在构建的应用程序的性质。进行额外的网络调用以填写实际文档以及错过 SPROC 等形式的交易肯定会影响性能。

关于azure-cosmosdb - 将 Documents 和 Graph 存储在同一个 DocumentDb 集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46647373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com