代码之家  ›  专栏  ›  技术社区  ›  Chris Snow

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

  •  0
  • Chris Snow  · 技术社区  · 7 年前

    这个 python-ambariclient 库具有用于检索host\u组件的api:

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

    如何提取IBM Analytics Engine群集的name\u节点?

    我想我需要打电话:

    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 回复  |  直到 7 年前
        1
  •  0
  •   Chris Snow    7 年前

    首先,安装 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)
    
        2
  •  0
  •   Chris Snow    7 年前

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

    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()