View a markdown version of this page

使用 AWS 运行 OpenCypher 查询的 SDK - Amazon Neptune

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

使用 AWS 运行 OpenCypher 查询的 SDK

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

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

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

openCypher AWS SDK 示例

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

Client-side 超时和重试配置

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

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

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

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

  2. 创建一个名为的文件openCypherExample.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}) ) response = client.execute_open_cypher_query( openCypherQuery='MATCH (n) RETURN n LIMIT 1' ) print(json.dumps(response['results'], indent=2))
  3. 运行示例:python openCypherExample.py

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

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

    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.ExecuteOpenCypherQueryRequest; import software.amazon.awssdk.services.neptunedata.model.ExecuteOpenCypherQueryResponse; // 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(); ExecuteOpenCypherQueryRequest request = ExecuteOpenCypherQueryRequest.builder() .openCypherQuery("MATCH (n) RETURN n LIMIT 1") .build(); ExecuteOpenCypherQueryResponse response = client.executeOpenCypherQuery(request); System.out.println(response.results().toString());
JavaScript
  1. 按照安装说明为设置 S AWS DK JavaScript。安装 neptunedata 客户端软件包:。npm install @aws-sdk/client-neptunedata

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

    import { NeptunedataClient, ExecuteOpenCypherQueryCommand } 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); const input = { openCypherQuery: "MATCH (n) RETURN n LIMIT 1" }; const command = new ExecuteOpenCypherQueryCommand(input); const response = await client.send(command); console.log(JSON.stringify(response, null, 2));
  3. 运行示例:node openCypherExample.js