

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

# 使用命令行工具访问亚马逊 Neptune
<a name="get-started-cli-tools"></a>

您可以使用 AWS CLI、 AWS 软件开发工具包和 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)"
```

有关更多信息，请参阅《命令参考》中的 [execute-gremlin-](https://docs.aws.amazon.com/cli/latest/reference/neptunedata/execute-gremlin-query.html) query。 AWS CLI 

------
#### [ 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"
```

有关更多信息，请参阅《命令参考》中的 [execute-open-cypher-query](https://docs.aws.amazon.com/cli/latest/reference/neptunedata/execute-open-cypher-query.html)。 AWS CLI 

------

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

您可以通过软件开发工具包使用 Neptune AWS Data 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 SDK](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 SDK](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)。

### 为 HT `TPS 设置 curl`
<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 证书验证](https://curl.haxx.se/docs/sslcerts.html)。

只要 `curl` 可以找到相应的证书，它即可像处理 HTTP 连接一样处理 HTTPS 连接，而无需额外的参数。本文档中的示例基于该场景。

有关该工具的更多信息，请参阅 [curl 手册页](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)