如何使用Ambari检索名称节点主机名?

时间:2017-12-16 08:53:30

标签: ibm-cloud analytics-engine

python-ambariclient库有一个用于检索host_components的API:

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

如何提取IBM Analytics Engine群集的name_node?

我想我需要打电话:

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

检索以下信息:

{
  "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 个答案:

答案 0 :(得分:0)

首先,安装python-ambariclient库:

! pip install --quiet python-ambariclient

接下来,您可以使用以下命令检索名称节点主机名:

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)

答案 1 :(得分:0)

我创建了一个库来提取这些信息。安装时:

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

然后运行:

from ibm_analytics_engine import AmbariOperations
ambari_ops = AmbariOperations(vcap_filename='./vcap.json')
ambari_ops.get_namenode_hostname()