AWS SDK を使用して openCypher クエリを実行する - Amazon Neptune

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK を使用して openCypher クエリを実行する

AWS SDK を使用すると、任意のプログラミング言語を使用して、Neptune グラフに対して openCypher クエリを実行できます。Neptune データ API SDK (サービス名 neptunedata) は、openCypher クエリを送信するための ExecuteOpenCypherQuery openCypher アクションを提供します。

これらの例は、Neptune DB クラスターと同じ Virtual Private Cloud (VPC) の Amazon EC2 インスタンス、またはクラスターエンドポイントへのネットワーク接続がある場所から実行する必要があります。

各 SDK 言語のneptunedataサービスの API リファレンスドキュメントへの直接リンクは、以下にあります。

openCypher AWS SDK の例

次の例は、neptunedataクライアントをセットアップし、openCypher クエリを実行し、結果を出力する方法を示しています。YOUR_NEPTUNE_HOSTYOUR_NEPTUNE_PORT を Neptune DB クラスターのエンドポイントとポートに置き換えます。

クライアント側のタイムアウトと再試行の設定

SDK クライアントのタイムアウトは、クライアントがレスポンスを待機する時間を制御します。クエリがサーバー上で実行される時間は制御されません。サーバーが終了する前にクライアントがタイムアウトした場合、クライアントが結果を取得する方法がない間、クエリは Neptune で実行し続ける可能性があります。

クライアント側の読み取りタイムアウトを 0 (タイムアウトなし) に設定するか、Neptune DB クラスターのサーバー側の 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 SDK for 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. インストール手順に従って AWS SDK for 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