首页 > 解决方案 > 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 查询,将每个订单连接到所有购买的产品(即在每个订单与其产品之间创建边)。我怎样才能一次为所有订单执行此操作,而不仅仅是一个特定的订单?

标签: sqlazure-cosmosdbazure-cosmosdb-sqlapiazure-cosmosdb-gremlinapi

解决方案


据我所知,没有这样的 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方法为循环中的每个订单创建边。


推荐阅读