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

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

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

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

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

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

Gremlin AWS SDK の例

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

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

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

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