快速入门:适用于 Node.js 的 Azure Cosmos DB for Apache Gremlin 客户端库

适用对象: Gremlin

Azure Cosmos DB for Apache Gremlin 是一种完全托管的图形数据库服务,用于实现常用的 Apache Tinkerpop(使用 Gremlin 查询语言的图形计算框架)。 API for Gremlin 为你提供了一种低摩擦方式来将 Gremlin 与服务结合使用,可通过最少的管理根据需要进行增长和横向扩展。

在本快速入门中,你会使用gremlin库连接到新建的 Azure Cosmos DB for Gremlin 帐户。

库源代码 | 包 (npm)

先决条件

设置

本部分会引导你创建 API for Gremlin 帐户和设置 Node.js 项目以使用该库连接到帐户。

创建 API for Gremlin 帐户

在使用 Node.js 库之前,应先创建 API for Gremlin 帐户。 此外,它还有助于设置数据库和图形。

  1. 为 accountName、resourceGroupName 和 location 创建 shell 变量。

    # Variable for resource group name
    resourceGroupName="msdocs-cosmos-gremlin-quickstart"
    location="westus"
    
    # Variable for account name with a randomly generated suffix
    
    let suffix=$RANDOM*$RANDOM
    accountName="msdocs-gremlin-$suffix"
    
  2. 如果尚未登录到 Azure CLI,请使用 az login 登录。

  3. 使用 az group create 在订阅中创建新的资源组。

    az group create \
        --name $resourceGroupName \
        --location $location
    
  4. 使用 az cosmosdb create 创建具有默认设置的新 API for Gremlin 帐户。

    az cosmosdb create \
        --resource-group $resourceGroupName \
        --name $accountName \
        --capabilities "EnableGremlin" \
        --locations regionName=$location \
        --enable-free-tier true
    
  5. 使用 az cosmosdb show 获取帐户的 API for Gremlin 终结点 NAME

    az cosmosdb show \
        --resource-group $resourceGroupName \
        --name $accountName \
        --query "name"
    
  6. 使用 az-cosmosdb-keys-list 从帐户的密钥列表中查找 KEY

    az cosmosdb keys list \
        --resource-group $resourceGroupName \
        --name $accountName \
        --type "keys" \
        --query "primaryMasterKey"
    
  7. 记录 NAMEKEY 的值。 稍后会使用这些凭据。

  8. 使用 az cosmosdb gremlin database create 创建名为“cosmicworks”的数据库

    az cosmosdb gremlin database create \
        --resource-group $resourceGroupName \
        --account-name $accountName \
        --name "cosmicworks"
    
  9. 使用 az cosmosdb gremlin graph create 创建图形。 将图形命名为products,然后将吞吐量设置为400,最后将分区键路径设置为/category

    az cosmosdb gremlin graph create \
        --resource-group $resourceGroupName \
        --account-name $accountName \
        --database-name "cosmicworks" \
        --name "products" \
        --partition-key-path "/category" \
        --throughput 400
    

新建 Node.js 控制台应用程序

使用首选终端在空文件夹中新建 Node.js 控制台应用程序。

  1. 在空文件夹中打开终端。

  2. 初始化新模块

    npm init es6 --yes
    
  3. 创建app.js文件

    touch app.js
    

安装 npm 包

gremlin npm 包添加到 Node.js 项目。

  1. 打开package.json文件,并将内容替换为此 JSON 配置。

    {
      "main": "app.js",
      "type": "module",
      "scripts": {
        "start": "node app.js"
      },
      "dependencies": {
        "gremlin": "^3.*"
      }
    }
    
  2. 使用npm install命令安装package.json文件中指定的所有包。

    npm install
    

配置环境变量

要使用本快速入门中前面获得的 NAMEURI 值,请将其保存到运行应用程序的本地计算机上的新环境变量。

  1. 要设置环境变量,请使用终端将值分别保留为“COSMOS_ENDPOINT”和“COSMOS_KEY”。

    export COSMOS_GREMLIN_ENDPOINT="<account-name>"
    export COSMOS_GREMLIN_KEY="<account-key>"
    
  2. 验证是否已正确设置环境变量。

    printenv COSMOS_GREMLIN_ENDPOINT
    printenv COSMOS_GREMLIN_KEY
    

代码示例

本文中的代码连接到名为“cosmicworks”的数据库和名为“products”的图形。 然后,代码在遍历添加的项之前,将顶点和边缘添加到图形。

验证客户端

对大多数 Azure 服务的应用程序请求必须获得授权。 对于 API for Gremlin,请使用本快速入门前面获取的名称URI值。

  1. 打开app.js文件。

  2. 导入 gremlin 模块。

// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. 创建 accountNameaccountKey 变量。 将COSMOS_GREMLIN_ENDPOINTCOSMOS_GREMLIN_KEY环境变量存储为每个相应变量的值。
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. 使用PlainTextSaslAuthenticator为帐户的凭据新建对象。
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. 使用Client通过远程服务器凭据和GraphSON 2.0序列化程序进行连接。 然后,使用Open新建与服务器的连接。
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

创建顶点

现在,应用程序已连接到帐户,请使用标准 Gremlin 语法创建顶点。

  1. 使用“submit”在 API for Gremlin 帐户的服务器端运行命令。 使用以下属性创建产品顶点:

    label product
    id 68719518371
    name Kiama classic surfboard
    price 285.55
    category surfboards
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. 使用以下属性创建第二个产品顶点:

    label product
    id 68719518403
    name Montau Turtle Surfboard
    price 600.00
    category surfboards
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. 使用以下属性创建第三个产品顶点:

    label product
    id 68719518409
    name Bondi Twin Surfboard
    price 585.50
    category surfboards
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

创建边缘

使用 Gremlin 语法创建边缘,以定义顶点之间的关系。

  1. 创建从名为“替换”的 Montau Turtle Surfboard 产品到 Kiama classic surfboard 产品的边缘。
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

提示

此边缘定义使用 g.V(['<partition-key>', '<id>']) 语法。 或者,也可使用 g.V('<id>').has('category', '<partition-key>')

  1. 创建从同一产品到 Bondi Twin Surfboard 的另一个替换边缘。
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

查询顶点和边缘

使用 Gremlin 语法遍历图形并发现顶点之间的关系。

  1. 遍历图形并查找 Montau Turtle Surfboard 替换的所有顶点。
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. 将此遍历的结果写入控制台。
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

运行代码

运行应用程序以验证应用程序是否按预期工作。 应用程序执行时应没有错误或警告。 应用程序的输出包括有关创建项和查询项的数据。

  1. 在 Node.js 项目文件夹中打开终端。

  2. 使用npm <script>运行应用程序。 观察应用程序的输出。

    npm start
    

清理资源

当不再需要 API for Gremlin 帐户时,删除相应的资源组。

  1. 为 resourceGroupName 创建 shell 变量(如果尚不存在)。

    # Variable for resource group name
    resourceGroupName="msdocs-cosmos-gremlin-quickstart"
    
  2. 使用 az group delete 删除资源组。

    az group delete \
        --name $resourceGroupName
    

下一步