Created
June 24, 2015 01:53
-
-
Save bmwh/c438f4b869fd155187bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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]) | |
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]) | |
sys.exit(0) | |
help() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment