Using API v2 Python client
The Cloudera AI API v2 Python client provides method signatures, authentication parameters, and code examples for managing workspace Projects, Jobs, and Runtimes.
Setting up the client
The client object is used to execute API commands. Create the client by connecting to the Cloudera AI Workbench using your API key and the
default_client() function.
- Select Apps in the left navigation pane on the Anywhere Cloud Console.
- Search for the required Cloudera AI Workbench
instance.
Ensure that the status of the instance is
Deployed. - Click on the name of the required instance.
A pane opens displaying the available information for the selected instance.
- Click the URL of the selected Cloudera AI Workbench
instance.
The Cloudera AI Workbench home page is displayed.
- Click .
- Go to the API Keys section and click on Download Python SDK.
-
Install the downloaded file (adjust the path if you saved it elsewhere):
pip3 install ~/Downloads/python.tar.gz - Generate an API key.
- Navigate to in the Cloudera AI Workbench console.
- Select Create API Key.
- Copy the generated API key value to the clipboard.
- Create an instance of the API and test the
connection:
> import cmlapi > client = cmlapi.default_client(url="<workbench domain>", cml_api_key="<api key>") > client.list_projects() - If your Cloudera AI Workbench uses a custom self-signed certificate,
configure the client to trust the
certificate:
> config = cmlapi.Configuration() > config.host = "<workbench domain>" > config.ssl_ca_cert = "<path to SSL certificate>" > api = cmlapi.ApiClient(config) > api.set_default_header("authorization", "Bearer " + "<api key>") > client = cmlapi.CMLServiceApi(api)
Using the Project API
- List the available
Projects:
projects = client.list_projects() # returns the first 10 second_page_projects = client.list_projects(page_token=projects.next_page_token) # returns the next 10 lots_of_projects = client.list_projects(page_size=100) second_page_lots_of_projects = client.list_projects(page_size=100, page_token=lots_of_projects.next_page_token) # must re-include the same page size for future pages filtered_projects = client.list_projects(search_filter=json.dumps({"name": "production"})) # returns all projects with "production" in the name - Select a particular Project ID, and then validate it with a command to fetch the
Project:
project = client.get_project(project_id="<project id>") - Delete a Project with the following
command:
client.delete_project(project_id="<project id>")
Using the Jobs API
- List the jobs in the
Project.
jobs = client.list_jobs(project_id="<projectid>") - Delete a
Job.
client.delete_job(project_id="<project id>",job_id="<job_id>") - Create and start a Job
run.
job_run =client.create_job_run(cmlapi.CreateJobRunRequest(), project_id="<project id>",job_id="<job id>") - Stop a running Job if
required.
client.stop_job_run(project_id="<project id>",job_id="<job id>", run_id=job_run.id) - Check the status of a
Job:
client.list_job_runs(project_id, job_id, sort="-created_at", page_size=1)
Using the Cursor class
list_projects, list_jobs, or
list_runtimes. The Cursor provides an iterable interface for retrieving
all results across pages automatically. The following example lists available
runtimes.cursor = Cursor(client.list_runtimes)
runtimes = cursor.items()
for rt in runtimes:
print(rt.image_identifier)You can also apply a search filter when creating the Cursor. The following example filters runtimes by image identifier:
cursor = Cursor(client.list_runtimes, search_filter = json.dumps({"image_identifier":"jupyter"}))
End to end example
This example demonstrates how to create a Project, a Job, and a Job run. The Job uses the
analysis.py file included in the Python template.
import cmlapi
import time
import sys
import random
import string
random_id=''.join(random.choice(string.ascii_lowercase) for i in range(10))
project_body = cmlapi.CreateProjectRequest(name="APIv2 Test Project " + random_id, description="Project for testing APIv2", template="Python")
project_result = client.create_project(project_body)
poll_retries = 5
while True: # wait for the project to reach the "success" state
project = client.get_project(project_result.id)
if project.creation_status == "success":
break
poll_retries -= 1
if poll_retries == 0:
print("failed to wait for project creation to succeed")
sys.exit(1)
time.sleep(2) # wait a couple seconds before the next retry
job_body = cmlapi.CreateJobRequest(name="APIv2 Test Job " + random_id, kernel="python3", script="analysis.py")
job_result = client.create_job(job_body, project_id=project_result.id)
job_run = client.create_job_run(cmlapi.CreateJobRunRequest(), project_id=project_result.id, job_id=job_result.id)
