Omley
Omley

Reputation: 446

How to get total count of hash-field pairs in redis

I have n number of hashes in a Redis DB. I am interested in knowing how many hash-field pairs exist in total in my db.

I see I can get the number of keys (hashes) in db through INFO command. And get number of fields in a given hash by HLEN. But neither meets my requirement.

Upvotes: 1

Views: 1586

Answers (2)

imbananko
imbananko

Reputation: 172

Same problem. I edited @Praga_t script, so we can count all hash fields in database.

for key in $(redis-cli --scan); do

    if [ $(redis-cli type $key) = "hash" ]; then
        length=$(redis-cli HLEN $key)

        let "count = count + length"
        echo key=$key, fields=$length. totalCount=$count
    fi 
done

Upvotes: 2

Praga_t
Praga_t

Reputation: 532

I had also face this situation and had a long search on this but I didn't get any direct commands to achieve this.So I use bash script to get the number of keys with respect to their type.

REDIS_KEY_PATTERN="${REDIS_KEY_PATTERN:-*}"
for key in $(redis-cli --scan --pattern "$REDIS_KEY_PATTERN")
do
type=$(redis-cli type $key)
if [ $type = "hash" ]
then
    let "count++"
fi
done
echo $count

I hope this will be helpful

Upvotes: 0

Related Questions