

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

# 使用 HTTP REST 端點連線至 Neptune 資料庫執行個體
<a name="access-graph-gremlin-rest"></a>

Amazon Neptune 會提供 HTTP 端點進行 Gremlin 查詢。REST 介面與您的資料庫叢集正在使用的任何 Gremlin 版本相容 (請參閱您正在使用之 Neptune 引擎版本的[引擎版本頁面](engine-releases.md)，以確定其支援哪個 Gemlin 版本)。

**注意**  
如 [使用 SSL/HTTPS 加密 Amazon Neptune 資料庫的連線](security-ssl.md) 中所討論，Neptune 現在要求您使用 HTTPS 而不是 HTTP 進行連線。此外，Neptune 目前不支援 HTTP/2 for REST API 請求。連接到端點時，用戶端必須使用 HTTP/1.1。

以下說明引導您使用 `curl` 命令和 HTTPS 連接到 Gremlin 端點。您必須從與您的 Neptune 資料庫執行個體位於同一虛擬私有雲端 (VPC) 的 Amazon EC2 執行個體依照以下指示進行。

對 Neptune 資料庫執行個體進行 Gremlin 查詢時所用的 HTTPS 端點為 `https://{{your-neptune-endpoint}}:{{port}}/gremlin`。

**注意**  
如需尋找 Neptune 資料庫執行個體主機名稱的相關資訊，請參閱 [連線至 Amazon Neptune 端點](feature-overview-endpoints.md)。

## 使用 HTTP REST 端點連線至 Neptune
<a name="access-graph-gremlin-rest-connect"></a>

下列範例示範如何將 Gremlin 查詢提交至 REST 端點。您可以使用 AWS SDK AWS CLI、 或 **curl**。

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

```
aws neptunedata execute-gremlin-query \
  --endpoint-url https://{{your-neptune-endpoint}}:{{port}} \
  --gremlin-query "g.V().limit(1)"
```

如需詳細資訊，請參閱《 AWS CLI 命令參考》中的 [execute-gremlin-query](https://docs.aws.amazon.com/cli/latest/reference/neptunedata/execute-gremlin-query.html)。

------
#### [ SDK ]

```
import boto3
import json
from botocore.config import Config

client = boto3.client(
    'neptunedata',
    endpoint_url='https://{{your-neptune-endpoint}}:{{port}}',
    config=Config(read_timeout=None, retries={'total_max_attempts': 1})
)

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))
```

如需 Java、.NET 等其他語言的 AWS SDK 範例，請參閱 [AWS 開發套件](access-graph-gremlin-sdk.md)。

------
#### [ awscurl ]

```
awscurl https://{{your-neptune-endpoint}}:{{port}}/gremlin \
  --region {{us-east-1}} \
  --service neptune-db \
  -X POST \
  -d '{"gremlin":"g.V().limit(1)"}'
```

**注意**  
此範例假設您的 AWS 登入資料已在您的環境中設定。將 {{us-east-1}} 取代為 Neptune 叢集的區域。

如需使用 **awscurl**搭配 IAM 身分驗證的詳細資訊，請參閱 [使用 `awscurl` 搭配臨時憑證，安全地連線至啟用 IAM 身分驗證的資料庫叢集](iam-auth-connect-command-line.md#iam-auth-connect-awscurl)。

------
#### [ curl ]

以下範例使用 **curl** 透過 HTTP **POST** 提交 Gremlin 查詢。查詢是以 POST 本文中的 JSON 格式做為 `gremlin` 屬性提交。

```
curl -X POST -d '{"gremlin":"g.V().limit(1)"}' https://{{your-neptune-endpoint}}:{{port}}/gremlin
```

雖然建議使用 HTTP **POST** 請求來傳送 Gremlin 查詢，但也可以使用 HTTP **GET** 請求：

```
curl -G "https://{{your-neptune-endpoint}}:{{port}}?gremlin=g.V().count()"
```

------

這些範例會使用周`g.V().limit(1)`遊傳回圖形中的第一個頂點。您可以查詢其他項目，方法是將其取代為另一個 Gremlin 周遊。

**重要**  
根據預設，REST 端點會以單一 JSON 結果集傳回所有結果。如果此結果集太大，Neptune 資料庫執行個體上可能會發生 `OutOfMemoryError` 例外狀況。  
您可以啟用區塊回應 (以一系列個別回應傳回的結果) 來避免這種情況。請參閱 [使用選用的 HTTP 結尾標頭來啟用多部分 Gremlin 回應](access-graph-gremlin-rest-trailing-headers.md)。

**注意**  
Neptune 不支援 `bindings` 屬性。