

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

# 将 Amazon Neptune 与图形笔记本结合使用
<a name="graph-notebooks"></a>

[要使用海王星图表，你可以使用海王星图形笔记本，也可以使用模板创建新的海王星数据库。 Amazon CloudFormation](get-started-cfn-create.md)

无论您是图形新手并想学习和体验，还是经验丰富并想要完善查询，Neptune [Workbench](#graph-notebooks-workbench) 都提供了一个交互式开发环境（IDE），可以在您构建图形应用程序时提高工作效率。Workbench 提供了一个用户友好的界面，用于与 Neptune 数据库进行交互、编写查询和可视化数据。

通过使用 Amazon CloudFormation 模板来设置 Neptune 数据库，使用工作台开发图形应用程序，您可以快速高效地开始使用 Neptune，而无需其他工具。这使您可以专注于构建应用程序，而不是设置底层基础设施。

**注意**  
 通过 Amazon A SageMaker I 管理的 Neptune 笔记本电脑目前不在亚太地区（马来西亚）（ap-southeast-5）地区上市。不过，您仍然可以通过其他非托管选项部署 Neptune 笔记本。有关手动部署笔记本的信息，请参阅[手动设置 Neptune 笔记本](#graph-notebook-manual-setup)。

Neptune 在开源 Neptune [图形[JupyterLab](https://jupyterlab.readthedocs.io/en/stable/index.html)笔记本项目和 Neptune 工作台中提供 [Jupyter](https://jupyter-notebook.readthedocs.io/en/stable/) GitHub 和笔记本](https://github.com/aws/graph-notebook)。这些笔记本在交互式编码环境中提供示例应用程序教程和代码片段，您可以在此环境中学习图形技术和 Neptune。您可以使用它们在后端通过不同的查询语言、不同的数据集甚至不同的数据库来逐步设置、配置、填充和查询图形。

可以通过多种不同方式托管这些笔记本：
+  [Nep [tune 工作台](#graph-notebooks-workbench)允许您在托管在 Amazon A SageMaker I 中的完全托管的环境中运行 Jupyter 笔记本，并自动为您加载最新版本的 Neptune 图形笔记本项目。](https://github.com/aws/graph-notebook)创建新的 Neptune 数据库时，可以轻松地在 [Neptune 控制台](https://console.amazonaws.cn/neptune)中设置工作台。
**注意**  
 创建 Neptune 笔记本实例时，您可以选择两个网络访问选项：通过 Amazon A SageMaker I 直接访问（默认）和通过 VPC 进行访问。无论选择哪个选项，笔记本都需要访问互联网才能获取安装 Neptune Workbench 所需的软件包依赖项。无法访问互联网将导致 Neptune 笔记本实例创建失败。
+ 您也可以[在本地安装 Jupyter](#graph-notebooks-local)。这使您可以从便携式电脑上运行笔记本，而便携式电脑连接到 Neptune 或其中一个开源图形数据库的本地实例。在后一种情况下，您可以随心所欲地尝试图形技术，而不必花一分钱。然后，准备就绪后，您可以顺利迁移到 Neptune 提供的托管式生产环境。

## 使用 Neptune Workbench 托管 Neptune 笔记本
<a name="graph-notebooks-workbench"></a>

Neptune 提供的 `T3` 和 `T4g` 实例类型可供您入门，每小时不到 0.10 美元。工作台资源是通过 Amazon A SageMaker I 向你收取的，与 Neptune 账单分开收费。请参阅 [Neptune 定价页面](https://www.amazonaws.cn/neptune/pricing/)。Jupyter 和在 Neptune 工作台上创建的 JupyterLab 笔记本都使用 Amazon Linux 2 和 4 环境。 JupyterLab 有关 JupyterLab 笔记本支持的更多信息，请参阅 [Amazon A SageMaker I 文档](https://docs.amazonaws.cn/sagemaker/latest/dg/nbi-jl.html)。

您可以通过以下两种方式使用 Neptune 工作台创建 Jupyter 或 JupyterLab 笔记本： Amazon Web Services 管理控制台 
+ 创建新的 Neptune 数据库集群时，使用**笔记本配置**菜单。为此，请按照[使用启动 Neptune 数据库集群 Amazon Web Services 管理控制台](manage-console-launch-console.md)中概述的步骤操作。
+ 创建数据库集群后，使用左侧导航窗格中的**笔记本**菜单。为此，请执行以下步骤。

****使用 “笔记本” 菜单创建 Jupyter 或 JupyterLab 笔记本****

1. [登录 Amazon 管理控制台，然后在家中打开 Amazon Neptune https://console.aws.amazon.com/neptune/ 主机。](https://console.amazonaws.cn/neptune/home)

1. 在左侧的导航窗格中，选择 **Notebooks (笔记本)**。

1. 选择**创建笔记本**。

1. 选择**数据库**作为 Neptune 服务。

1. 在**集群**列表中，选择您的 Neptune 数据库集群。如果您还没有数据库集群，请选择 **Create cluster (创建集群)** 以创建一个。

1. 选择**笔记本实例类型**。

1. 为您的笔记本提供一个名称以及可选的描述。

1. 除非您已经为笔记本创建了 Amazon Identity and Access Management (IAM) 角色，否则请选择**创建 IAM 角色**并输入 IAM 角色名称。
**注意**  
如果您选择重用为之前的笔记本创建的 IAM 角色，则该角色策略必须包含访问您正在使用的 Neptune 数据库集群的正确权限。您可以通过检查资源 ARN 中 `neptune-db:*` 操作下的组件是否与该集群匹配来验证这一点。当您尝试运行笔记本魔术命令时，权限配置不正确会导致连接错误。

1. 选择**创建笔记本**。在一切准备就绪之前，创建过程可能需要 5 到 10 分钟。

1. **创建笔记本后，将其选中，然后选择 “**打开 Jupyter” 或 “打开**”。 JupyterLab**

控制台可以为您的笔记本创建 Amazon Identity and Access Management (IAM) 角色，您也可以自己创建一个角色。此角色的策略应包括以下内容：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowS3GetObjectS3ListBucket",
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::aws-neptune-notebook",
        "arn:aws:s3:::aws-neptune-notebook/*",
        "arn:aws:s3:::aws-neptune-notebook-{{us-east-1}}",
        "arn:aws:s3:::aws-neptune-notebook-{{us-east-1}}/*"
      ]
    },
    {
      "Sid": "AllowAccessNeptuneDB",
      "Effect": "Allow",
      "Action": "neptune-db:*",
      "Resource": [
        "arn:aws:neptune-db:{{us-east-1}}:{{111122223333}}:{{cluster-resource-id}}/*"
      ]
    }
  ]
}
```

------

请注意，上述策略中的第二条语句列出了一个或多个 Neptune [群集](iam-data-resources.md)资源。 IDs

此外，角色应建立以下信任关系：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "sagemaker.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
```

------

再说一遍，一切准备就绪可能需要 5 至 10 分钟。

您可以将新笔记本配置为与 Neptune ML 结合使用，如[手动为 Neptune ML 配置 Neptune 笔记本](ml-manual-setup-notebooks.md)中所述。

### 使用 Python 将通用 SageMaker AI 笔记本连接到 Neptune
<a name="graph-notebooks-workbench-python"></a>

如果你已经安装了海王星的魔法，那么将笔记本连接到海王星很容易，但是即使你没有使用海王星 SageMaker 笔记本电脑，也可以使用Python将人工智能笔记本连接到海王星。

**在 SageMaker AI 笔记本单元中连接到 Neptune 需要采取的步骤**

1. 安装 Gremlin Python 客户端：

   ```
   !pip install gremlinpython
   ```

   Neptune 笔记本会为你安装 Gremlin Python 客户端，因此，只有当你使用的是普通的 A SageMaker I 笔记本时，才需要执行此步骤。

1. 编写如下代码来连接和发出 Gremlin 查询：

   ```
   from gremlin_python import statics
   from gremlin_python.structure.graph import Graph
   from gremlin_python.process.graph_traversal import __
   from gremlin_python.process.strategies import *
   from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
   from gremlin_python.driver.aiohttp.transport import AiohttpTransport
   from gremlin_python.process.traversal import *
   import os
   
   port = 8182
   server = '{{(your server endpoint)}}'
   
   endpoint = f'wss://{server}:{port}/gremlin'
   
   graph=Graph()
   
   connection = DriverRemoteConnection(endpoint,'g',
                    transport_factory=lambda:AiohttpTransport(call_from_event_loop=True))
   
   g = graph.traversal().withRemote(connection)
   
   results = (g.V().hasLabel('airport')
                   .sample(10)
                   .order()
                   .by('code')
                   .local(__.values('code','city').fold())
                   .toList())
   
   # Print the results in a tabular form with a row index
   for i,c in enumerate(results,1):
       print("%3d %4s %s" % (i,c[0],c[1]))
   
   connection.close()
   ```

**注意**  
如果您碰巧使用的是早于 3.5.0 的 Gremlin Python 客户端，那么这行：  

```
connection = DriverRemoteConnection(endpoint,'g',
                 transport_factory=lambda:AiohttpTransport(call_from_event_loop=True))
```
只会是：  

```
connection = DriverRemoteConnection(endpoint,'g')
```

## 通过以下方式以 Amazon Neptune Workbench 的形式推出图形笔记本电脑 Amazon CloudFormation
<a name="graph-notebooks-cloudformation"></a>

您可以使用部署 Amazon Neptune 工作台笔记本电脑。 Amazon CloudFormation该 Amazon CloudFormation 模板将 Neptune Workbench 笔记本部署为资源，并包括基本的 “入门” 笔记本。工作台允许你使用亚马逊托管的 Jupyter 笔记本来处理你的 Amazon Neptune 数据库集群。 SageMaker您需要通过亚马逊 SageMaker为工作台资源付费，这与 Neptune 账单是分开的。

 Amazon CloudFormation 模板和详细说明（包括 IAM 角色要求和权限）可在 [graph-notebook GitHub ](https://github.com/aws/graph-notebook/tree/main/additional-databases/sagemaker/neptune-notebook-cloudformation) 存储库中找到。

## 在 Neptune 笔记本电脑上启用 CloudWatch 日志
<a name="graph-notebooks-cw-logs"></a>

CloudWatch Neptune 笔记本现在默认启用日志。如果您的旧笔记本无法生成 CloudWatch 日志，请按照以下步骤手动启用日志：

1. 登录 Amazon Web Services 管理控制台 并打开 [SageMaker AI 控制台](https://console.amazonaws.cn/sagemaker/home)。

1. 在左侧的导航窗格上，选择**笔记本**，然后选择**笔记本实例**。查找要为其启用日志的 Neptune 笔记本的名称。

1. 选择该笔记本实例的名称，进入详细信息页面。

1. 如果笔记本实例正在运行，请选择笔记本详细信息页面右上角的**停止**按钮。

1. 在**权限和加密**下，有一个对应于 **IAM 角色 ARN** 的字段。选择此字段中的链接可转到运行此笔记本实例的 IAM 角色。

1. 创建以下策略：

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

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Action": [
           "logs:CreateLogDelivery",
           "logs:CreateLogGroup",
           "logs:CreateLogStream",
           "logs:DeleteLogDelivery",
           "logs:Describe*",
           "logs:GetLogDelivery",
           "logs:GetLogEvents",
           "logs:ListLogDeliveries",
           "logs:PutLogEvents",
           "logs:PutResourcePolicy",
           "logs:UpdateLogDelivery"
         ],
         "Resource": "*"
       }
     ]
   }
   ```

------

1. 保存此新策略并将其附加到在步骤 4 中找到的 IAM 角色。

1. 单击 SageMaker AI 笔记本实例详细信息页面右上角的 “**启动**”。

1. 日志开始流动后，您应该会在详细信息页面**笔记本实例设置**部分的左下角附近标有**生命周期配置**的字段下方看到**查看日志**链接。

如果笔记本无法启动， SageMaker AI 控制台的笔记本详细信息页面将显示一条消息，说明笔记本实例花了 5 分钟才启动。 CloudWatch 可以在以下名称下找到与此问题相关的日志：

```
{{(your-notebook-name)}}/LifecycleConfigOnStart
```

## 在本地计算机上设置图形笔记本
<a name="graph-notebooks-local"></a>

图形笔记本项目包含有关在本地计算机上设置 Neptune 笔记本的说明：
+ [先决条件](https://github.com/aws/graph-notebook/#prerequisites)
+ [Jupyter 和安装 JupyterLab ](https://github.com/aws/graph-notebook/#installation)
+ [连接到图形数据库](https://github.com/aws/graph-notebook/#connecting-to-a-graph-database)

您可以将本地笔记本连接到 Neptune 数据库集群，也可以连接到开源图形数据库的本地或远程实例。

### 将 Neptune 笔记本与 Neptune 集群结合使用
<a name="graph-notebooks-local-with-neptune"></a>

如果您在后端连接到 Neptune 集群，则可能需要在 Ama SageMaker zon AI 中运行笔记本电脑。[从 SageMaker 人工智能连接到 Neptune 比从本地安装笔记本电脑更方便，而且它可以让你更轻松地使用 Neptune ML。](machine-learning.md)

有关如何在 SageMaker AI 中设置笔记本的说明，请参阅使用 A [mazon 启动图形笔记本](https://github.com/aws/graph-notebook/blob/main/additional-databases/sagemaker/README.md)。 SageMaker

有关如何设置和配置 Neptune 本身的说明，请参阅[设置 Amazon Neptune](neptune-setup.md)。

您也可以将本地安装的 Neptune 笔记本连接到 Neptune 数据库集群。这可能稍微复杂一些，因为 Amazon Neptune 数据库集群只能在设计上与外界隔离的 Amazon Virtual Private Cloud (VPC) 中创建。有多种方法可以从 VPC 外部连接到 VPC。一种是使用负载均衡器。另一种方法是使用 VPC 对等连接（请参阅 [Amazon Virtual Private Cloud 对等连接指南](https://docs.amazonaws.cn/vpc/latest/peering/what-is-vpc-peering.html)）。

但是，对于大多数人来说，最便捷的方法是进行连接以在 VPC 中设置 Amazon EC2 代理服务器，然后使用 [SSH 隧道](https://www.ssh.com/ssh/tunneling/)（也称为端口转发）来连接到它。你可以在[图形](https://github.com/aws/graph-notebook/) GitHub 笔记本项目的`additional-databases/neptune`文件夹中[将绘图笔记本本地连接到 Amazon Neptune](https://github.com/aws/graph-notebook/tree/main/additional-databases/neptune) 中找到有关如何设置的说明。

### 使用带有开源图形数据库的 Neptune 笔记本
<a name="graph-notebooks-open-source_databases"></a>

要免费开始使用图形技术，还可以在后端使用带有各种开源数据库的 Neptune 笔记本。例如 TinkerPop [Gremlin 服务器](https://tinkerpop.apache.org/docs/current/reference/#gremlin-server)和 [Blaz](https://blazegraph.com/) egraph 数据库。

要使用 Gremlin 服务器作为后端数据库，请按照以下步骤操作：
+ [将图形笔记本连接到 Gremlin 服务器](https://github.com/aws/graph-notebook/blob/main/additional-databases/gremlin-server/README.md#connecting-graph-notebook-to-a-gremlin-server)文件夹。 GitHub 
+ [图形笔记本 Grem](https://github.com/aws/graph-notebook/#gremlin-server) lin 配置文件夹。 GitHub 

要使用 [Blazegraph](https://github.com/blazegraph/database) 的本地实例作为后端数据库，请按照以下步骤操作：
+ 查看 [Blazegraph 快速入门说明](https://github.com/blazegraph/database/wiki/Quick_Start)，了解运行 Blazegraph 实例所需的基本设置和配置。
+ 访问[图形笔记本 Blazegraph 配置](https://github.com/aws/graph-notebook/#blazegraph) GitHub 文件夹，其中包含设置本地 Blazegraph 实例所需的文件和说明。
+ 在 GitHub 存储库中，导航到 “blazegraph” 目录，然后按照提供的说明设置本地 Blazegraph 实例。这包括下载 Blazegraph 软件、配置必要文件和启动 Blazegraph 服务器的步骤。

运行本地 Blazegraph 实例后，即可将其与应用程序集成，以用作基于图形的数据和查询的后端数据库。请参阅图形笔记本存储库中提供的文档和示例代码，了解如何将应用程序连接到 Blazegraph 实例。

## 将 Neptune 笔记本电脑迁移到 4.x JupyterLab
<a name="migrating-neptune-notebooks-jupyterlab"></a>

本节概述了将您的 Neptune 笔记本电脑迁移到 JupyterLab 4.x 及更高版本的 Amazon Linux 环境的各种方法。有关 JupyterLab 版本控制的详细信息，请参阅 [Amazon A SageMaker I JupyterLab 版本控制](https://docs.amazonaws.cn/sagemaker/latest/dg/nbi-jl.html)。

### 迁移方法
<a name="migration-approaches"></a>

#### 全新安装
<a name="fresh-installation"></a>

如果您不需要保留现有工作空间文件或配置，您可以：

1. 创建一个运行 JupyterLab 4.x 的新笔记本实例（notebook-al2-v3）

1. 确认新设置是否按照预期运行

1. 停止并删除旧笔记本实例

#### 文件传输功能迁移
<a name="file-transfer-migration"></a>

此方法使用您的本地系统或 Amazon S3 作为中间存储。

**适用于**  

+ [通过 Amazon A SageMaker I 联网配置直接访问互联网](https://docs.amazonaws.cn//sagemaker/latest/dg/appendix-notebook-and-internet-access.html#appendix-notebook-and-internet-access-default)。
+ 需要迁移的数据量适中
+ 要保留特定文件而不是整个工作空间配置。

##### 方法 1：使用 JupyterLab 用户界面
<a name="method-jupyterlab-ui"></a>

**适用于**  

+ 文件量较少
+ 有选择地迁移文件
+ 更喜欢简单的 drag-and-drop操作

**Steps**  


1. 从源 JupyterLab 实例下载文件：
   + 在中导航并选择要迁移到新实例的文件 JupyterLab
   + 右键单击并选择**下载**

1. 上传到新 JupyterLab 实例：
   + 使用中的上传按钮 JupyterLab ，选择要复制到新实例的所有文件
   + （或者）直接拖放文件

##### 方法 2：使用 Amazon S3
<a name="method-s3"></a>

**适用于**  

+ 文件量较大
+ 保留文件夹结构
+ 批量迁移

**先决条件**  
确保与笔记本关联的角色具有上传和访问 Amazon S3 存储桶所需的相应权限：

```
{
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:GetObject", "s3:ListBucket"],
"Resource": ["arn:aws:s3:::your-bucket-name/*", "arn:aws:s3:::your-bucket-name"]
}
```

**注意**  
 [Amazon CLI](https://awscli.amazonaws.com/v2/documentation/api/latest/index.html) 应预先安装在 SageMaker AI 笔记本电脑上。

**Steps**  


1. 在带`!`前缀的笔记本单元格中打开终端 JupyterLab 或键入终端命令。

1. 使用 Amazon S3 [cp 或 Amazon](https://docs.amazonaws.cn//cli/latest/reference/s3/cp.html) S3 [同步 CLI 命令将文件从旧 JupyterLab 实例复制到](https://docs.amazonaws.cn//cli/latest/reference/s3/sync.html) S3：

   ```
   # using Amazon s3 cp
   aws s3 cp /home/ec2-user/SageMaker/your-folder s3://your-bucket/backup/ --recursive
   
   # (OR) using Amazon s3 sync
   aws s3 sync /home/ec2-user/SageMaker/your-folder s3://your-bucket/backup/
   ```

1. 将文件从 S3 复制到您的新 JupyterLab 实例：

   ```
   # using Amazon s3 cp
   aws s3 cp s3://your-bucket/backup/ /home/ec2-user/SageMaker/your-folder --recursive
   
   # (OR) using Amazon s3 sync
   aws s3 sync s3://your-bucket/backup/ /home/ec2-user/SageMaker/your-folder
   ```

**注意**  
使用 `sync` 来维护文件夹结构和增量更新并使用 `cp` 进行一次性传输。

#### Amazon EFS 迁移
<a name="amazon-efs-migration"></a>

**适用于**  

+ [仅限 VPC](https://docs.amazonaws.cn//sagemaker/latest/dg/appendix-notebook-and-internet-access.html#appendix-notebook-and-internet-access-default-vpc) 联网配置
+ 数据量较大

**Steps**  
关注将 [EFS 文件系统挂载到 Amazon SageMaker AI 笔记本](https://www.amazonaws.cn/blogs//machine-learning/mount-an-efs-file-system-to-an-amazon-sagemaker-notebook-with-lifecycle-configurations/)博客，在笔记本实例中使用 Amazon EFS 文件系统。

此外，还有一些步骤专门适用于将 Neptune 笔记本迁移到新环境：

1. [在控制台中创建 Neptune 笔记本](https://docs.amazonaws.cn//neptune/latest/userguide/graph-notebooks.html#graph-notebooks-workbench)时，请在生命周期配置下选择**创建新的生命周期配置**。

1. 在模板生命周期配置中，在 install.sh 脚本后面附加您的 Amazon EFS 挂载命令（`sudo mount -t nfs ...`）

这样可以确保您的笔记本实例每次启动或重新启动时都会自动挂载您的 Amazon EFS 文件系统。要解决挂载问题，请参阅 [Amazon EFS 问题排查文档](https://docs.amazonaws.cn/efs/latest/ug/troubleshooting.html)。

**优点**  

+ 跨实例无缝访问文件
+ 无需中间传输直接访问文件
+ 高效处理大型数据集

#### Amazon EBS 卷迁移
<a name="ebs-volume-migration"></a>

**最适合需要保留以下内容的情况**  

+ 完整的工作空间配置
+ 隐藏文件
+ 系统设置
+ 保留完整的工作空间配置、隐藏文件和系统设置

按照[适用于 Amazon EBS 卷的 A Amazon SageMaker I 迁移指南](https://docs.amazonaws.cn/sagemaker/latest/dg/nbi-upgrade.html)，从与笔记本实例关联的 Amazon EBS 卷中传输文件。

此外，还有一些步骤专门适用于将 Neptune 笔记本迁移到新环境。

## Neptune 特定的先决条件
<a name="graph-notebooks-migrating-to-JL3-prereqs"></a>

在源 Neptune 笔记本的 IAM 角色中，添加以下所有权限：

```
{
  "Effect": "Allow",
  "Action": [
    "s3:GetObject",
    "s3:ListBucket",
    "s3:CreateBucket",
    "s3:PutObject"
  ],
  "Resource": [
    "arn:aws:s3:::{{(your ebs backup bucket name)}}",
    "arn:aws:s3:::{{(your ebs backup bucket name)}}/*"
  ]
},
{
  "Effect": "Allow",
  "Action": [
    "sagemaker:ListTags"
  ],
  "Resource": [
    "*"
  ]
}
```

请务必为要用于备份的 S3 桶指定正确的 ARN。

## 特定于 Neptune 的生命周期配置
<a name="graph-notebooks-migrating-to-JL3-lifecycle"></a>

按博客文章中所述创建第二个生命周期配置脚本以恢复备份（从 `on-create.sh`）时，生命周期名称必须遵循 `aws-neptune-*` 格式，例如 `aws-neptune-sync-from-s3`。这样可以确保在 Neptune 控制台中创建笔记本时可以选择 LCC。

## 从快照到新实例的 Neptune 特定同步
<a name="graph-notebooks-migrating-to-JL3-synchronizing"></a>

在博客文章中描述的从快照同步到新实例的步骤中，以下是特定于 Neptune 的更改：
+ 在步骤 4 中，选择 **notebook-al2-v3**。
+ 在步骤 5 中，重用源 Neptune 笔记本中的 IAM 角色。
+ 在步骤 7 和 8 之间：
  + 在**笔记本实例设置**中，设置使用 `aws-neptune-*` 格式的名称。
  + 打开**网络**设置手风琴式折叠组件，选择与源笔记本中相同的 VPC、子网和安全组。

## 创建新笔记本后 Neptune 特定的步骤
<a name="graph-notebooks-migrating-to-JL3-afterwards"></a>

1. 选择笔记本的**打开 Jupyter** 按钮。一旦 `SYNC_COMPLETE` 文件出现在主目录中，请继续执行下一步。

1. 前往 A SageMaker I 控制台中的笔记本实例页面。

1. 停止运行 Notebook。

1. 选择**编辑**。

1. 在笔记本实例设置中，通过选择源 Neptune 笔记本的原始生命周期来编辑**生命周期配置**字段。请注意，这不是 EBS 备份生命周期。

1. 选择**更新笔记本设置**。

1. 再次启动笔记本。

通过对博客文章中概述的步骤进行此处描述的修改，您的图形笔记本现在应该迁移到使用 Amazon Linux 2 和 JupyterLab 4 环境的新 Neptune 笔记本实例上。**它们将显示在的 Neptune 页面上以供访问和管理，现在 Amazon Web Services 管理控制台，你可以选择 “**打开 Jupyter” 或 “打开**”，从上次停下来的地方继续工作。 JupyterLab**

## 在亚马 SageMaker 逊 AI 实例中创建 Neptune 笔记本
<a name="create-notebook-next"></a>

****

1. 打开 Amazon A SageMaker I 控制台，网址为[https://console.aws.amazon.com/sagemaker/](https://console.amazonaws.cn/sagemaker/)。

1. 在导航窗格中，展开**笔记本**，然后选择**创建笔记本实例**。

1. 选择**创建笔记本实例**。

1. 在**笔记本实例设置**的**笔记本实例名称**下，为笔记本指定一个前缀为 `aws-neptune-`（例如 `aws-neptune-my-test-notebook`）的名称。

1. 在**平台标识符**下，选择**亚马逊 Linux 2、 JupyterLab 4**。

1. 选择**其它配置**。

1. 在**生命周期配置**下，选择**创建新生命周期配置**。

1. 在**配置**的**名称**下，输入步骤 4 中的笔记本实例名称。

1. 在**脚本**的**启动笔记本**下，将现有脚本替换为：

   ```
   #!/bin/bash
   
   sudo -u ec2-user -i <<'EOF'
   
   echo "export GRAPH_NOTEBOOK_AUTH_MODE=IAM" >> ~/.bashrc
   echo "export GRAPH_NOTEBOOK_SSL=True" >> ~/.bashrc
   echo "export GRAPH_NOTEBOOK_SERVICE={{neptune-db for Neptune, or neptune-graph for Neptune Analytics}}" >> ~/.bashrc
   echo "export GRAPH_NOTEBOOK_HOST={{(Neptune Analytics graph endpoint, public or private)}}" >> ~/.bashrc
   echo "export GRAPH_NOTEBOOK_PORT=8182" >> ~/.bashrc
   echo "export NEPTUNE_LOAD_FROM_S3_ROLE_ARN=" >> ~/.bashrc
   echo "export AWS_REGION={{(Amazon region)}}" >> ~/.bashrc
   
   aws s3 cp s3://aws-neptune-notebook-{{(AWS region)}}/graph_notebook.tar.gz /tmp/graph_notebook.tar.gz
   rm -rf /tmp/graph_notebook
   tar -zxvf /tmp/graph_notebook.tar.gz -C /tmp
   /tmp/graph_notebook/install_jl4x.sh
   
   EOF
   ```

1. 选择 **Create configuration (创建配置)**。

1. 在**权限和加密**的 **IAM 角色**下，选择您在前面创建的角色。

1. 在**网络**中，如果您使用的是私有图形端点：

   1. 在 **VPC** 下，选择 Neptune Analytics 分析数据库引擎图形所在的 VPC。

   1. 在**子网**下，选择与 Neptune Analytics 分析数据库引擎图形关联的子网。

   1. 在**安全组**下，选择与 Neptune Analytics 分析数据库引擎图形关联的所有安全组。

1. 选择**创建笔记本实例**。

1. 5 或 10 分钟后，当新笔记本达到 `Ready` 状态时，将其选中。**选择 “**打开 Jupyter**” 或 “打开”。 JupyterLab**

## 手动设置 Neptune 笔记本
<a name="graph-notebook-manual-setup"></a>

 您还可以使用适用于图形笔记本和图形资源管理器的 Amazon 开源包来设置 Neptune 笔记本环境。虽然使用开源程序包设置笔记本有多种方法，但推荐的方法是：
+  在本地计算机上设置[图形笔记本](https://github.com/aws/graph-notebook)和一个 Amazon EC2 SSH 隧道，该隧道将您的本地计算机连接到与 Amazon EC2 实例位于同一 VPC 上的 Neptune 集群。
+  在 VPC 内的 Amazon EC2 实例上设置[图形浏览器](https://github.com/aws/graph-explorer)。

 有关设置开源 [graph-notebook 和 graph-](https://github.com/aws/graph-notebook) [explorer](https://github.com/aws/graph-explorer) 包的详细说明，请参阅以下官方 Amazon 文档和存储库： GitHub 
+  [https://docs.amazonaws.cn//neptune/latest/userguide/graph-notebooks.html#graph-notebooks-local](https://docs.amazonaws.cn//neptune/latest/userguide/graph-notebooks.html#graph-notebooks-local) 
+  [https://docs.amazonaws.cn//neptune/latest/userguide/get-started-connect-ec2-same-vpc.html](https://docs.amazonaws.cn//neptune/latest/userguide/get-started-connect-ec2-same-vpc.html) 
+  [https://github.com/aws/graph-notebook/tree/main/additional-databases/neptune](https://github.com/aws/graph-notebook/tree/main/additional-databases/neptune) 
+  [https://github.com/aws/graph-explorer/blob/main/additionaldocs/getting-started/README.md#amazon-ec2-setup](https://github.com/aws/graph-explorer/blob/main/additionaldocs/getting-started/README.md#amazon-ec2-setup) 

**安全配置**  
 在设置环境时，请确保相应地设置了以下安全配置：
+  **Neptune 集群安全组** - 支持从 Amazon EC2 实例的安全组通过端口 8182 传入 TCP 流量。
+  **Amazon EC2 实例安全组** - 配置入站 HTTPS 规则以启用图形浏览器访问权限。

 建议针对 Neptune 集群和 Amazon EC2 实例使用一个安全组。这可以简化配置并减少潜在的连接问题。但是，请记得添加自引用规则，以支持组内资源之间进行通信。