

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

# 连接到 Amazon Neptune 集群
<a name="get-started-connecting"></a>

创建 Neptune 集群后，您可以配置连接方法以访问该集群。

## 设置 `curl` 或 awscurl 以与您的 Neptune 端点进行通信
<a name="get-started-access-graph-curl"></a>

如本文档中的许多示例所示，拥有用于向 Neptune 数据库集群提交查询的命令行工具非常方便。未启用 IAM 身份验证时，[curl](https://curl.haxx.se/) 命令行工具是与 Neptune 端点通信的上佳选择。从 7.75.0 开始的版本支持 `--aws-sigv4` 选项，用于在启用 IAM 身份验证时对请求进行签名。

对于*启用了* IAM 身份验证的端点，也可以使用 [awscurl](https://github.com/okigan/awscurl)，它使用的语法与 `curl` 几乎完全相同，但支持按 IAM 身份验证的要求对请求进行签名。由于 IAM 身份验证提供了额外的安全性，因此通常最好启用它。

 有关如何使用 `curl`（或 `awscurl`）的信息，请参阅 [curl 手册页面](https://curl.haxx.se/docs/manpage.html)和电子书 *[Everything curl](https://ec.haxx.se/)*。

要使用 HTTPS 进行连接（Neptune 需要），`curl` 需要访问相应的证书。只要 `curl` 可以找到相应的证书，它即可像处理 HTTP 连接一样处理 HTTPS 连接，而无需额外的参数。对于 `awscurl` 也是如此。本文档中的示例基于该场景。

要了解如何获取此类证书以及如何将其正确格式化为 `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)。

## 连接到 Neptune 数据库集群的不同方法
<a name="get-started-connect-ways"></a>

Amazon Neptune 数据库集群*只能* 在 Amazon Virtual Private Cloud (Amazon VPC) 中创建。除非您为数据库集群启用和设置 [Neptune 公共终端节点，否则只能在该 VPC 内访问其终端节点](neptune-public-endpoints.md)。

有以下几种不同的方法可以设置对 VPC 中 Neptune 数据库集群的访问：
+ [从同一 VPC 中的 Amazon EC2 实例进行连接](get-started-connect-ec2-same-vpc.md)
+ [从另一个 VPC 中的 Amazon EC2 实例进行连接](get-started-connect-ec2-other-vpc.md)
+ [从私有网络连接](get-started-connect-private-net.md)
+ [从公有端点连接](neptune-public-endpoints.md)

# 在同一 VPC 中将 Amazon EC2 实例连接到 Amazon Neptune 集群
<a name="get-started-connect-ec2-same-vpc"></a>

连接到 Neptune 数据库的最常见方法之一是从与 Neptune 数据库集群位于同一 VPC 中的 Amazon EC2 实例进行连接。例如，EC2 实例可能正在运行与互联网连接的 Web 服务器。在这种情况下，只有 EC2 实例可以访问 Neptune 数据库集群，而互联网只能访问 EC2 实例：

![\[从同一 VPC 中的 EC2 实例访问 Neptune 集群的示意图。\]](http://docs.aws.amazon.com/zh_cn/neptune/latest/userguide/images/VPC-connection-01.png)


要启用此配置，您需要设置正确的 VPC 安全组和子网组。Web 服务器托管在公有子网中以便它可访问公共互联网，而您的 Neptune 集群实例托管在私有子网中以确保其安全。请参阅[设置您的 Amazon Neptune 数据库集群所在的 Amazon VPC](get-started-vpc.md)。

为了使 Amazon EC2 实例连接到（例如，端口 `8182`）上的 Neptune 端点，您需要设置一个安全组来执行此操作。如果您的 Amazon EC2 实例使用的是安全组（例如，名为 `ec2-sg1`），则需要创建另一个 Amazon EC2 安全组（比方说 `db-sg1`），此安全组具有用于端口 `8182` 的入站规则并使用 `ec2-sg1` 作为其源。然后，将 `db-sg1` 添加到您的 Neptune 集群以允许连接。

创建 Amazon EC2 实例后，您可以使用 SSH 登录该实例并连接到您的 Neptune 数据库集群。有关使用 SSH 连接到 EC2 实例的信息，请参阅《Amazon EC2 用户指南》**中的[连接到您的 Linux 实例](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstances.html)。

如果您使用 Linux 或 macOS 命令行连接到 EC2 实例，则可以将该**SSHAccess**项目中的 SSH 命令粘贴到堆栈的** CloudFormation 输出**部分。您在当前目录中必须具有 PEM 文件并且 PEM 文件权限必须设置为 400 (`chmod 400 keypair.pem`)。

**创建包含公有子网和私有子网的 VPC**

1. 登录 AWS 管理控制台 并打开 Amazon VPC 控制台，网址为[https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/)。

1. 在的右上角 AWS 管理控制台，选择要在其中创建 VPC 的区域。

1. 在 **VPC 控制面板**上，选择**启动 VPC 向导**。

1. 完成**创建 VPC** 页面的 **VPC 设置**区域：

   1. 在 **Resources to create**（要创建的资源）下，选择 **VPC, subnets, etc.**（VPC、子网等）。

   1. 保留默认名称标签不变，或输入您选择的名称，或者取消选中**自动生成**复选框以禁用名称标签生成。

   1. 将 IPv4 CIDR 块值保留为。`10.0.0.0/16`

   1. 将 IPv6 CIDR 块值保留为 “**无 IPv6 CIDR** 块”。

   1. 将**租赁**保留为**默认值**。

   1. 将**可用区 (AZs)** 的数量保留为 **2**。

   1. 除非需要一个或多个 NAT 网关，否则将 **NAT 网关 (\$1)** 设置为**无**。

   1. 除非您将使用 Amazon S3，否则将 **VPC 端点**设置为**无**。

   1. **启用 DNS 主机名**和**启用 DNS 解析**应已选中。

1. 选择**创建 VPC**。

# 在不同 VPC 中将 Amazon EC2 实例连接到 Amazon Neptune 集群
<a name="get-started-connect-ec2-other-vpc"></a>

Amazon Neptune 数据库集群*只能*在 Amazon Virtual Private Cloud（Amazon VPC）中创建，并且其端点在该 VPC 内进行访问，通常是从在该 VPC 中运行的 Amazon Elastic Compute Cloud（Amazon EC2）实例进行访问。或者，也可以使用公有端点对其进行访问。有关公有端点的更多信息，请参阅 [Neptune 公有端点](neptune-public-endpoints.md)。

当您的数据库集群与您用来访问它的 EC2 实例位于不同的 VPC 中时，您可以使用 [VPC 对等连接](https://docs.aws.amazon.com/vpc/latest/peering/what-is-vpc-peering.html)进行连接：

![\[从另一个 VPC 中访问 Neptune 集群的示意图。\]](http://docs.aws.amazon.com/zh_cn/neptune/latest/userguide/images/VPC-connection-02.png)


VPC 对等连接是两者之间的网络连接 VPCs ，用于私下路由它们之间的流量，因此任一 VPC 中的实例都可以像在同一个网络中一样进行通信。您可以在您的账户 VPCs 中、您账户中的 VPC 与其他账户中的 VPC 之间，或者与其他 AWS 区域的 VPC 之间创建 VPC 对等连接。 AWS AWS 

AWS 使用 VPC 的现有基础设施创建 VPC 对等连接。它既不是网关也不是 AWS Site-to-Site VPN连接，也不依赖于单独的物理硬件。它没有单点通信故障，也没有带宽瓶颈。

有关如何使用 VPC 对等连接的更多信息，请参阅 [Amazon VPC 对等连接指南](https://docs.aws.amazon.com/vpc/latest/peering/)。

# 通过专用网络连接到 Amazon Neptune 集群
<a name="get-started-connect-private-net"></a>

您可以通过两种不同的方式从私有网络访问 Neptune 数据库集群：
+ 使用 [AWS Site-to-Site VPN](https://docs.aws.amazon.com/vpn/latest/s2svpn/VPC_VPN.html) 连接。
+ 使用 [AWS Direct Connect](https://docs.aws.amazon.com/directconnect/latest/UserGuide/) 连接。

上面的链接包含有关这些连接方法以及如何设置它们的信息。 AWS Site-to-Site 连接的配置可能如下所示：

![\[从专用网络访问 Neptune 集群的示意图。\]](http://docs.aws.amazon.com/zh_cn/neptune/latest/userguide/images/VPC-connection-04.png)


# Neptune 公有端点
<a name="neptune-public-endpoints"></a>

## 概述
<a name="neptune-public-endpoints-overview"></a>

Amazon Neptune 集群通常部署在 VPC 内，并且只能从该 VPC 内对其进行访问。这需要在 VPC 内配置应用程序和开发环境，或者使用代理服务连接到该 VPC，但这会增加设置时间和成本。

公有端点允许通过互联网直接连接到 Neptune，从而简化这种体验，让您无需具备专业联网知识即可更轻松地开始使用图形数据库。

## 何时使用公有端点
<a name="neptune-public-endpoints-when-to-use"></a>

在以下场景中，可以考虑使用公有端点：
+ 您希望在开发或测试环境中快速测试 Neptune 而无需复杂的网络配置
+ 你没有专业的 AWS 网络知识
+ 应用程序的安全状况不需要私有 VPC
+ 您需要从本地开发环境连接到 Neptune

## 安全注意事项
<a name="neptune-public-endpoints-security"></a>

使用公有端点时，请记住以下安全注意事项：
+ 启用公有端点的集群需要 IAM 身份验证。
+ 对数据库的访问由它使用的安全组控制。
+ 您可以限制哪些 IP 地址能够连接到集群。
+ 您可以使用 IAM 策略来控制谁能创建或修改具有公共访问权限的集群。请参阅：[限制公共访问权限的创建](#neptune-public-endpoints-restrict-access)

## 启用公有端点
<a name="neptune-public-endpoints-enabling"></a>

默认情况下，新的 Neptune 数据库是在禁用公有端点的情况下创建的。创建或修改集群时，您必须显式启用公共访问权限。

Neptune 引擎发行版本 1.4.6.x 支持公有端点。您需要将现有集群至少升级到此版本才能使用此功能。

公有端点设置在 Neptune 实例而不是 Neptune 集群上可用。因此，Neptune 集群可能存在一些带有公有端点的实例，以及一些没有公有端点的实例。但是，我们不建议使用这种设置。有关这方面的更多信息，请参阅：[公有端点的工作原理](#neptune-public-endpoints-how-they-work)

## 先决条件
<a name="neptune-public-endpoints-prerequisites"></a>

### Neptune 集群上的 IAM 身份验证设置
<a name="neptune-public-endpoints-iam-auth"></a>

在 Neptune 实例上启用公有端点之前，请确保集群支持 IAM 身份验证。如果不支持，请使用以下命令启用该功能：

```
aws neptune modify-db-cluster \
  --region us-west-2 \
  --engine graphdb \
  --engine-version 1.4.6.x \
  --db-cluster-identifier neptune-public-endpoint \
  --enable-iam-database-authentication
```

### 网络设置
<a name="neptune-public-endpoints-network-settings"></a>

1. 确保 VPC 具有启用公有路由（公有路由在子网的路由表中包含互联网网关的条目）的子网。如果您在创建集群时未提供 `db-subnet-group-name` 参数，则系统会选择默认子网组来创建集群。

1. 确保附加到集群的安全组允许以下入站流量：允许的 IP 范围和允许的端口的入站流量。例如，如果您要允许来自所有的 TCP 流量 IPs 连接到在端口 8182 上运行的 Neptune 实例，则入站规则应具有：

   1. 类型：所有 TCP

   1. 协议：TCP

   1. 端口范围：8182

   1. CIDR 数据块：0.0.0.0/0

**注意**  
虽然您可以将 CIDR 数据块范围设置为 0.0.0.0/0，但我们建议将其缩小到客户端应用程序的特定 IP 范围，以获得更好的安全状况。

## 创建带有公有端点的新实例
<a name="neptune-public-endpoints-creating-instance"></a>

您可以使用 AWS 管理控制台、CL AWS I 或 AWS SDK 创建带有公共终端节点的新 Neptune 实例。

使用 C AWS LI：

```
aws neptune create-db-instance \
  --region us-west-2 \
  --engine graphdb \
  --engine-version 1.4.6.x \
  --db-cluster-identifier neptune-public-endpoint \
  --publicly-accessible
```

## 修改现有实例以进行公共访问
<a name="neptune-public-endpoints-modifying-instance"></a>

要修改现有 Neptune 实例以启用公共访问，请执行以下操作：

```
aws neptune modify-db-instance \
  --region us-west-2 \
  --engine graphdb \
  --engine-version 1.4.6.x \
  --db-instance-identifier neptune-public-endpoint \
  --publicly-accessible
```

**注意**  
公共访问是在实例级别启用的，而不是在集群级别启用的。为确保集群始终可通过公有端点进行访问，集群中的所有实例都必须启用公共访问。

## 使用公有端点
<a name="neptune-public-endpoints-using"></a>

要检查您的数据库是否可以访问，请使用 AWS CLI `NeptuneData` API 检查状态：

```
aws neptunedata get-engine-status \
  --endpoint-url https://my-cluster-name.cluster-abcdefgh1234.us-east-1.neptune.amazonaws.com:8182
```

如果数据库可访问，则响应如下所示：

```
{
    "status": "healthy",
    "startTime": "Sun Aug 10 06:54:15 UTC 2025",
    "dbEngineVersion": "1.4.6.0.R1",
    "role": "writer",
    "dfeQueryEngine": "viaQueryHint",
    "gremlin": {
        "version": "tinkerpop-3.7.1"
    },
    "sparql": {
        "version": "sparql-1.1"
    },
    "opencypher": {
        "version": "Neptune-9.0.20190305-1.0"
    },
    "labMode": {
        "ObjectIndex": "disabled",
        "ReadWriteConflictDetection": "enabled"
    },
    "features": {
        "SlowQueryLogs": "disabled",
        "InlineServerGeneratedEdgeId": "disabled",
        "ResultCache": {
            "status": "disabled"
        },
        "IAMAuthentication": "disabled",
        "Streams": "disabled",
        "AuditLog": "disabled"
    },
    "settings": {
        "StrictTimeoutValidation": "true",
        "clusterQueryTimeoutInMs": "120000",
        "SlowQueryLogsThreshold": "5000"
    }
}
```

## 如何查询数据库的示例
<a name="neptune-public-endpoints-examples"></a>

### AWS CLI
<a name="neptune-public-endpoints-aws-cli"></a>

```
aws neptunedata execute-open-cypher-query \
--open-cypher-query "MATCH (n) RETURN n LIMIT 10" \
--endpoint-url https://my-cluster-name.cluster-abcdefgh1234.us-east-1.neptune.amazonaws.com:8182
```

### Python
<a name="neptune-public-endpoints-python"></a>

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

# Configuration - Replace with your actual Neptune cluster details
cluster_endpoint = "my-cluster-name.cluster-abcdefgh1234.my-region.neptune.amazonaws.com"
port = 8182
region = "my-region"

# Configure Neptune client
# This disables retries and sets the client timeout to infinite 
#     (relying on Neptune's query timeout)
endpoint_url = f"https://{cluster_endpoint}:{port}"
config = Config(
    region_name=region,
    retries={'max_attempts': 1},
    read_timeout=None
)

client = boto3.client("neptunedata", config=config, endpoint_url=endpoint_url)

cypher_query = "MATCH (n) RETURN n LIMIT 5"
try:
    response = client.execute_open_cypher_query(openCypherQuery=cypher_query)
    print("openCypher Results:")
    for item in response.get('results', []):
        print(f"  {item}")
except Exception as e:
    print(f"openCypher query failed: {e}")
```

### JavaScript
<a name="neptune-public-endpoints-javascript"></a>

```
import {
    NeptunedataClient,
    GetPropertygraphSummaryCommand
} from "@aws-sdk/client-neptunedata";
import { inspect } from "util";
import { NodeHttpHandler } from "@smithy/node-http-handler";

/**
 * Main execution function
 */
async function main() {
    // Configuration - Replace with your actual Neptune cluster details
    const clusterEndpoint = 'my-cluster-name.cluster-abcdefgh1234.my-region.neptune.amazonaws.com';
    const port = 8182;
    const region = 'my-region';

    // Configure Neptune client
    // This disables retries and sets the client timeout to infinite 
    //     (relying on Neptune's query timeout)
    const endpoint = `https://${clusterEndpoint}:${port}`;
    const clientConfig = {
        endpoint: endpoint,
        sslEnabled: true,
        region: region,
        maxAttempts: 1,  // do not retry
        requestHandler: new NodeHttpHandler({
            requestTimeout: 0  // no client timeout
        })
    };

    const client = new NeptunedataClient(clientConfig);
    try {
        try {
            const command = new GetPropertygraphSummaryCommand({ mode: "basic" });
            const response = await client.send(command);
            console.log("Graph Summary:", inspect(response.payload, { depth: null }));
        } catch (error) {
            console.log("Property graph summary failed:", error.message);
        }    
    } catch (error) {
        console.error("Error in main execution:", error);
    }
}

// Run the main function
main().catch(console.error);
```

### Go
<a name="neptune-public-endpoints-go"></a>

```
package main
import (
    "context"
    "fmt"
    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/neptunedata"
    "os"
    "encoding/json"
    "net/http"
)

func main() {    
    // Configuration - Replace with your actual Neptune cluster details
    clusterEndpoint := "my-cluster-name.cluster-abcdefgh1234.my-region.neptune.amazonaws.com"
    port := 8182
    region := "my-region"
    
    // Configure Neptune client
    // Configure HTTP client with no timeout
    //    (relying on Neptune's query timeout)
    endpoint := fmt.Sprintf("https://%s:%d", clusterEndpoint, port)
    // Load AWS SDK configuration
    sdkConfig, _ := config.LoadDefaultConfig(
        context.TODO(),
        config.WithRegion(region),
        config.WithHTTPClient(&http.Client{Timeout: 0}),
    )
    
    // Create Neptune client with custom endpoint
    client := neptunedata.NewFromConfig(sdkConfig, func(o *neptunedata.Options) {
        o.BaseEndpoint = aws.String(endpoint)
        o.Retryer = aws.NopRetryer{} // Do not retry calls if they fail
    })

    gremlinQuery := "g.addV('person').property('name','charlie').property(id,'charlie-1')"
    serializer := "application/vnd.gremlin-v1.0+json;types=false"
    
    gremlinInput := &neptunedata.ExecuteGremlinQueryInput{
        GremlinQuery: &gremlinQuery,
        Serializer:   &serializer,
    }
    gremlinResult, err := client.ExecuteGremlinQuery(context.TODO(), gremlinInput)
    if err != nil {
        fmt.Printf("Gremlin query failed: %v\n", err)
    } else {
        var resultMap map[string]interface{}
        err = gremlinResult.Result.UnmarshalSmithyDocument(&resultMap)
        if err != nil {
            fmt.Printf("Error unmarshaling Gremlin result: %v\n", err)
        } else {
            resultJSON, _ := json.MarshalIndent(resultMap, "", "  ")
            fmt.Printf("Gremlin Result: %s\n", string(resultJSON))
        }
    }
}
```

## 公有端点的工作原理
<a name="neptune-public-endpoints-how-they-work"></a>

当 Neptune 实例可公开访问时：
+ 它的 DNS 端点会从数据库集群的 VPC 内解析为私有 IP 地址，
+ 从数据库集群的 VPC 外解析为公有 IP 地址。
+ 访问由分配给集群的安全组控制。
+ 只有可公开访问的实例才能通过互联网访问。

### 读取器端点行为
<a name="neptune-public-endpoints-reader-behavior"></a>
+ 如果所有读取器实例均可公开访问，则读取器端点将始终通过公共互联网进行解析。
+ 如果只有部分读取器实例可公开访问，则只有当读取器端点选择可公开访问的实例来提供读取查询时，它才会进行公开解析。

### 集群端点行为
<a name="neptune-public-endpoints-cluster-behavior"></a>
+ 数据库集群端点始终解析为写入器的实例端点。
+ 如果在写入器实例上启用公有端点，则集群端点将可公开访问，否则将不可公开访问。

### 集群失效转移后的行为
<a name="neptune-public-endpoints-failover-behavior"></a>
+ Neptune 集群可以将实例设置为不同的公共可访问设置。
+ 如果集群具有公共写入器和非公共读取器，则在集群失效转移后，新的写入器（之前的读取器）将变为非公共写入器，而新的读取器（之前的写入器）将变为公共读取器。

## 网络配置要求
<a name="neptune-public-endpoints-network-requirements"></a>

要使公有端点正常工作，需满足以下要求：

1. Neptune 实例必须位于 VPC 内的公有子网中。

1. 与这些子网关联的路由表必须具有通往互联网网关的路由 0.0.0.0/0。

1. 安全组必须允许从您想要授予访问权限的公有 IP 地址或 CIDR 范围进行访问。

## 限制公共访问权限的创建
<a name="neptune-public-endpoints-restrict-access"></a>

您可以使用 IAM 策略来限制谁能创建或修改具有公共访问权限的 Neptune 集群。以下示例策略拒绝创建具有公共访问权限的 Neptune 实例：

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Deny",
      "Action": [
        "rds:CreateDBInstance",
        "rds:ModifyDBInstance",
        "rds:RestoreDBInstanceFromDBSnapshot",
        "rds:RestoreDBInstanceToPointInTime"
      ],
      "Resource": "*",
      "Condition": {
        "Bool": {
          "rds:PubliclyAccessible": true
        }
      }
    }
  ]
}
```

------

有关 `rds:PublicAccessEnabled` IAM 条件键的更多信息：[Amazon RDS Service Authorization Reference](https://docs.aws.amazon.com//service-authorization/latest/reference/list_amazonrds.html#amazonrds-rds_PubliclyAccessible)

## CloudFormation 支持
<a name="neptune-public-endpoints-cloudformation"></a>

通过在模板中指定`PubliclyAccessible`参数 CloudFormation ，您可以使用启动启用公共终端节点的 Neptune CloudFormation 集群。

## 与 Neptune 功能的兼容性
<a name="neptune-public-endpoints-compatibility"></a>

启用公有端点的集群支持仅 VPC 集群支持的所有 Neptune 功能，包括：
+ Neptune Workbench
+ 全文搜索集成
+ Neptune 流
+ 自定义端点
+ Neptune 无服务器
+ 图形浏览器

## 定价
<a name="neptune-public-endpoints-pricing"></a>

除了标准的 Neptune 定价之外，使用公有端点不会产生额外费用。但是，通过公有 IP 从本地环境连接到 Neptune 可能会增加数据传输费用。