Skip to content

Instantly share code, notes, and snippets.

@prasanthkothuri
Last active June 23, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasanthkothuri/5e4db6b082ac19dd51aadb111e3d4d0d to your computer and use it in GitHub Desktop.
Save prasanthkothuri/5e4db6b082ac19dd51aadb111e3d4d0d to your computer and use it in GitHub Desktop.
#Determine the active namenode of HDFS, this is required as the webHDFS implementation doesn't redirect to active namenode
import os
import json
import xml.etree.ElementTree as ET
try:
from urllib import urlopen
except ImportError:
from urllib.request import urlopen
def find_element(conf, property):
tree = ET.parse(conf)
root = tree.getroot()
for elem in root.iter('property'):
if elem[0].text == property:
return elem[1].text
conf = os.environ.get('HADOOP_CONF_DIR','/etc/hadoop/conf') + '/hdfs-site.xml'
nameservice = find_element(conf, 'dfs.internal.nameservices')
namenodes = find_element(conf, 'dfs.ha.namenodes' + '.' + nameservice)
for namenode in namenodes.split(','):
if json.loads(urlopen('http://' + namenode
+ ':50070/jmx?get=Hadoop:service=NameNode,name=NameNodeStatus::State'
).read())['beans'][0]['State'] == 'active':
break
print(namenode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment