Skip to content

Commit d806349

Browse files
DOC-4952 start scan iteration page
1 parent 5b00653 commit d806349

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Iterate through results from `SCAN`, `HSCAN`, etc
13+
linkTitle: Scan iteration
14+
title: Scan iteration
15+
weight: 5
16+
---
17+
18+
Redis has a small family of related commands that retrieve
19+
keys and sometimes their associated values:
20+
21+
- [`SCAN`]({{< relref "/commands/scan" >}}) retrieves keys
22+
from the main Redis keyspace.
23+
- [`HSCAN`]({{< relref "/commands/hscan" >}}) retrieves keys and optionally,
24+
their values from a
25+
[hash]({{< relref "/develop/data-types/hashes" >}}) object.
26+
- [`SSCAN`]({{< relref "/commands/sscan" >}}) retrieves keys from a
27+
[set]({{< relref "/develop/data-types/sets" >}}) object.
28+
- [`ZSCAN`]({{< relref "/commands/zscan" >}}) retrieves keys and their score values from a
29+
[sorted set]({{< relref "/develop/data-types/sorted-sets" >}}) object.
30+
31+
These commands can potentially return large numbers of results, so Redis
32+
provides a paging mechanism to access the results in small, separate batches.
33+
With the basic commands, you must maintain a cursor value in your code
34+
to keep track of the current page. As a convenient alternative, `redis-py`
35+
also lets you access the results using an
36+
[iterator](https://docs.python.org/3/glossary.html#term-iterable).
37+
This handles the paging transparently, so you simply need to process
38+
the items it returns one-by-one in a `for` loop or pass the iterator
39+
object itself in place of a
40+
[sequence](https://docs.python.org/3/glossary.html#term-sequence).
41+
42+
Each of the commands has its own equivalent iterator. The following example shows
43+
how to use a `SCAN` iterator on the Redis keyspace. Note that, as with the `SCAN`
44+
command, the results are not sorted into any particular order, . Also, you
45+
can pass `match`, `count`, and `_type` parameters to `scan_iter()` to constrain
46+
the set of keys it returns (see the [`SCAN`]({{< relref "/commands/scan" >}})
47+
command page for examples).
48+
49+
{{< clients-example scan_iter scan Python >}}
50+
{{< /clients-example >}}
51+
52+
The iterators for the other commands are also named with `_iter()` after
53+
the name of the basic command (`hscan_iter()`, `sscan_iter()`, and `zscan_iter()`).
54+
They work in a similar way to `scan_iter()` except that you must pass a
55+
key to identify the object you want to scan. The example below shows how to
56+
iterate through the items in a sorted set using `zscan_iter()`.
57+
58+
{{< clients-example scan_iter zscan Python >}}
59+
{{< /clients-example>}}
60+
61+
Note that in this case, the item returned by the iterator is a
62+
[tuple](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences)
63+
with two elements for the key and score. By default, `hscan_iter()`
64+
also returns a 2-tuple for the key and value, but you can
65+
pass a value of `True` for the `no_values` parameter to retrieve just
66+
the keys.

0 commit comments

Comments
 (0)