How to retrieve the name node host name using Ambari?

892 views Asked by At

The python-ambariclient library has an api for retrieving the host_components:

ambari.services(service_name).components(component_name).host_components

How can I extract the name_node for an IBM Analytics Engine cluster?

I think I need to make the call:

GET https://xxxx.bi.services.us-south.bluemix.net:9443/api/v1/clusters/AnalyticsEngine/services/HDFS/components/NAMENODE?fields=host_components

Which retrieves the following information:

{
  "href" : "https://xxxx.bi.services.us-south.bluemix.net:9443/api/v1/clusters/AnalyticsEngine/services/HDFS/components/NAMENODE?fields=host_components",
  "ServiceComponentInfo" : {
    "cluster_name" : "AnalyticsEngine",
    "component_name" : "NAMENODE",
    "service_name" : "HDFS"
  },
  "host_components" : [
    {
      "href" : "https://xxxx.bi.services.us-south.bluemix.net:9443/api/v1/clusters/AnalyticsEngine/hosts/xxxx.bi.services.us-south.bluemix.net/host_components/NAMENODE",
      "HostRoles" : {
        "cluster_name" : "AnalyticsEngine",
        "component_name" : "NAMENODE",
        "host_name" : "xxxx.bi.services.us-south.bluemix.net"
      }
    }
  ]
}
2

There are 2 answers

0
Chris Snow On BEST ANSWER

I have created a library to extract this information. Install with:

pip install --quiet --upgrade git+https://github.com/snowch/ibm-analytics-engine-python@master

Then run:

from ibm_analytics_engine import AmbariOperations
ambari_ops = AmbariOperations(vcap_filename='./vcap.json')
ambari_ops.get_namenode_hostname()
0
Chris Snow On

First, install the python-ambariclient library:

! pip install --quiet python-ambariclient

Next, you can use the following to retrieve the name node host name:

from future.standard_library import install_aliases
install_aliases()
from urllib.parse import urlparse

import json
vcap = json.load(open('./vcap.json'))

USER         = vcap['cluster']['user']
PASSWORD     = vcap['cluster']['password']
AMBARI_URL   = vcap['cluster']['service_endpoints']['ambari_console']
CLUSTER_ID   = vcap['cluster']['cluster_id']

url = urlparse(AMBARI_URL)

HOST = url.hostname
PORT = url.port
PROTOCOL = url.scheme

from ambariclient.client import Ambari
ambari = Ambari(HOST, port=PORT, username=USER, password=PASSWORD, protocol=PROTOCOL)

CLUSTER_NAME = ambari.clusters.next().cluster_name # gets first cluster - there will only be one

namenode_hc = ambari.clusters(CLUSTER_NAME).services('HDFS').components('NAMENODE').host_components

namenode_host_name = [hc.host_name for hc in namenode_hc if hc.host_name][0]

print(namenode_host_name)