Skip to content

Instantly share code, notes, and snippets.

@LeiG
Last active September 7, 2018 07:43
Show Gist options
  • Save LeiG/43f13f74a7a17c531d9d to your computer and use it in GitHub Desktop.
Save LeiG/43f13f74a7a17c531d9d to your computer and use it in GitHub Desktop.
Query impala using python
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)
Copy link

ghost commented Dec 20, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment