diff --git a/README.md b/README.md index c825689f1..523aabaf2 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,8 @@ with open(targetfile, 'w', newline='') as outcsv: writer.writerow(row) ``` -You can also get back a pandas DataFrame object +#### Pandas +You can get back the cursor data as pandas DataFrame object. ```python from impala.util import as_pandas @@ -135,6 +136,16 @@ df = as_pandas(cur) # carry df through scikit-learn, for example ``` +#### Json +You also can get back the cursor as list of dict. Useful for build APIs that returns JSON objects. + +```python +import json +from impala.util import as_dict + +result = as_dict(cur) +data = json.dumps(result) +``` [pep249]: http://legacy.python.org/dev/peps/pep-0249/ [pandas]: http://pandas.pydata.org/ diff --git a/impala/util.py b/impala/util.py index 95aa42cf5..d40705401 100644 --- a/impala/util.py +++ b/impala/util.py @@ -64,6 +64,18 @@ def as_pandas(cursor, coerce_float=False): coerce_float=coerce_float) +def as_dict(cursor): + """ + :param cursor: `HiveServer2Cursor` + The cursor object that has a result set waiting to be fetched. + :return: list of dict + """ + columns = [col[0].lower() for col in cursor.description] + data = [ + dict(zip(columns, row)) for row in cursor.fetchall()] + return data + + def _random_id(prefix='', length=8): return prefix + ''.join(random.sample(string.ascii_uppercase, length))