View a markdown version of this page

使用 Amazon 用于运行 Gremlin 查询的 SDK - Amazon Neptune
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon 用于运行 Gremlin 查询的 SDK

借助 Amazon SDK,您可以使用自己选择的编程语言对海王星图运行 Gremlin 查询。海王星数据 API 软件开发工具包(服务名称neptunedata)提供了提交 Gremlin 查询的ExecuteGremlinQuery操作。

您必须在与 Neptune 数据库集群相同的虚拟私有云 (VPC) 中的 Amazon EC2 实例上运行这些示例,或者从与您的集群终端节点具有网络连接的位置运行这些示例。

可以在下面找到每种 SDK 语言的neptunedata服务的 API 参考文档的直接链接:

Gremlin Amazon SDK 示例

以下示例显示如何设置neptunedata客户端、运行 Gremlin 查询和打印结果。将YOUR_NEPTUNE_HOSTYOUR_NEPTUNE_PORT替换为 Neptune 数据库集群的终端节点和端口。

Client-side 超时和重试配置

SDK 客户端超时控制客户端等待响应的时间。它不控制查询在服务器上运行多长时间。如果客户端在服务器完成之前超时,则查询可能会继续在 Neptune 上运行,而客户端无法检索结果。

我们建议将客户端读取超时设置为0(无超时)或比海王星数据库集群服务器端 neptune_query_ timeout 设置长至少几秒钟的值。这让 Neptune 可以控制查询何时超时。

我们还建议将最大重试尝试次数设置为1(不重试)。如果 SDK 重试仍在服务器上运行的查询,则可能导致重复的操作。这对于突变查询尤其重要,在突变查询中,重试可能会导致意外的重复写入。

Python
  1. 按照安装说明安装 Boto3。

  2. 创建一个名为的文件gremlinExample.py并粘贴以下代码:

    import boto3 import json from botocore.config import Config # Disable the client-side read timeout and retries so that # Neptune's server-side neptune_query_timeout controls query duration. client = boto3.client( 'neptunedata', endpoint_url=f'https://YOUR_NEPTUNE_HOST:YOUR_NEPTUNE_PORT', config=Config(read_timeout=None, retries={'total_max_attempts': 1}) ) # Use the untyped GraphSON v3 serializer for a cleaner JSON response. response = client.execute_gremlin_query( gremlinQuery='g.V().limit(1)', serializer='application/vnd.gremlin-v3.0+json;types=false' ) print(json.dumps(response['result'], indent=2))
  3. 运行示例:python gremlinExample.py

Java
  1. 按照安装说明设置 Amazon 适用于 Java 的软件开发工具包。

  2. 使用以下代码设置NeptunedataClient、运行 Gremlin 查询并打印结果:

    import java.net.URI; import java.time.Duration; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.services.neptunedata.NeptunedataClient; import software.amazon.awssdk.services.neptunedata.model.ExecuteGremlinQueryRequest; import software.amazon.awssdk.services.neptunedata.model.ExecuteGremlinQueryResponse; // Disable the client-side timeout and retries so that // Neptune's server-side neptune_query_timeout controls query duration. NeptunedataClient client = NeptunedataClient.builder() .endpointOverride(URI.create("https://YOUR_NEPTUNE_HOST:YOUR_NEPTUNE_PORT")) .overrideConfiguration(ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ZERO) .retryPolicy(RetryPolicy.none()) .build()) .build(); // Use the untyped GraphSON v3 serializer for a cleaner JSON response. ExecuteGremlinQueryRequest request = ExecuteGremlinQueryRequest.builder() .gremlinQuery("g.V().limit(1)") .serializer("application/vnd.gremlin-v3.0+json;types=false") .build(); ExecuteGremlinQueryResponse response = client.executeGremlinQuery(request); System.out.println(response.result().toString());
JavaScript
  1. 按照安装说明为设置 S Amazon DK JavaScript。安装 neptunedata 客户端软件包:。npm install @aws-sdk/client-neptunedata

  2. 创建一个名为的文件gremlinExample.js并粘贴以下代码:

    import { NeptunedataClient, ExecuteGremlinQueryCommand } from "@aws-sdk/client-neptunedata"; import { NodeHttpHandler } from "@smithy/node-http-handler"; const config = { endpoint: "https://YOUR_NEPTUNE_HOST:YOUR_NEPTUNE_PORT", // Disable the client-side request timeout so that // Neptune's server-side neptune_query_timeout controls query duration. requestHandler: new NodeHttpHandler({ requestTimeout: 0 }), maxAttempts: 1 }; const client = new NeptunedataClient(config); // Use the untyped GraphSON v3 serializer for a cleaner JSON response. const input = { gremlinQuery: "g.V().limit(1)", serializer: "application/vnd.gremlin-v3.0+json;types=false" }; const command = new ExecuteGremlinQueryCommand(input); const response = await client.send(command); console.log(JSON.stringify(response, null, 2));
  3. 运行示例:node gremlinExample.js