使用 Cosmos DB Graph 一次插入多个顶点-API

Insert multiple Vertices at once using Cosmos DB Graph-API

我希望使用 Azure Cosmos DB Graph 快速插入多个顶点-API。大多数当前的 Microsoft 示例都一个一个地创建顶点并为每个顶点执行 Gremlin 查询,如下所示:

IDocumentQuery<dynamic> query = client.CreateGremlinQuery<dynamic>(graph, "g.addV('person').property('id', 'thomas').property('name', 'Thomas').property('age', 44)");

while (query.HasMoreResults)
{                    
    foreach (dynamic result in await query.ExecuteNextAsync())  {   
        Console.WriteLine($"\t {JsonConvert.SerializeObject(result)}"); 
    }
    Console.WriteLine();
}


query = client.CreateGremlinQuery<dynamic>(graph, "g.addV('person').property('id', 'mary').property('name', 'Mary').property('lastName', 'Andersen').property('age', 39)");

while (query.HasMoreResults)
{                    
    foreach (dynamic result in await query.ExecuteNextAsync())  {   
        Console.WriteLine($"\t {JsonConvert.SerializeObject(result)}"); 
    }
    Console.WriteLine();
}

但是,当我想创建几千个顶点和边来初始填充图形时,这不太理想,因为这可能需要一些时间。

这是 Microsoft.Azure.Graphs 库 v0.2.0-preview

如何高效地将多个顶点一次添加到 Cosmos DB,以便稍后可以使用 Graph API 语法进行查询?

假设 CosmosDB 100% 兼容 TinkerPop,并且根据 gremlin 执行程序超时设置,您应该能够更新您的 gremlin 脚本以同时执行多项操作。

例如:

g.addV('person').property('id', 'mary').property('name', 'Mary').property('lastName', 'Andersen').property('age', 39)

可以转化为:

g.addV('person').property('id', 'mary').property('name', 'Mary').property('lastName', 'Andersen').property('age', 39); g.addV('person').property('id', 'david').property('name', 'David').property('lastName', 'P').property('age', 24);

等等等等

您的 gremlin 脚本也只是 Groovy 代码,因此您甚至可以编写循环以及无法创建顶点、附加属性等的内容。

我发现为图表设置种子的最快方法实际上是使用文档 API。利用这种技术,我已经能够在一台开发机器上每秒插入 5500+ vertices/edges。诀窍是理解 Cosmos 期望的边和顶点格式。只需通过 gremlin API 向图形中添加几个顶点和边,然后通过转到 Azure 中的数据资源管理器并执行文档查询来检查这些文档的格式 SELECT * FROM c.

在工作中,我构建了一个轻型 ORM,它使用反射来获取边缘和顶点的 POCO,并将它们转换为您在门户中看到的格式。我希望尽快开源,届时我很可能会发布一个 Nuget 包和随附的博客 post。希望与此同时,这会帮助您指明正确的方向,如果您对这种方法有更多疑问,请告诉我。

数据迁移工具可能支持 SQL API 或 MongoDB 场景,但它不支持图 api 顶点 - 边阶段。如前所述,您可能会使用生成的图形查询结果作为主要参考模式,然后在您的源上进行一些搜索和替换...以最终得到正确的格式...虽然我发现只是 运行ning 一个控制台应用程序流数据可能更充足。我能够在 Marvel 和机场航班场景中重用相同的控制台应用程序,我需要做的就是每次修改几行代码。 代码是 运行 的 2 个序列。第一个块提取并转换顶点。第二个序列提取字段关系并将其转换为边。我需要修改的只是我需要提取的字段。这可能需要一些时间来转换,具体取决于数据的大小,尽管它每次都能给我准确的预期结果,而不必不断地修改源数据。

我们需要一个工具来帮助我们将数据迁移到 cosmosdb 图表,但由于没有可用的工具,我最终创建了这个 - https://github.com/microsoft/migratetograph

您可以使用它从某些 sql 或 json 中获取数据,对其进行转换并将其推送到图形数据库。 它支持并行执行 gremlin 查询,因此速度相当快。
默认情况下,它会并行触发 10 个 gremlin 查询,但您可以通过在 graph-config 文件中传递 batchSize 来增加它

我正在使用此代码通过 NodeJS 更新多顶点

const __ = gremlin.process.statics;
let trt = await g.withBulk(true).V('test-3').fold().coalesce(__.unfold().property(gremlin.process.cardinality.single, 'runways', 4), __.addV('truongtest').property(gremlin.process.t.id, 'test-3').property(gremlin.process.cardinality.single, 'runways', 4))
        .V('test-10').fold().coalesce(__.unfold().property(gremlin.process.cardinality.single, 'runways', 100), __.addV('truongtest').property(gremlin.process.t.id, 'test-10').property(gremlin.process.cardinality.single, 'runways', 100))
        .next()
        
// if you wanna add alot , using loop 

let trt = await g.withBulk(true)
trt = trt.V('test-3').fold().coalesce(__.unfold().property(gremlin.process.cardinality.single, 'runways', 4), __.addV('truongtest').property(gremlin.process.t.id, 'test-3').property(gremlin.process.cardinality.single, 'runways', 4))
        
trt = trt.V('test-10').fold().coalesce(__.unfold().property(gremlin.process.cardinality.single, 'runways', 100), __.addV('truongtest').property(gremlin.process.t.id, 'test-10').property(gremlin.process.cardinality.single, 'runways', 100))

// after done run next()
trt.next()