Showing posts with label Redis. Show all posts
Showing posts with label Redis. Show all posts

Wednesday, June 17, 2015

Redis 3.0.2 shell commands in Ubuntu 14.0.4

ram@ram-pc:~$ redis-cli INFO
# Server
redis_version:3.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9a95cedf214a3630
redis_mode:standalone
os:Linux 3.16.0-30-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.2
process_id:5171
run_id:37b0cd8c28fc3303789eac3e2b08b62866a25804
tcp_port:6379
uptime_in_seconds:623
uptime_in_days:0
hz:10
lru_clock:8494215
config_file:/etc/redis/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:508784
used_memory_human:496.86K
used_memory_rss:7081984
used_memory_peak:508784
used_memory_peak_human:496.86K
used_memory_lua:36864
mem_fragmentation_ratio:13.92
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1434556952
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:11
total_commands_processed:13
instantaneous_ops_per_sec:0
total_net_input_bytes:330
total_net_output_bytes:15769
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.41
used_cpu_user:0.17
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
ram@ram-pc:~$



ram@ram-pc:~$ redis-cli

127.0.0.1:6379> help
redis-cli 3.0.2
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit

127.0.0.1:6379> CONFIG GET *
  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) ""
  5) "masterauth"
  6) ""
  7) "unixsocket"
  8) ""
  9) "logfile"
..
..
127) "notify-keyspace-events"
128) ""
129) "bind"
130) "127.0.0.1"

Single Key Value Pair

127.0.0.1:6379[1]> set emp_name "ramkumar"
OK
127.0.0.1:6379[1]> set emp_name "Karthik"
OK
127.0.0.1:6379[1]> get emp_name
"Karthik"
127.0.0.1:6379[1]>

Multiple Key Value Pair

127.0.0.1:6379[1]> mset emp_no "1001" emp_name "Ramkumar" emp_deptid "dept01" location "Chennai"
OK
127.0.0.1:6379[1]> mget emp_no emp_name emp_deptid
1) "1001"
2) "Ramkumar"
3) "dept01"
127.0.0.1:6379[1]>

Some Numeric Operations

127.0.0.1:6379[1]> set incr 1
OK
127.0.0.1:6379[1]> set counter 1
OK
127.0.0.1:6379[1]> incr counter
(integer) 2
127.0.0.1:6379[1]> incr counter
(integer) 3
127.0.0.1:6379[1]> get counter

Incrimenting string will result errors
127.0.0.1:6379[1]> set strcounter "a"
OK
127.0.0.1:6379[1]> incr strcounter
(error) ERR value is not an integer or out of range
127.0.0.1:6379[1]> set strcounter a
OK
127.0.0.1:6379[1]> incr strcounter
(error) ERR value is not an integer or out of range
127.0.0.1:6379[1]> set strcounter 1a
OK
127.0.0.1:6379[1]> incr strcounter
(error) ERR value is not an integer or out of range
127.0.0.1:6379[1]>

Batch operation

127.0.0.1:6379[1]> MULTI
OK
127.0.0.1:6379[1]> set name "Karthik"QUEUED
127.0.0.1:6379[1]> set salary 100
QUEUED
127.0.0.1:6379[1]> incr salary
QUEUED
127.0.0.1:6379[1]> exec
1) OK
2) OK
3) (integer) 101
127.0.0.1:6379[1]>


127.0.0.1:6379[1]> mset emp:personal:name "Ram" emp:personal:native "Chennai" emp:personal:education "MSC"
OK
127.0.0.1:6379[1]> mget emp:personal
1) (nil)
127.0.0.1:6379[1]> mget emp:personal:name emp:personal:education
1) "Ram"
2) "MSC"
127.0.0.1:6379[1]>

or

127.0.0.1:6379[1]> hmset emp:personal name "Karthik" native "hyd" education "MCA"
OK
127.0.0.1:6379[1]> hvals emp:personal
1) "Karthik"
2) "hyd"
3) "MCA"
127.0.0.1:6379[1]>

127.0.0.1:6379[1]> hkeys emp:personal
1) "name"
2) "native"
3) "education"
127.0.0.1:6379[1]>

LISTs - multiple ordered sets (can act as queues or stacks)

127.0.0.1:6379[1]> rpush emp:info 1001 ram dept51 gritt chennai 27000
(integer) 6

0 - starting point
-1 - ending point (-1 indicates end)

127.0.0.1:6379[1]> lrange emp:info 0 -1
1) "1001"
2) "ram"
3) "dept51"
4) "gritt"
5) "chennai"
6) "27000"
127.0.0.1:6379[1]> lrange emp:info 2 2
1) "dept51"
127.0.0.1:6379[1]> lrange emp:info 2 3
1) "dept51"
2) "gritt"
127.0.0.1:6379[1]>

127.0.0.1:6379[1]> lrem emp:info 1 "gritt"
(integer) 1

127.0.0.1:6379[1]> lrange emp:info 0 -1
1) "1001"
2) "ram"
3) "dept51"
4) "chennai"
5) "27000"

127.0.0.1:6379[1]> lpop emp:info
"1001"
127.0.0.1:6379[1]> lpop emp:info
"ram"
127.0.0.1:6379[1]> lpop emp:info
"dept51"
127.0.0.1:6379[1]> lrange emp:info 0 -1
1) "chennai"
2) "27000"
127.0.0.1:6379[1]>

move keys between databases

127.0.0.1:6379[1]> select 2
OK
127.0.0.1:6379[2]> set name "ram"
OK
127.0.0.1:6379[2]> select 3
OK
127.0.0.1:6379[3]> get name
(nil)
127.0.0.1:6379[3]> select 2
OK
127.0.0.1:6379[2]> get name
"ram"
127.0.0.1:6379[2]> move name 3
(integer) 1
127.0.0.1:6379[2]> select 3
OK
127.0.0.1:6379[3]> get name
"ram"

reference:
Seven databases in seven weeks by  by Eric Redmond and Jim R. Wilson (Author)

Installing Redis on Ubuntu 14.04

ram@ram-pc:~$  sudo add-apt-repository ppa:chris-lea/redis-server
[sudo] password for ram:
 Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.
 More info: https://launchpad.net/~chris-lea/+archive/ubuntu/redis-server
Press [ENTER] to continue or ctrl-c to cancel adding it
gpg: keyring `/tmp/tmpydr5ohrk/secring.gpg' created
gpg: keyring `/tmp/tmpydr5ohrk/pubring.gpg' created
gpg: requesting key C7917B12 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpydr5ohrk/trustdb.gpg: trustdb created
gpg: key C7917B12: public key "Launchpad chrislea" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

ram@ram-pc:~$  sudo apt-get update
Ign http://repo.mongodb.org trusty/mongodb-org/3.0 InRelease
Ign http://extras.ubuntu.com trusty InRelease                                 
Hit http://extras.ubuntu.com trusty Release.gpg                               
Ign http://security.ubuntu.com trusty-security InRelease                      
Ign http://in.archive.ubuntu.com trusty InRelease   
...
...
Ign http://in.archive.ubuntu.com trusty/restricted Translation-en_IN          
Ign http://in.archive.ubuntu.com trusty/universe Translation-en_IN            
Fetched 3,115 kB in 26s (116 kB/s)                                            
Reading package lists... Done

ram@ram-pc:~$  sudo apt-get install redis-server
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
  libjemalloc1 redis-tools
The following NEW packages will be installed:
  libjemalloc1 redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 297 not upgraded.
Need to get 485 kB of archives.
After this operation, 1,426 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu/ trusty/main libjemalloc1 amd64 3.6.0-1chl1~trusty1 [77.2 kB]
Get:2 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu/ trusty/main redis-tools amd64 3:3.0.2-1chl1~trusty1 [79.7 kB]
Get:3 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu/ trusty/main redis-server amd64 3:3.0.2-1chl1~trusty1 [329 kB]
Fetched 485 kB in 16s (28.6 kB/s)    
Selecting previously unselected package libjemalloc1.
(Reading database ... 169044 files and directories currently installed.)
Preparing to unpack .../libjemalloc1_3.6.0-1chl1~trusty1_amd64.deb ...
Unpacking libjemalloc1 (3.6.0-1chl1~trusty1) ...
Selecting previously unselected package redis-tools.
Preparing to unpack .../redis-tools_3%3a3.0.2-1chl1~trusty1_amd64.deb ...
Unpacking redis-tools (3:3.0.2-1chl1~trusty1) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_3%3a3.0.2-1chl1~trusty1_amd64.deb ...
Unpacking redis-server (3:3.0.2-1chl1~trusty1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
Setting up libjemalloc1 (3.6.0-1chl1~trusty1) ...
Setting up redis-tools (3:3.0.2-1chl1~trusty1) ...
Setting up redis-server (3:3.0.2-1chl1~trusty1) ...
Starting redis-server: redis-server.
Processing triggers for libc-bin (2.19-0ubuntu6.5) ...
Processing triggers for ureadahead (0.100.0-16) ...
ram@ram-pc:~$

ram@ram-pc:~$ redis-cli ping
PONG

ram@ram-pc:~$ redis-cli
127.0.0.1:6379>

127.0.0.1:6379> shutdown
not connected> exit

ram@ram-pc:~$ sudo service redis-server restart
Stopping redis-server: redis-server.
Starting redis-server: redis-server.
ram@ram-pc:~$

ram@ram-pc:~$ redis-cli INFO
# Server
redis_version:3.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9a95cedf214a3630
redis_mode:standalone
os:Linux 3.16.0-30-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.2
process_id:5171
run_id:37b0cd8c28fc3303789eac3e2b08b62866a25804
tcp_port:6379
uptime_in_seconds:264
uptime_in_days:0
hz:10
lru_clock:8493856
config_file:/etc/redis/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0


reference:
http://linuxg.net/how-to-install-redis-server-2-8-17-on-ubuntu-14-04-ubuntu-12-04-and-derivative-systems/