

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

# 使用的最佳实践 AWS CLI 还有带有 Neptune 的 SDK
<a name="best-practices-cli-sdk"></a>

默认情况下， AWS CLI 和 AWS SDK 会在 60 秒后超时，并自动重试失败的请求。对于完成时间超过 60 秒的查询，此默认行为会导致重复执行查询。当原始查询仍在服务器上运行时，重试会触发一个新的请求。

**重要**  
当您通过 Neptune 数据平面 API 运行长时间运行的查询时（例如`execute-open-cypher-query`，），请禁用自动重试。还要增加或删除读取超时时间。否则，客户端会在原始查询仍在执行时重试查询，这会使您的 Neptune 集群的负载增加一倍。

以下建议适用于任何运行时间可能超过默认 60 秒超时的 Neptune 数据平面操作：
+ 将读取超时设置为 0（无限）或大于预期的最长查询持续时间的值。
+ 将总尝试次数设置为 1（一次尝试，零次重试）。这可以防止客户端重试服务器仍在处理的请求。

以下示例说明如何为 AWS CLI 和常用 SDK 配置这些设置。

------
#### [ AWS CLI ]

```
export AWS_MAX_ATTEMPTS=1
aws neptunedata execute-open-cypher-query \
  --endpoint-url https://{{your-cluster-endpoint}}:{{port}} \
  --open-cypher-query "RETURN 1" \
  --cli-read-timeout 0
```

该`--cli-read-timeout 0`参数禁用读取超时，允许无限期等待响应。 AWS CLI `AWS_MAX_ATTEMPTS=1`环境变量将总尝试次数设置为 1，这意味着请求只尝试过一次，不重试。

------
#### [ Python (boto3) ]

```
import boto3
from botocore.config import Config

client = boto3.client(
    'neptunedata',
    endpoint_url='https://{{your-cluster-endpoint}}:{{port}}',
    region_name='{{us-east-1}}',
    config=Config(
        retries={'total_max_attempts': 1, 'mode': 'standard'},
        read_timeout=None
    )
)
```

设置`read_timeout=None`禁用读取超时。设置`total_max_attempts`为`1`意味着该请求只被尝试过一次，没有重试。

------
#### [ Java V2 SDK ]

```
import software.amazon.awssdk.services.neptunedata.NeptunedataClient;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.retry.RetryPolicy;
import software.amazon.awssdk.regions.Region;
import java.time.Duration;
import java.net.URI;

// Build the Neptune dataplane client with no retries and infinite timeout
NeptunedataClient neptunedataClient = NeptunedataClient.builder()
    .endpointOverride(URI.create("https://{{your-cluster-endpoint}}:{{port}}"))
    .region(Region.of("{{us-east-1}}"))
    .overrideConfiguration(ClientOverrideConfiguration.builder()
        .apiCallTimeout(Duration.ZERO)
        .retryPolicy(RetryPolicy.none())
        .build())
    .build();
```

设置`apiCallTimeout`为`Duration.ZERO`可配置 API 调用的无限超时。使用`RetryPolicy.none()`会禁用所有自动重试。

------