Starting a Job Run Using Python
To start a job run using Python, Cloudera recommends using Requests, an HTTP library for Python; it comes with a convenient API that makes it easy to submit job run requests to Cloudera Data Science Workbench.
Extending the Risk Analysis example from the previous section, the following sample Python code will create an HTTP request to run the job with the job ID, 208.
Python
2
# example.py import requests import json HOST = "http://cdsw.example.com" USERNAME = "alice" API_KEY = "uysgxtj7jzkps96njextnxxmq05usp0b" PROJECT_NAME = "risk-analysis" JOB_ID = "208" url = "/".join([HOST, "api/v1/projects", USERNAME, PROJECT_NAME, "jobs", JOB_ID, "start"]) job_params = {"START_DATE": "2017-01-01", "END_DATE": "2017-01-31"} res = requests.post( url, headers = {"Content-Type": "application/json"}, auth = (API_KEY,""), data = json.dumps({"environment": job_params}) ) print "URL", url print "HTTP status code", res.status_code print "Engine ID", res.json().get('engine_id')
When you run the code, you should see output of the form:
python example.py
URL http://cdsw.example.com/api/v1/projects/alice/risk-analysis/jobs/208/start HTTP status code 200 Engine ID r11w5q3q589ryg9o