sql - SQL查询一次在多个顶点之间创建边
问题描述
我正在尝试使用 SQL 查询在 CosmosDB 中创建一个图形。
我已经以 JSON 格式将数据集上传到 CosmosDB。我有一堆顶点显示。在我的 JSON 文件中,我存储了两种类型的标签:订单和产品。订单顶点包含有关购买产品的信息(见下文)
{
"id": "o0001",
"label": "Order",
"type": "vertex",
"products": [
{
"Product2": 1.0
},
{
"Product3": 1.0
},
{
"Product4": 1.0
},
{
"Product5": 1.0
},
{
"Product6": 1.0
},
{
"Product7": 1.0
},
{
"Product8": 1.0
},
{
"Product24": 1.0
},
{
"Product25": 1.0
},
{
"Product26": 1.0
},
{
"Product27": 1.0
}
]
}
...
{
"id": "Product02",
"label": "Product",
"type": "vertex"
}
...
我想编写一个 SQL 查询,将每个订单连接到所有购买的产品(即在每个订单与其产品之间创建边)。我怎样才能一次为所有订单执行此操作,而不仅仅是一个特定的订单?
解决方案
据我所知,没有这样的 sql 可以一次为所有订单创建所有边。由于您的订单产品结构是肯定的,我建议您使用带有 Graph API cosmos db 的批量执行器库。
示例代码:
IBulkExecutor graphbulkExecutor = new GraphBulkExecutor(documentClient, targetCollection);
BulkImportResponse vResponse = null;
BulkImportResponse eResponse = null;
try
{
// Import a list of GremlinVertex objects
vResponse = await graphbulkExecutor.BulkImportAsync(
Utils.GenerateVertices(numberOfDocumentsToGenerate),
enableUpsert: true,
disableAutomaticIdGeneration: true,
maxConcurrencyPerPartitionKeyRange: null,
maxInMemorySortingBatchSize: null,
cancellationToken: token);
// Import a list of GremlinEdge objects
eResponse = await graphbulkExecutor.BulkImportAsync(
Utils.GenerateEdges(numberOfDocumentsToGenerate),
enableUpsert: true,
disableAutomaticIdGeneration: true,
maxConcurrencyPerPartitionKeyRange: null,
maxInMemorySortingBatchSize: null,
cancellationToken: token);
}
catch (DocumentClientException de)
{
Trace.TraceError("Document client exception: {0}", de);
}
catch (Exception e)
{
Trace.TraceError("Exception: {0}", e);
}
边缘对象:
// Creating an edge
GremlinEdge e = new GremlinEdge(
"edgeId",
"edgeLabel",
"targetVertexId",
"sourceVertexId",
"targetVertexLabel",
"sourceVertexLabel",
"targetVertexPartitioningKey",
"sourceVertexPartitioningKey");
// Adding custom properties to the edge
e.AddProperty("customProperty", "value");
您可以先查询所有订单,然后使用Utils.GenerateEdges
方法为循环中的每个订单创建边。
推荐阅读
- geodjango - GeoDjango 将传单插件添加到管理站点
- python - Kivy,ScreenManager:如何访问另一个屏幕中定义的变量?
- javascript - ES6 可以在函数范式中原生地对 Set 上的排列进行编码吗?
- api - 无法通过 JavaScript 从 Office365 生成 OAuth 2.0 访问令牌
- java - 语法错误(Sqlite 代码 1 SQLITE_ERROR)
- c - 不使用 [] 索引运算符的数组
- python - Flask 在页面加载时将电子邮件列表显示为 [email protected]
- c++ - 为数组分配内存
- vue.js - 如何解决通过 CDN 导入 TailwindCSS 工作正常但将其安装为 NPM 依赖项的问题不仅在 codeandbox.io 上?
- javascript - 在鼠标悬停事件期间为动态生成的表格添加弹出窗口