

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

# AWS SDK を使用して Gremlin クエリを実行する
<a name="access-graph-gremlin-sdk"></a>

 AWS SDK を使用すると、任意のプログラミング言語を使用して Neptune グラフに対して Gremlin クエリを実行できます。Neptune データ API SDK (サービス名 `neptunedata`) は、Gremlin クエリを送信するための [ExecuteGremlinQuery](https://docs.aws.amazon.com/neptune/latest/data-api/API_ExecuteGremlinQuery.html) アクションを提供します。

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

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


| プログラミング言語 | Neptunedata API リファレンス | 
| --- | --- | 
| C\+\+ | [https://sdk.amazonaws.com/cpp/api/LATEST/aws-cpp-sdk-neptunedata/html/annotated.html](https://sdk.amazonaws.com/cpp/api/LATEST/aws-cpp-sdk-neptunedata/html/annotated.html) | 
| Go | [https://docs.aws.amazon.com/sdk-for-go/api/service/neptunedata/](https://docs.aws.amazon.com/sdk-for-go/api/service/neptunedata/) | 
| Java | [https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/neptunedata/package-summary.html](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/neptunedata/package-summary.html) | 
| JavaScript | [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-neptunedata/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-neptunedata/) | 
| Kotlin | [https://sdk.amazonaws.com/kotlin/api/latest/neptunedata/index.html](https://sdk.amazonaws.com/kotlin/api/latest/neptunedata/index.html) | 
| .NET | [https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Neptunedata/NNeptunedata.html](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Neptunedata/NNeptunedata.html) | 
| PHP | [https://docs.aws.amazon.com/aws-sdk-php/v3/api/namespace-Aws.Neptunedata.html](https://docs.aws.amazon.com/aws-sdk-php/v3/api/namespace-Aws.Neptunedata.html) | 
| Python | [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/neptunedata.html](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/neptunedata.html) | 
| Ruby | [https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Neptunedata.html](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Neptunedata.html) | 
| Rust | [https://crates.io/crates/aws-sdk-neptunedata](https://crates.io/crates/aws-sdk-neptunedata) | 
| CLI | [https://docs.aws.amazon.com/cli/latest/reference/neptunedata/](https://docs.aws.amazon.com/cli/latest/reference/neptunedata/) | 

## Gremlin AWS SDK の例
<a name="access-graph-gremlin-sdk-examples"></a>

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

**クライアント側のタイムアウトと再試行の設定**  
SDK クライアントのタイムアウトは、*クライアント*がレスポンスを待機する時間を制御します。クエリがサーバーで実行される時間は制御されません。サーバーが終了する前にクライアントがタイムアウトした場合、クライアントが結果を取得する方法がない間、クエリは Neptune で実行し続ける可能性があります。  
クライアント側の読み取りタイムアウトを `0` (タイムアウトなし) に設定するか、Neptune DB クラスターのサーバー側の [neptune\_query\_timeout](parameters.md#parameters-db-cluster-parameters-neptune_query_timeout) 設定よりも少なくとも数秒長い値に設定することをお勧めします。これにより、Neptune はクエリのタイムアウトを制御できます。  
また、最大再試行回数を設定することをお勧めします `1` (再試行なし）。SDK がサーバーでまだ実行されているクエリを再試行すると、オペレーションが重複する可能性があります。これは、再試行によって意図しない重複書き込みが発生する可能性があるミューテーションクエリでは特に重要です。

------
#### [ Python ]

1. Boto3 をインストールするには、[インストール手順に従い](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html)ます。

1. という名前のファイルを作成し`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))
   ```

1. 例を実行します。 `python gremlinExample.py`

------
#### [ Java ]

1. [インストール手順に従って](https://docs.aws.amazon.com//sdk-for-java/latest/developer-guide/setup.html) AWS SDK for Java をセットアップします。

1. 次のコードを使用して をセットアップし`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. [インストール手順に従って](https://docs.aws.amazon.com//sdk-for-javascript/v3/developer-guide/getting-started-nodejs.html) AWS SDK for JavaScript をセットアップします。neptunedata クライアントパッケージをインストールします: `npm install @aws-sdk/client-neptunedata`。

1. という名前のファイルを作成し`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));
   ```

1. 例を実行します。 `node gremlinExample.js`

------