How do you get a list of streams?
Asked Answered
A

4

13

I need to get a list of all streams (keys) in a database but I can't find a command for it.

I've already tried going over all keys and checking their typebut it is too slow/expensive.

I'd like to do something like XSCAN and get a list of keys like: ["stream1", "stream2"]

Absorber answered 9/1, 2019 at 0:12 Comment(0)
T
22

As of version 6.0 you can use the TYPE option to ask SCAN to only return objects that match a given type.

SCAN 0 TYPE stream

https://redis.io/commands/scan

Traveler answered 27/10, 2020 at 16:11 Comment(0)
H
4

There's no such command. Same as there's no way to get a list of other data structures, e.g. LIST, SET.

Instead, you can create an extra SET to record the keys of the streams you created. So that you can scan the SET to get the list of streams.

Heffernan answered 9/1, 2019 at 2:38 Comment(2)
I'm aware that there is no XSCAN but there are methods "get a list of other data structures" SortedSet Set Hash. I would rather have an XSCAN so I don't have to keep a list myself.Absorber
@Absorber ZSCAN, SSCAN and HSCAN are commands for scanning the elements in a single sorted set, set and hash. They are NOT commands for getting a list of these data structures. If you only want to scan elements in a single stream, you can use the XREAD. If you need to get a list of sorted set, set, hash or stream, you still have to keep a list yourself.Heffernan
E
1

If you can have a prefix in the stream names ex: 'MyStream:1', 'MyStream:2' Then you can use regular scan command with patterns matching MyStream:*

EDIT: To address OPs concern to not have to use prefix and use SCAN command as is, adding from comments :

You can avoid using a prefix by using namespacing capability provided by redis. You can assign a 'database' (0-15 by default) for streams names. Say you use database 5 for streams, then scan command in database 5 should return the keys in it only. redis.io/commands/select

Ermeena answered 9/1, 2019 at 14:42 Comment(4)
I'm going with that for now although I was hoping to have a way to get the list without a prefix.Absorber
@Absorber : You can avoid using a prefix by using namespacing capability provided by redis. You can assign a 'database' (0-15 by default) for streams names. Say you use database 5 for streams, then scan command in database 5 should return they keys in it only. redis.io/commands/selectErmeena
thanks for the suggestion. using the databases is something I hadn't think of.Absorber
@Absorber : Glad to be of help. If you think this best answers your question, please accept it to close the question. I have updated the database suggestion in original answer.Ermeena
P
-1

I think you can do that with the command below

xread streams mystream 0

I think this should help.

Portly answered 12/9, 2023 at 17:17 Comment(1)
This is not what was asked. XREAD documentation says "Read data from one or multiple streams".Searching

© 2022 - 2024 — McMap. All rights reserved.