

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 搭配 Neptune 使用 AWS CLI 和 SDKs最佳實務
<a name="best-practices-cli-sdk"></a>

根據預設， AWS CLI 和 AWS SDKs會在 60 秒後逾時，並自動重試失敗的請求。對於需要超過 60 秒才能完成的查詢，此預設行為會導致重複的查詢執行。重試會在原始查詢仍在伺服器上執行時觸發新的請求。

**重要**  
當您透過 Neptune 資料平面 API 執行長時間執行的查詢 （例如 `execute-open-cypher-query`) 時，請停用自動重試。同時增加或移除讀取逾時。否則，用戶端會在原始伺服器仍在執行時重試查詢，這會加倍 Neptune 叢集的負載。

下列建議適用於執行時間可能超過預設 60 秒逾時的任何 Neptune 資料平面操作：
+ 將讀取逾時設定為 0 （無限） 或大於您最長預期查詢持續時間的值。
+ 將嘗試次數上限設定為 1 （一次嘗試，零次重試）。這可防止用戶端重試伺服器仍在處理的請求。

下列範例示範如何為 AWS CLI 和熱門 SDKs 設定這些設定。

------
#### [ 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()`停用所有自動重試。

------