工作区 CLI(旧版)

重要

本文档已过时,将来可能不会更新。

此信息适用于旧版 Databricks CLI 0.18 及更低版本。 Databricks 建议改用较新的 Databricks CLI 0.205 或更高版本。 请参阅什么是 Databricks CLI?。 若要查找你的 Databricks CLI 的版本,请运行 databricks -v

要从 Databricks CLI 版本 0.18 或更低版本迁移到 Databricks CLI 版本 0.205 或更高版本,请参阅 Databricks CLI 迁移

可以通过将 Databricks 工作区 CLI 子命令追加到 databricks workspace 之后来运行这些命令。 这些子命令调用工作区 API

databricks workspace -h
Usage: databricks workspace [OPTIONS] COMMAND [ARGS]...

  Utility to interact with the Databricks workspace. Workspace paths must be
  absolute and be prefixed with `/`.

Common Options:
  -v, --version  [VERSION]
  -h, --help     Show this message and exit.

Commands:
  delete      Deletes objects from the Databricks workspace. rm and delete are synonyms.
    Options:
        -r, --recursive
  export      Exports a file from the Databricks workspace.
    Options:
      -f, --format FORMAT      SOURCE, HTML, JUPYTER, or DBC. Set to SOURCE by default.
      -o, --overwrite          Overwrites file with the same name as a workspace file.
  export_dir  Recursively exports a directory from the Databricks workspace.
    Options:
      -o, --overwrite          Overwrites local files with the same names as workspace files.
  import      Imports a file from local to the Databricks workspace.
    Options:
      -l, --language LANGUAGE  SCALA, PYTHON, SQL, R  [required]
      -f, --format FORMAT      SOURCE, HTML, JUPYTER, or DBC. Set to SOURCE by default.
      -o, --overwrite          Overwrites workspace files with the same names as local files.
  import_dir  Recursively imports a directory to the Databricks workspace.

    Only directories and files with the extensions .scala, .py, .sql, .r, .R,
    .ipynb are imported. When imported, these extensions are stripped off
    the name of the notebook.

    Options:
      -o, --overwrite          Overwrites workspace files with the same names as local files.
      -e, --exclude-hidden-files
  list        Lists objects in the Databricks workspace. ls and list are synonyms.
    Options:
      --absolute               Displays absolute paths.
      -l                       Displays full information including ObjectType, Path, Language
  ls          Lists objects in the Databricks workspace. ls and list are synonyms.
    Options:
      --absolute               Displays absolute paths.
      -l                       Displays full information including ObjectType, Path, Language
  mkdirs      Makes directories in the Databricks workspace.
  rm          Deletes objects from the Databricks workspace. rm and delete are synonyms.
    Options:
        -r, --recursive

从工作区中删除对象

若要显示使用情况文档,请运行 databricks workspace delete --helpdatabricks workspace rm --help

databricks workspace delete --recursive "/Users/someone@example.com/My Folder"

或:

databricks workspace rm --recursive "/Users/someone@example.com/My Folder"

如果成功,则不显示任何输出。

将工作区中的文件导出到本地文件系统

若要显示使用情况文档,请运行 databricks workspace export --help

databricks workspace export --overwrite --format JUPYTER "/Users/someone@example.com/My Python Notebook" /Users/me/Downloads

此选项还可用于从 Databricks 存储库导出笔记本:

databricks workspace export "/Repos/someone@example.com/MyRepoNotebook" /Users/me/Downloads

如果成功,则不显示任何输出。

将工作区中的目录导出到本地文件系统

若要显示使用情况文档,请运行 databricks workspace export_dir --help

databricks workspace export_dir --overwrite /Users/someone@example.com/my-folder /Users/me/Downloads/my-folder
/Users/someone@example.com/my-folder/My Python Notebook -> /Users/me/Downloads/my-folder/My Python Notebook.py
/Users/someone@example.com/my-folder/My Scala Notebook -> /Users/me/Downloads/my-folder/My Scala Notebook.scala
/Users/someone@example.com/my-folder/My R Notebook -> /Users/me/Downloads/my-folder/My R Notebook.r
/Users/someone@example.com/my-folder/My SQL Notebook -> /Users/me/Downloads/my-folder/My SQL Notebook.sql

将本地文件系统中的文件导入到工作区

若要显示使用情况文档,请运行 databricks workspace import --help

只能导入扩展名为 .scala.py.sql.r.R 的文件。 导入后,将从笔记本名称中删除这些扩展名。

databricks workspace import ./a.py /Users/someone@example.com/example
./a.py -> /Users/someone@example.com/example/a

将本地文件系统中的目录导入工作区

若要显示使用情况文档,请运行 databricks workspace import_dir --help

此命令以递归方式将本地文件系统中的目录导入到工作区。 仅导入扩展名为 .scala.py.sql.r.R 的目录和文件。 导入后,将从笔记本名称中删除这些扩展名。

若要覆盖目标路径上的现有笔记本,请添加标志 --overwrite-o

tree
.
├── a.py
├── b.scala
├── c.sql
├── d.R
└── e
databricks workspace import_dir . /Users/someone@example.com/example
./a.py -> /Users/someone@example.com/example/a
./b.scala -> /Users/someone@example.com/example/b
./c.sql -> /Users/someone@example.com/example/c
./d.R -> /Users/someone@example.com/example/d
databricks workspace ls /Users/someone@example.com/example -l
NOTEBOOK   a  PYTHON
NOTEBOOK   b  SCALA
NOTEBOOK   c  SQL
NOTEBOOK   d  R
DIRECTORY  e

列出工作区中的对象

若要显示使用情况文档,请运行 databricks workspace list --helpdatabricks workspace ls --help

databricks workspace list --absolute --long --id /Users/someone@example.com

或:

databricks workspace ls --absolute --long --id /Users/someone@example.com
NOTEBOOK           /Users/someone@example.com/My Python Notebook  PYTHON  1234567898012345
NOTEBOOK           /Users/someone@example.com/My Scala Notebook   SCALA   2345678980123456
NOTEBOOK           /Users/someone@example.com/My R Notebook       R       3456789801234567
DIRECTORY          /Users/someone@example.com/My Directory                4567898012345678
MLFLOW_EXPERIMENT  /Users/someone@example.com/My_Experiment               5678980123456789

在工作区中创建目录

若要显示使用情况文档,请运行 databricks workspace mkdirs --help

databricks workspace mkdirs "/Users/someone@example.com/My New Folder"

如果成功,则不显示任何输出。