Skip to content

Instantly share code, notes, and snippets.

@bmwh
Created June 24, 2015 01:53
Show Gist options
  • Save bmwh/c438f4b869fd155187bb to your computer and use it in GitHub Desktop.
Save bmwh/c438f4b869fd155187bb to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# impala-queries.py
# List, query and terminate in-flight Impala queries through the CM API
# -----------------------------------------------------------------------
# Copyright (C) 2015 Cloudera and Ben White
import urllib2, json, sys
# Cloudera Manager credentials
USERNAME = "admin"
PASSWORD = "admin"
# Cloudera Manager connection details
CMHOST = "localhost"
CMPORT = "7180"
CMSCHEME = "http"
# Cluster and service name (URL encoded)
CLUSTER = "CDH5"
SERVICE = "impala"
# -----------------------------------------------------------------------
#
# Print help
#
def help():
print """usage:
impala-queries.py list
impala-queries.py cancel QUERY-ID"""
if len(sys.argv) < 2:
help()
sys.exit(0)
#
# Set up urllib2
#
base_url = "%s://%s:%s" % (CMSCHEME, CMHOST, CMPORT)
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, base_url, USERNAME, PASSWORD)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(password_mgr)))
#
# List running queries
#
if sys.argv[1] == 'list':
list_url = ("%s/api/v4/clusters/%s/services/%s/impalaQueries" %
(base_url, CLUSTER, SERVICE))
response = json.loads(urllib2.urlopen(urllib2.Request(list_url)).read())
for q in response['queries']:
for k in q.keys():
print "%s: %s" % (k, q[k])
print
sys.exit(0)
if sys.argv[1] == 'cancel' and len(sys.argv) > 2:
query_id = sys.argv[2]
cancel_url = ("%s/api/v4/clusters/%s/services/%s/impalaQueries/%s/cancel" %
(base_url, CLUSTER, SERVICE, query_id))
request = urllib2.Request(cancel_url)
request.add_header('Content-Type', 'application/json')
response = json.loads(urllib2.urlopen(request, "").read())
for k in response.keys():
print "%s: %s" % (k, response[k])
print
sys.exit(0)
help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment