Last active
September 7, 2018 07:43
-
-
Save LeiG/43f13f74a7a17c531d9d to your computer and use it in GitHub Desktop.
Query impala using python
This file contains hidden or 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
import yaml | |
import pyodbc | |
import impala.dbapi | |
import impala.util | |
with open('config.yaml', 'r') as f: | |
cfg = yaml.load(f) | |
# METHOD 1: using pyodbc to establish connection | |
connString = 'Driver=%s;Host=%s;Port=%d;Database=default; \ | |
AuthMech=3;UseSASL=1;UID=%s;PWD=%s;SSL=1; \ | |
AllowSelfSignedServerCert=1' % \ | |
(cfg['driver'], cfg['host'], cfg['port'], | |
cfg['username'], cfg['password']) | |
conn = pyodbc.connect(connString, autocommit = True) | |
# METHOD 2: using impyla to establish connection | |
conn = impala.dbapi.connect(host = cfg['host'], port = cfg['port'], | |
database = 'default', use_ssl = True, | |
user = cfg['username'], password = cfg['password'], | |
auth_mechanism = 'PLAIN') | |
# query and convert to pandas data frame | |
cur = conn.cursor() | |
cur.execute('SELECT * FROM req_requests LIMIT 10') | |
df = impala.util.as_pandas(cur) | |
df.head(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi LeiG,
In Method 1, what is the "Driver" parameter all about ? Is it an absolute path to the driver such as : Driver=/opt/cloudera/impalaodbc/lib/universal/libclouderaimpalaodbc.dylib ? (http://danielfrg.com/blog/2014/02/28/impala-to-python/)
Thanks,
Josh