Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the util function as_dict #390

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,25 @@ 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
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/
Expand Down
12 changes: 12 additions & 0 deletions impala/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def as_pandas(cursor, coerce_float=False):
coerce_float=coerce_float)


def as_dict(cursor):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think as_dicts() would be a clearer name (it sounds like it returns a single dictionary).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! Another option is dictfetchall. This name refers to the fetchall operation that occurs on the function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either is fine by me. It looks like the dictfetchall is used by django so there's some precedent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for this method. E.g. see impala/tests/test_hive_dict_cursor.py

"""
: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))

Expand Down