elasticache (2)

💻 Programming

[AWS] redis-cli 를 이용한 elasticache 데이터 조회

아직 redis-cli 설치가 안되어있다면 redis-cli 설치를 참고해주세요

 

1) 전체 key 개수 조회

redis.0001.apn.cache..amazonaws.com:6379> info keyspace
# Keyspace
db0:keys=23976,expires=2023,avg_ttl=125758665

2) 전체 key 목록 조회

redis.0001.apn.cache..amazonaws.com:6379> keys *
1) "EmployeeName:Tom Johnson"
2) "ZipCode:67410"
3) "EmployeeName:Tom Hanks"
4) "EmployeeName:Tom Thumb"
5) "ZipCode:15206"

 

keys 명령어 사용시 주의점: 운영환경에서는 신중히 사용할 것 !!

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

3) value 타입 확인 (value 타입에 따라 조회 명령어가 다름)

redis.0001.apn.cache..amazonaws.com:6379> get "EmployeeName:Tom Johnson"
(error) WRONGTYPE Operation against a key holding the wrong kind of value

redis.0001.apn.cache..amazonaws.com:6379> type "EmployeeName:Tom Johnson"

hash

4) 타입 유형별 조회 command

string : get <key>
hash : hgetall <key>
lists : lrange <key> <start> <end>
sets :  smembers <key>
sorted sets : ZRANGEBYSCORE <key> <min> <max>

데이터 타입에 따른 더 다양한 명령어는 Redis 공식문서 참고

Redis 모든 명령어 목록

5) 특정 key에 대한 value 조회 (hash타입 데이터)

redis.0001.apn.cache..amazonaws.com:6379> hgetall "EmployeeName:Tom Johnson"
1) "_class"
2) "com.mycompany.employee.EmployeeName"
3) "name"
4) "Tom Johnson"
5) "id"
6) "2267849"

redis.0001.apn.cache..amazonaws.com:6379> hget "EmployeeName:Tom Johnson" name

"Tom Johnson"

sudo yum install -y gcc 
wget http://download.redis.io/redis-stable.tar.gz && tar xvzf redis-stable.tar.gz && cd redis-stable && make
sudo cp src/redis-cli /usr/bin
redis-cli -h {redis-endpoint-address}

1) EC2에 build를 위한 gcc 설치

$ sudo yum install -y gcc 

2) redis stable 버전 다운로드 및 빌드

$ wget http://download.redis.io/redis-stable.tar.gz && tar xvzf redis-stable.tar.gz && cd redis-stable && make

3) redis-cli 로 접속해보기 (기본포트 사용시 포트정보 필요없음)

$ ./src/redis-cli -h {레디스엔드포인트주소} -p {포트번호}

4) 사용의 편의성을 위해 usr/bin 디렉토리에 redis-cli 명령어 복사해놓기

$ sudo cp src/redis-cli /usr/bin