

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

# 使用命令列工具存取 Amazon Neptune
<a name="get-started-cli-tools"></a>

您可以使用 AWS CLI、 AWS SDKs和 HTTP 工具，例如 `curl`和 `awscurl`，將查詢提交到您的 Neptune 資料庫叢集。下列各節說明如何設定每個工具，並執行基本 Gremlin 和 openCypher 查詢。

## 使用 AWS CLI
<a name="get-started-cli-tools-cli"></a>

這些`aws neptunedata`命令可讓您執行 Gremlin 和 openCypher 查詢、檢查引擎狀態、管理大量負載等。如需完整的命令參考，請參閱《 AWS CLI 命令參考》[https://docs.aws.amazon.com/cli/latest/reference/neptunedata/](https://docs.aws.amazon.com/cli/latest/reference/neptunedata/)中的 。

下列範例示範如何執行基本查詢：

------
#### [ Gremlin ]

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

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

------
#### [ openCypher ]

```
aws neptunedata execute-open-cypher-query \
  --endpoint-url https://{{your-neptune-endpoint}}:{{port}} \
  --region {{us-east-1}} \
  --open-cypher-query "MATCH (n) RETURN n LIMIT 1"
```

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

------

## 使用 AWS SDK
<a name="get-started-cli-tools-sdk"></a>

您可以透過 AWS SDKs 使用 Neptune 資料 API，以程式設計方式執行查詢。下列 Python 範例示範如何執行基本查詢：

------
#### [ Gremlin ]

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

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

------
#### [ openCypher ]

```
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='https://{{your-neptune-endpoint}}:{{port}}',
    config=Config(read_timeout=None, retries={'total_max_attempts': 1})
)

response = client.execute_open_cypher_query(
    openCypherQuery='MATCH (n) RETURN n LIMIT 1'
)

print(json.dumps(response['results'], indent=2))
```

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

------

## 使用 `curl`和 `awscurl`
<a name="get-started-cli-tools-curl"></a>

[curl](https://curl.haxx.se/) 命令列工具會將 HTTP 請求直接提交至 Neptune 端點。如果啟用 IAM 身分驗證，請使用 [awscurl](https://github.com/okigan/awscurl) 或 `curl` 7.75.0\+ 搭配 `--aws-sigv4`選項來簽署請求。如需詳細資訊，請參閱[使用 `awscurl` 搭配臨時憑證，安全地連線至啟用 IAM 身分驗證的資料庫叢集](iam-auth-connect-command-line.md#iam-auth-connect-awscurl)。

### 設定 `curl` HTTPS
<a name="get-started-cli-tools-curl-setup"></a>

若要使用 HTTPS 連線 （如 Neptune 在大多數區域中的需求）， `curl`需要存取適當的憑證。如需如何取得憑證並將其格式化為憑證授權機構 (CA) 存放區的詳細資訊，請參閱 `curl` 文件中的 [SSL 憑證驗證](https://curl.haxx.se/docs/sslcerts.html)。

您可以使用 `CURL_CA_BUNDLE`環境變數指定此 CA 憑證存放區的位置。在 Windows 上，`curl` 會自動在名為 `curl-ca-bundle.crt` 的檔案中尋找它。它會先在和 `curl.exe` 相同的目錄中尋找，然後再尋找路徑的其他位置。如需詳細資訊，請參閱 [SSL Certificate Verification](https://curl.haxx.se/docs/sslcerts.html)。

只要 `curl` 可以找到適當的憑證，它處理 HTTPS 連線的方式就跟 HTTP 連線一樣，無需額外參數。此文件的範例是以該案例為基礎。

如需工具的詳細資訊，請參閱 [curl man 頁面](https://curl.haxx.se/docs/manpage.html)和書籍 *[Everything curl](https://ec.haxx.se/)*。

### 查詢範例
<a name="get-started-cli-tools-curl-examples"></a>

下列範例示範如何使用 `curl`和 執行基本查詢`awscurl`：

------
#### [ Gremlin (awscurl) ]

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

------
#### [ Gremlin (curl) ]

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

**注意**  
只有在叢集上未啟用 IAM 身分驗證時，Plain 才能`curl`運作。

------
#### [ openCypher (awscurl) ]

```
awscurl https://{{your-neptune-endpoint}}:{{port}}/openCypher \
  --region {{us-east-1}} \
  --service neptune-db \
  -X POST \
  -d "query=MATCH (n) RETURN n LIMIT 1"
```

------
#### [ openCypher (curl) ]

```
curl -X POST \
  -d "query=MATCH (n) RETURN n LIMIT 1" \
  https://{{your-neptune-endpoint}}:{{port}}/openCypher
```

**注意**  
只有在叢集上未啟用 IAM 身分驗證時，Plain 才能`curl`運作。

------

如需更多 Gremlin HTTP 範例，請參閱 [HTTPS REST](access-graph-gremlin-rest.md)。如需 openCypher HTTP 範例的詳細資訊，請參閱 [HTTPS 端點](access-graph-opencypher-queries.md)。