-
Notifications
You must be signed in to change notification settings - Fork 277
DOC-4952 Python scan iterators page #1313
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d806349
DOC-4952 start scan iteration page
andy-stark-redis e35821a
DOC-4952 added Python scan iterator page
andy-stark-redis fa07449
Update content/develop/clients/redis-py/scaniter.md
andy-stark-redis f1d6fa9
DOC-4952 implemented fixes from feedback
andy-stark-redis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| --- | ||
| categories: | ||
| - docs | ||
| - develop | ||
| - stack | ||
| - oss | ||
| - rs | ||
| - rc | ||
| - oss | ||
| - kubernetes | ||
| - clients | ||
| description: Iterate through results from `SCAN`, `HSCAN`, etc | ||
| linkTitle: Scan iteration | ||
| title: Scan iteration | ||
| weight: 5 | ||
| --- | ||
|
|
||
| Redis has a small family of related commands that retrieve | ||
| keys and, in some cases, their associated values: | ||
|
|
||
| - [`SCAN`]({{< relref "/commands/scan" >}}) retrieves keys | ||
| from the main Redis keyspace. | ||
| - [`HSCAN`]({{< relref "/commands/hscan" >}}) retrieves keys and optionally, | ||
| their values from a | ||
| [hash]({{< relref "/develop/data-types/hashes" >}}) object. | ||
| - [`SSCAN`]({{< relref "/commands/sscan" >}}) retrieves keys from a | ||
| [set]({{< relref "/develop/data-types/sets" >}}) object. | ||
| - [`ZSCAN`]({{< relref "/commands/zscan" >}}) retrieves keys and their score values from a | ||
| [sorted set]({{< relref "/develop/data-types/sorted-sets" >}}) object. | ||
|
|
||
| These commands can potentially return large numbers of results, so Redis | ||
| provides a paging mechanism to access the results in small, separate batches. | ||
| With the basic commands, you must maintain a cursor value in your code | ||
| to keep track of the current page. As a convenient alternative, `redis-py` | ||
| also lets you access the results using an | ||
| [iterator](https://docs.python.org/3/glossary.html#term-iterable). | ||
| This handles the paging transparently, so you simply need to process | ||
| the items it returns one-by-one in a `for` loop or pass the iterator | ||
| object itself in place of a | ||
| [sequence](https://docs.python.org/3/glossary.html#term-sequence). | ||
|
|
||
| Each of the commands has its own equivalent iterator. The following example shows | ||
| how to use a `SCAN` iterator on the Redis keyspace. Note that, as with the `SCAN` | ||
| command, the results are not sorted into any particular order, . Also, you | ||
| can pass `match`, `count`, and `_type` parameters to `scan_iter()` to constrain | ||
| the set of keys it returns (see the [`SCAN`]({{< relref "/commands/scan" >}}) | ||
| command page for examples). | ||
|
|
||
| ```py | ||
| import redis | ||
|
|
||
| r = redis.Redis(decode_responses=True) | ||
| # REMOVE_START | ||
|
||
| r.flushall() | ||
| # REMOVE_END | ||
|
||
|
|
||
| r.set("key:1", "a") | ||
| r.set("key:2", "b") | ||
| r.set("key:3", "c") | ||
| r.set("key:4", "d") | ||
| r.set("key:5", "e") | ||
|
|
||
| for key in r.scan_iter(): | ||
| print(f"Key: {key}, value: {r.get(key)}") | ||
| # >>> Key: key:1, value: a | ||
| # >>> Key: key:4, value: d | ||
| # >>> Key: key:3, value: c | ||
| # >>> Key: key:2, value: b | ||
| # >>> Key: key:5, value: e | ||
| ``` | ||
|
|
||
| The iterators for the other commands are also named with `_iter()` after | ||
| the name of the basic command (`hscan_iter()`, `sscan_iter()`, and `zscan_iter()`). | ||
| They work in a similar way to `scan_iter()` except that you must pass a | ||
| key to identify the object you want to scan. The example below shows how to | ||
| iterate through the items in a sorted set using `zscan_iter()`. | ||
|
|
||
| ```py | ||
| r.zadd("battles", mapping={ | ||
| "hastings": 1066, | ||
| "agincourt": 1415, | ||
| "trafalgar": 1805, | ||
| "somme": 1916, | ||
| }) | ||
|
|
||
| for item in r.zscan_iter("battles"): | ||
| print(f"Key: {item[0]}, value: {int(item[1])}") | ||
| # >>> Key: hastings, value: 1066 | ||
| # >>> Key: agincourt, value: 1415 | ||
| # >>> Key: trafalgar, value: 1805 | ||
| # >>> Key: somme, value: 1916 | ||
| ``` | ||
|
|
||
| Note that in this case, the item returned by the iterator is a | ||
| [tuple](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences) | ||
| with two elements for the key and score. By default, `hscan_iter()` | ||
| also returns a 2-tuple for the key and value, but you can | ||
| pass a value of `True` for the `no_values` parameter to retrieve just | ||
| the keys: | ||
|
|
||
| ```py | ||
| r.hset("details", mapping={ | ||
| "name": "Mr Benn", | ||
| "address": "52 Festive Road", | ||
| "hobbies": "Cosplay" | ||
| }) | ||
|
|
||
| for key in r.hscan_iter("details", no_values=True): | ||
| print(f"Key: {key}, value: {r.hget("details", key)}") | ||
| # >>> Key: name, value: Mr Benn | ||
| # >>> Key: address, value: 52 Festive Road | ||
| # >>> Key: hobbies, value: Cosplay | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.