你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于 .NET 的 Azure Cosmos DB for Apache Gremlin 客户端库入门,用于存储、管理和查询非结构化数据。 按照本指南中的步骤创建新帐户、安装 .NET 客户端库、连接到帐户、执行常见作以及查询最终示例数据。
Prerequisites
Azure 订阅服务
- 如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
Azure Cloud Shell 中最新版本的 Azure CLI。
- 如果想要在本地运行 CLI 引用命令,请使用
az login
该命令登录到 Azure CLI。
- 如果想要在本地运行 CLI 引用命令,请使用
- .NET SDK 9.0 或更高版本
设置
首先,为本指南设置帐户和开发环境。 本部分将指导你完成创建帐户、获取其凭据以及准备开发环境的过程。
创建帐户
首先创建用于 Apache Gremlin 帐户的 API。 创建帐户后,创建数据库和图形资源。
- Azure CLI
- Azure 门户
如果还没有目标资源组,请使用
az group create
命令在订阅中创建新的资源组。az group create \ --name "<resource-group-name>" \ --location "<location>"
使用
az cosmosdb create
命令创建具有默认设置的新 Azure Cosmos DB for Apache Gremlin 帐户。az cosmosdb create \ --resource-group "<resource-group-name>" \ --name "<account-name>" \ --locations "regionName=<location>" \ --capabilities "EnableGremlin"
使用
az cosmosdb gremlin database create
创建一个名为cosmicworks
的新数据库。az cosmosdb gremlin database create \ --resource-group "<resource-group-name>" \ --account-name "<account-name>" \ --name "cosmicworks"
使用
az cosmosdb gremlin graph create
命令创建一个名为products
的新图形。az cosmosdb gremlin graph create \ --resource-group "<resource-group-name>" \ --account-name "<account-name>" \ --database-name "cosmicworks" \ --name "products" \ --partition-key-path "/category"
获取凭据
现在,获取用于创建与最近创建的帐户的连接的客户端库的密码。
- Azure CLI
- Azure 门户
使用
az cosmosdb show
获取帐户的主机。az cosmosdb show \ --resource-group "<resource-group-name>" \ --name "<account-name>" \ --query "{host:name}"
记录来自上一个命令输出的属性
host
的值。 此属性的值是你在本指南后面部分用于通过库连接到帐户的主机。使用
az cosmosdb keys list
来获取帐户的 密钥。az cosmosdb keys list \ --resource-group "<resource-group-name>" \ --name "<account-name>" \ --type "keys"
记录来自上一个命令输出的属性
primaryMasterKey
的值。 此属性的值是本指南稍后使用的 密钥 ,用于通过库连接到帐户。
准备开发环境
然后,使用新项目和客户端库配置开发环境。 在转到本指南的其余部分之前,此步骤是最后一个必需的先决条件。
从空文件夹开始。
创建新的 .NET 控制台应用程序
dotnet new console
从 NuGet 导入
Gremlin.Net
包。dotnet add package Gremlin.Net
构建项目。
dotnet build
对象模型
DESCRIPTION | |
---|---|
GremlinClient |
表示用于连接和与 Gremlin 服务器交互的客户端 |
GraphTraversalSource |
用于构造和执行 Gremlin 遍历 |
代码示例
对客户端进行身份验证
首先,使用本指南前面收集的凭据对客户端进行身份验证。
在集成开发环境(IDE)中打开 Program.cs 文件。
删除文件中的任何现有内容。
为以下命名空间添加 using 指令:
Gremlin.Net.Driver
Gremlin.Net.Structure.IO.GraphSON
using Gremlin.Net.Driver; using Gremlin.Net.Structure.IO.GraphSON;
为本指南前面收集的凭据创建字符串变量。 命名变量
hostname
和primaryKey
.string hostname = "<host>"; string primaryKey = "<key>";
使用在前面步骤中创建的凭据和配置变量来创建
GremlinServer
。 为变量server
命名。GremlinServer server = new( $"{hostname}.gremlin.cosmos.azure.com", 443, enableSsl: true, username: "/dbs/cosmicworks/colls/products", password: primaryKey );
现在,使用
server
变量和GraphSON2MessageSerializer
配置创建一个GremlinClient
。GremlinClient client = new( server, new GraphSON2MessageSerializer() );
插入数据
接下来,在图形中插入新的顶点和边缘数据。 创建新数据之前,请清除任何现有数据的图形。
g.V().drop()
运行查询以清除图形中的所有顶点和边缘。await client.SubmitAsync("g.V().drop()");
创建添加顶点的 Gremlin 查询。
string insertVertexQuery = """ g.addV('product') .property('id', prop_id) .property('name', prop_name) .property('category', prop_category) .property('quantity', prop_quantity) .property('price', prop_price) .property('clearance', prop_clearance) """;
为单个产品添加顶点。
await client.SubmitAsync(insertVertexQuery, new Dictionary<string, object> { ["prop_id"] = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb", ["prop_name"] = "Yamba Surfboard", ["prop_category"] = "gear-surf-surfboards", ["prop_quantity"] = 12, ["prop_price"] = 850.00, ["prop_clearance"] = false });
为两个额外产品再添加两个顶点。
await client.SubmitAsync(insertVertexQuery, new Dictionary<string, object> { ["prop_id"] = "bbbbbbbb-1111-2222-3333-cccccccccccc", ["prop_name"] = "Montau Turtle Surfboard", ["prop_category"] = "gear-surf-surfboards", ["prop_quantity"] = 5, ["prop_price"] = 600.00, ["prop_clearance"] = true }); await client.SubmitAsync(insertVertexQuery, new Dictionary<string, object> { ["prop_id"] = "cccccccc-2222-3333-4444-dddddddddddd", ["prop_name"] = "Noosa Surfboard", ["prop_category"] = "gear-surf-surfboards", ["prop_quantity"] = 31, ["prop_price"] = 1100.00, ["prop_clearance"] = false });
创建另一个用于添加边的 Gremlin 查询。
string insertEdgeQuery = """ g.V([prop_partition_key, prop_source_id]) .addE('replaces') .to(g.V([prop_partition_key, prop_target_id])) """;
添加两个边缘。
await client.SubmitAsync(insertEdgeQuery, new Dictionary<string, object> { ["prop_partition_key"] = "gear-surf-surfboards", ["prop_source_id"] = "bbbbbbbb-1111-2222-3333-cccccccccccc", ["prop_target_id"] = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" }); await client.SubmitAsync(insertEdgeQuery, new Dictionary<string, object> { ["prop_partition_key"] = "gear-surf-surfboards", ["prop_source_id"] = "bbbbbbbb-1111-2222-3333-cccccccccccc", ["prop_target_id"] = "cccccccc-2222-3333-4444-dddddddddddd" });
读取数据
然后,读取以前插入到图形中的数据。
创建使用唯一标识符和分区键值读取顶点的查询。
string readVertexQuery = "g.V([prop_partition_key, prop_id])";
然后,通过提供所需的参数来读取顶点。
ResultSet<Dictionary<string, object>> readResults = await client.SubmitAsync<Dictionary<string, object>>(readVertexQuery, new Dictionary<string, object> { ["prop_partition_key"] = "gear-surf-surfboards", ["prop_id"] = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" }); Dictionary<string, object> matchedItem = readResults.Single();
查询数据
最后,使用查询查找与图中特定遍历或筛选器匹配的所有数据。
创建一个查询,用于查找从特定顶点遍历的所有顶点。
string findVerticesQuery = """ g.V().hasLabel('product') .has('category', prop_partition_key) .has('name', prop_name) .outE('replaces').inV() """;
执行指定
Montau Turtle Surfboard
产品的查询。ResultSet<Dictionary<string, object>> findResults = await client.SubmitAsync<Dictionary<string, object>>(findVerticesQuery, new Dictionary<string, object> { ["prop_partition_key"] = "gear-surf-surfboards", ["prop_name"] = "Montau Turtle Surfboard" });
遍历查询结果。
foreach (Dictionary<string, object> result in findResults) { // Do something here with each result }
运行代码
使用应用程序目录中的终端运行新创建的应用程序。
dotnet run
清理资源
如果不再需要此帐户,请通过删除资源来从 Azure 订阅中删除该帐户。
- Azure CLI
- Azure 门户
az cosmosdb delete \
--resource-group "<resource-group-name>" \
--name "<account-name>"