Community Articles
Find and share helpful community-sourced technical articles
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.
Labels (1)

Pre-requisites

Hortonworks Data Platform 2.5 on CentOS 7.2

Python distribution that comes with HDP 2.5 - Python 2.7.5

Download and install pip

#wget https://bootstrap.pypa.io/get-pip.py

Install add-on package

#pip install requests

Start Python CLI (default version)

#python

Import pre-reqs

>>>import requests
>>>import json
>>>import sys

Environment Variables

Set Ambari domain variable to the IP address or FQDN of your Ambari node.

>>>AMBARI_DOMAIN = '127.0.0.1'

Set Ambari port, Ambari user and password variables to match your specifics.

>>>AMBARI_PORT = '8080'
>>>AMBARI_USER_ID = 'admin'
>>>AMBARI_USER_PW = 'admin'

Set the following variable to the IP address or FQDN of your ResourceManager node.

>>>RM_DOMAIN = '127.0.0.1'

Set Resource Manager port variable

>>>RM_PORT = '8088'

Ambari REST API Call Examples

Let's find Cluster Name, Cluster Version, Stack and Stack Version:

>>>restAPI='/api/v1/clusters'
>>>url="http://"+AMBARI_DOMAIN+":"+AMBARI_PORT+restAPI
>>>r=requests.get(url, auth=(AMBARI_USER_ID, AMBARI_USER_PW))
>>>json_data=json.loads(r.text)
>>>CLUSTER_NAME = json_data["items"][0]["Clusters"]["cluster_name"]
>>>print(CLUSTER_NAME)
>>>CLUSTER_VERSION =json_data["items"][0]["Clusters"]["version"]
>>>print(CLUSTER_VERSION)
>>>STACK = CLUSTER_VERSION.split('-')[0]
>>>print(STACK)
>>>STACK_VERSION = CLUSTER_VERSION.split('-')[1]
>>>print(STACK_VERSION)
>>>CLUSTER_INFO=json_data
>>>print(CLUSTER_INFO)

Let's find HDP stack repository:

>>>restAPI = "/api/v1/stacks/"+STACK+"/versions/"+STACK_VERSION+"/operating_systems/redhat7/repositories/"+CLUSTER_VERSION
>>>url = "http://"+AMBARI_DOMAIN+":"+AMBARI_PORT+restAPI
>>>r= requests.get(url, auth=(AMBARI_USER_ID, AMBARI_USER_PW))
>>>json_data=json.loads(r.text)
>>>print(json_data)
>>>REPOSITORY_NAME=json_data["Repositories"]["latest_base_url"]
>>>print(REPOSITORY_NAME)

A more elegant approach is to create utility functions. See my repo: https://github.com/cstanca1/HDP-restAPI/

restAPIFunctions.py script in the repo defines a number of useful functions that I have collected over time.

Run restAPIFunctions.py

The same example presented above can now be implemented with a single line call to get CLUSTER_NAME, CLUSTER_VERSION and CLUSTER_INFO using getClusterVersionAndName() function:

>>>CLUSTER_NAME,CLUSTER_VERSION,CLUSTER_INFO = getClusterVersionAndName()
>>>print(CLUSTER_NAME)
>>>print(CLUSTER_VERSION)
>>>print(CLUSTER_INFO)

Resource Manager REST API Call Examples

>>>RM_INFO=getResourceManagerInfo()
>>>RM_SCHEDULER_INFO=getRMschedulerInfo()
>>>print(RM_INFO)
>>>print(RM_SCHEDULER_INFO)

Other Functions

These are other functions included in restAPIFunctions.py script

getServiceActualConfigurations()
getClusterRepository()
getAmbariHosts()
getResourceManagerInfo()
getRMschedulerInfo()
getAppsSummary()
getNodesSummary()
getServiceConfigTypes()
getResourceManagerMetrics()
getCheckClusterForRollingUpgrades()
3,750 Views
Don't have an account?
Coming from Hortonworks? Activate your account here
Version history
Revision #:
3 of 3
Last update:
‎02-05-2020 12:10 AM
Updated by:
 
Contributors
Top Kudoed Authors