Cloudera Data Science Workbench API v2
Cloudera Data Science Workbench exposes a REST API that you can use to perform operations related to projects, jobs, and runs. You can use API commands to integrate Cloudera Data Science Workbench with third-party workflow tools or to control Cloudera Data Science Workbench from the command line.
API v2 supersedes the existing Jobs API. For more information on the Jobs API, see Jobs API in the Related information section, below.
How to view the API Specification
You can view the comprehensive API specification on the REST API v2 Reference page. See Related information, below, for the link.
- REST API:
https://<domain name of CML instance>/api/v2/swagger.html
- Python API:
https://<domain name of CML instance>/api/v2/python.html
swagger.json
.Quickstart
API key authentication
- Sign in to Cloudera Data Science Workbench.
- In Create API Key. , click
- Copy this API key to the clipboard.
Using curl from the command line
- Copy the API key.
- Open a terminal, and store it to a variable. On unix-based systems:
export API_KEY=<paste the API key value here>
- Copy the domain name, which is in the address bar of the browser. On unix-based systems:
export CDSW_DOMAIN=<domain>
(a value like:ml-xxxx123456.com
).
Using the Python client library
To use the Python API in your own code, first install the Python API client and point it to your cluster.
pip3 install https://$CDSW_DOMAIN/api/v2/python.tar.gz
Include the following code, and specify the values for <CDSW_DOMAIN> and <API_KEY> with variables or values for your installation.
# In a session:
api_instance = default_client()
# Outside a session:
default_client("https://"+cluster, APIKEY)
Then you can use commands in your script, such as a call to list projects:
projects = api_instance.list_projects()
The API returns objects that store values as attributes. To access the values, use dot notation. Do not use bracket notation as you would with a dictionary. For example:
myproj = client.create_project(...)
# This doesn't work:
myproj["id"]
# But this does
myproj.id
Check the Python documentation for a full list of the available API commands.
Using the Python client library in the REPL
Here is an example of a stub Python script that contains the environmental variables for your installation. Save it to your computer and run it locally to get a Python prompt for API commands.
demo.py
import clap
import argparse
parser = argparse.ArgumentParser(description=‘Test the generated python package.’)
parser.add_argument(“—host”, type=str, help=‘The host name of your workspace”)
parser.add_argument(“—token”, type=str, help=‘Your API key”)
args = parser.parse_args()
config = clap.Configuration()
config.host = ars.host
client = cmlapi.ApiClient(config)
client.set_default_header(“authorization”, “Bearer “ + args.token)
api = cmlapi.Apiapi(client)
python3 -i demo.py —host https://$CDSW_DOMAIN —token $API_KEY
>>> api
<cmlapi.api.api_api.ApiApi object at 0xlasjoid>
>>> api.api_list_projects()
api.api_list_projects(searchFilter=‘demo’)
api.api_list_projects(page_size=2)
api.api_list_projects(page_size=2, page_token=‘<token value>’)