你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:将 Jupyter Notebook 从 GitHub 导入到 Azure Cosmos DB for NoSQL(预览版)

适用范围: NoSQL

警告

Azure Cosmos DB 的 Jupyter Notebook 功能将于 2024 年 3 月 30 日停用;你将无法使用 Azure Cosmos DB 帐户中的内置 Jupyter 笔记本。 建议使用 Visual Studio Code 对 Jupyter 笔记本的支持或首选笔记本客户端。

本教程介绍如何从 GitHub 存储库导入 Jupyter Notebook 并在 Azure Cosmos DB for NoSQL 帐户中运行它们。 导入笔记本后,可以运行、编辑这些笔记本,并将所做的更改持久保存回同一个 GitHub 存储库。

先决条件

创建 GitHub 存储库的副本

  1. 导航到 azure-samples/cosmos-db-nosql-notebooks 模板存储库。

  2. 在自己的 GitHub 帐户或组织中创建模板存储库的新副本。

从 GitHub 拉取笔记本

与其每次启动工作区时都创建新笔记本,不妨从 GitHub 导入现有笔记本。 在本部分中,你将使用示例笔记本连接到现有的 GitHub 存储库。

  1. 导航到你的 Azure Cosmos DB 帐户,打开“数据资源管理器”。

  2. 选择“连接到 GitHub”。

    Screenshot of the Data Explorer with the 'Connect to GitHub' option highlighted.

  3. 在“连接到 GitHub”对话框中,选择适合你的 GitHub 存储库的访问选项,然后选择“授权访问”。

    Screenshot of the 'Connect to GitHub' dialog with options for various levels of access.

  4. 完成 GitHub 第三方授权工作流,该工作流授予访问 GitHub 存储库所需的组织访问权限。 有关详细信息,请参阅授权 GitHub 应用

  5. 在“管理 GitHub 设置”对话框中,选择之前创建的 GitHub 存储库。

    Screenshot of the 'Manage GitHub settings' dialog with a list of unpinned and pinned repositories.

  6. 返回数据资源管理器,找到固定存储库的新节点树,然后打开 website-metrics-python.ipynb 文件。

    Screenshot of the pinned repositories in the Data Explorer.

  7. 在笔记本的编辑器中,找到以下单元格。

    import pandas as pd
    pd.options.display.html.table_schema = True
    pd.options.display.max_rows = None
    
    df_cosmos.groupby("Item").size()
    
  8. 该单元格当前输出唯一项的数量。 将单元格的最后一行替换为新行,以便输出数据集中唯一操作的数量。

    df_cosmos.groupby("Action").size()
    
  9. 依次运行所有单元格以查看新数据集。 新数据集应仅包含“操作”列的三个潜在值。 (可选)可以为结果选择数据可视化。

    Screenshot of the Pandas dataframe visualization for the data.

将笔记本更改推送到 GitHub

提示

目前,如果空闲 20 分钟,将取消分配临时工作区。 每天使用时间上限为 60 分钟。 这些限制将来可能会发生变化。

要永久保存工作,请将笔记本保存回 GitHub 存储库。 在本部分中,你会将临时工作区中的更改作为新提交持久保存到 GitHub。

  1. 选择“保存”,为对笔记本所做的更改创建提交。

    Screenshot of the 'Save' option in the Data Explorer menu.

  2. 在“保存”对话框中,添加描述性提交消息。

    Screenshot of the 'Save' dialog with an example of a commit message.

  3. 导航到使用浏览器创建的 GitHub 存储库。 新提交现应在联机存储库中可见。

    Screenshot of the updated notebook on the GitHub website.

后续步骤