所以我正在尝试使用CosmosDb,并希望将Gremlin API用于Vertex和Edge查询。我有许多实体,它们之间有多个边缘连接,如“知道”,“信任”,“喜欢”等等。
但是我希望将复杂的文档作为顶点,所以不要使用具有许多属性的文本查询的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库创建的简单测试文档,然后在我从数据资源管理器中的c执行SQL Select *时显示:
{
"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()
答案 0 :(得分:3)
可以在同一个集合中混合和匹配Graph和Document API。但是,只有以GraphSON格式插入图形文档所需的顶点和边时,它才有效。如果您测试为边和顶点运行一些gremlin插入,然后使用Document API检查集合中文档的格式,您将看到预期格式的样子。
在我的工作中,我们构建了一个自定义ORM,它使用C#POCO表示我们的顶点和边缘,并在使用Document API进行实际插入之前使用反射构建适当的结构。通过这种方式,您可以获得两全其美的效果。
您在此处提到的另一个选项是,根据您正在构建的应用程序的性质,有一个用于表示纯关系的集合和另一个用于存储实际文档的集合。肯定会有额外的网络电话填写实际文件以及错过SPROC等形式的交易。