Check Data Type with TYPE
In this step, you will learn how to check the data type of a key stored in Redis using the TYPE
command. Redis supports various data types, including strings, lists, sets, sorted sets, and hashes. Understanding the data type of a key is crucial for performing appropriate operations on it.
First, connect to the Redis server using redis-cli
:
redis-cli
In the previous steps, you created several keys with different values. Let's check their data types.
First, let's check the data type of the mycounter
key:
TYPE mycounter
The output will be:
string
This indicates that mycounter
is stored as a string, even though it contains a numerical value. Redis automatically converts numerical values to strings when using SET
.
Next, let's check the data type of the user:1000:name
key:
TYPE user:1000:name
The output will be:
string
This also indicates that user:1000:name
is stored as a string.
Now, let's check the data type of a key that doesn't exist, such as nonexistentkey
:
TYPE nonexistentkey
The output will be:
none
This indicates that the key does not exist in the database.
To further illustrate data types, let's create a list:
LPUSH mylist "item1"
LPUSH mylist "item2"
Now, check the data type of mylist
:
TYPE mylist
The output will be:
list
This confirms that mylist
is stored as a list.
Similarly, you can create other data types like sets, sorted sets, and hashes and use the TYPE
command to verify their types. For example:
SADD myset "member1"
SADD myset "member2"
TYPE myset
The output will be:
set
ZADD mysortedset 1 "element1"
ZADD mysortedset 2 "element2"
TYPE mysortedset
The output will be:
zset
HSET myhash field1 "value1"
HSET myhash field2 "value2"
TYPE myhash
The output will be:
hash
Finally, exit the redis-cli
:
exit