Jedis operation commands for various data types in Redis, Redis command Daquan

Source: Internet
Author: User
Tags array length hash min redis sort
introduction of common data types:

There are five data types used by Redis: String,hash,list,set,zset (sorted set). 1. String Type

String is the simplest type, and a key corresponds to a value

of type string with a maximum of 1G.
A string type can be treated as an integer, allowing the "INCR" command family to operate (Incrby, DECR, Decrby), in which case the value of the integer is limited to 64-bit signed numbers. The
separate element types contained in list, set, and Zset are Redis string types.
2. List Type
Linked list type, the main function is push, pop, get all values of a range and so on. The key can be understood as the name of the linked list.

in Redis, lists are the list of Redis strings, sorted by insert order. For example, using the Lpush command to insert an element in the list header, use the Rpush command to insert an element at the end of the list. When one of these two commands acts on an empty key, a new list is created.

the maximum length of a list is 2^32-1 elements. (2^32 = 4 294 967 296) 4.2 billion AH
3. Set Type
Collections, similar to the concept of collections in mathematics. The key in the operation is understood as the name of the collection.

in Redis, set is an unordered collection of redis strings, and no duplicate elements are allowed.

the maximum number of elements for set is 2^32-1. (2^32 = 4 294 967 296) 4.2 billion AH

, the operation of set in Redis also has the intersection, the set, the difference set and so on.
4, Zset (Sorted Set) type
Zset is an upgraded version of Set, which adds a sequential attribute on the set, which can be specified when a modification element is added, and Zset automatically installs the specified value in the reorder order after each assignment. It can be understood as a table, a column of value, and a list of stored orders. The key in the operation is understood as the name of Zset.

the maximum number of elements for Zset is 2^32-1. (2^32 = 4 294 967 296) 4.2 billion Ah,

for an already ordered zset, you can still use the sort command by specifying asc| The desc parameter to sort it.
5. Hash type
Hash is the data type closest to the relational database structure, which can convert one object in a database or program into a hashmap stored in Redis.
Second, Jedis operation command: 1. Commands for value operations
Exists (key): Verify that a key exists

del (key): Delete a key type

(key): The type of the return value

keys (pattern): Returns all keys

that satisfy a given pattern Randomkey: Randomly returns a key

rename (Oldname, newname) for key space: Rename key from Oldname to NewName and delete newname

if newname exists Dbsize: Returns the number of keys in the current database

expire: Sets the active time (s) of a key

TTL: Gets the active time of a key

Select (Index): Query by index

move (key , Dbindex): Transfers the key from the current database to a dbindex indexed database

FLUSHDB: Deletes all keys in the currently selected database

Flushall: Delete all keys in all databases
2. Commands for string manipulation
Set (key, value): Assigns the value of a string named key in the database to

the values "get" (key): Returns a string of value

Getset (key, value) named key in the database : Assigns a string named key to the last value

mget (key1, Key2,..., key N): Returns multiple strings in the library whose names are Key1,key2 ... Value

setnx (key, value): If a string with the name key does not exist, add a string to the library with the name key and the value

Setex (key, time, value) : Add a string to the library (named key, value) at the same time, set the expiration Time

mset (Key1, value1, Key2, value2,... key N, value N): Assigning multiple strings at the same time, named key I string assignment value I

msetnx (Key1, value1, Key2, value2,... key N, value N): If all strings with the name key I do not exist, add string to the library, name key I is assigned value I

incr (key): string with the name key is incremented by 1 operation

Incrby (Key, Integer): String with Name Key added integer

decr (key) : String minus 1 with name Key

Decrby (Key, Integer): string with the name key reduced by integer

append (key, value) : string with Name key appended value

substr (key, start, end): substring of value that returns string with Name key
3. Commands for list operations
Rpush (key, value): Adds a value of element Lpush (key, value) at the end of the list with the name Key

: Adds an element of value to the list header named key

Llen (key) : Returns the length of the list named key

Lrange (key, start, end): Returns the element between start and end in list named key (subscript starting from 0, below)

LTrim (key, start, end) : intercepts the list named key, preserving the element between start and end

lindex (key, index): Returns the element

LSet (key, index, value) of the index position in the list named key : Assigns the element of the index position in the list named key to value

Lrem (key, Count, value): Removes the element that has the value in the list of Count names key. Count is 0, removing all elements that have values of value, Count>0 removes the element with count values from start to end, Count<0 removes |count| values as value from the tail.

Lpop (Key): Returns and removes the first element in the list named key

Rpop (key): Returns and removes the tail element in the list named key

Blpop (Key1, Key2,... key N, timeout) : The block version of the Lpop command. That is, when timeout is 0 o'clock, if a list with the name key I does not exist or the list is empty, the command ends. If timeout>0, if this is the case, wait for timeout seconds, and if the problem is not resolved, perform a pop operation on the list starting with key i+1.

Brpop (key1, Key2,... key N, timeout): The block version of Rpop. Refer to the previous command.

Rpoplpush (Srckey, Dstkey): Returns and removes the tail element of the list named Srckey, and adds the element to the head of the list named Dstkey
4. Commands for set operations
Sadd (Key, member): add element to set named Key member

Srem (Key, member): delete element in set named Key member

Spop (key) : Randomly returns and deletes an element in a set named Key

Smove (Srckey, Dstkey, member): Moves the member element from a collection named Srckey to a collection named Dstkey

SCard (key) : Returns the cardinality of the set named Key

Sismember (key, member): Tests whether the member is a set element named key

sinter (Key1, Key2,... key N): Intersection

Sinterstore (Dstkey, Key1, Key2,... key N): asks for the intersection and saves the intersection to Dstkey's collection

sunion (key1, Key2,... key N): Set

Sunionstore (Dstkey , Key1, Key2,... key N): Gather and save the set to Dstkey collection

Sdiff (key1, Key2,... key N): Differential set

sdiffstore (Dstkey, Key1, Key2,... key N): request The difference set and save the difference to the Dstkey collection

smembers (key): Returns all elements of a set named key

Srandmember (key): Randomly returns an element of a set with the name key
5. Commands for Zset (sorted set) operation
Zadd (key, Score, member): Adds an element member,score for sorting to a zset named key.

If the element already exists, the order of the element is updated according to score. Zrem (Key, member): Delete the element in Zset named key member Zincrby (key, Increment, member): If the element Zset already exists in member named Key, The score of the element increases increment; otherwise, the element is added to the collection, and its score value is increment Zrank (key, member) : Returns the rank of the member element (that is, index, starting from 0) of the Zset with the name key (the element has been sorted score from small to large), and returns "nil" Zrevrank (key, member) if there is no member element : Returns the rank of the member element (that is, index, starting from 0) of the Zset with the name key (the element has been sorted by score from large to small), and returns "nil" Zrange (key, start, end) If there is no member element : Returns all elements of index from start to end (key, start, end) in Zset with the name key (the element has been sorted score from small to large) : Returns all elements of Zset (key, Min, Max) from start to end of the index named key (the element has been sorted by score from large to small) Zrangebyscore: Returns Zset in the name Key score >=

Min and score all elements of <= Max Zcard (key): Returns the cardinality of the Zset named Key Zscore (key, Element): Returns the zset of element elements in score with the name key Zremrangebyrank (Key, Min, max): Removes Zset rank >= min in the name of key and all elements of rank <= max Zremrangebyscore (key, Min, max): delete name Ke Y zset score >= min and all elements of score <= Max Zunionstore/zinterstore (Dstkeyn, Key1,..., KeyN, WEIGHTS W1,... WN, AGgregate sum| min| MAX): Sets and intersections of N Zset, and saves the last collection in Dstkeyn. For the score of each element in a collection, multiply the weight parameter for the aggregate operation before doing it. If weight is not provided, the default is 1. The default aggregate is sum, that is, the score of the elements in the result set is the value of the sum operation for all the corresponding elements of the collection, while Min and Max means that the score of the elements in the result set are the minimum and maximum values in the corresponding elements of all the collections.
6, the command of the hash operation
Hset (Key, field, value): Adds an element to the hash named key Field<->value

hget (Key, field): Returns the value of field corresponding to the hash named key

hmget (Key, field1, ..., field N): Returns the value

Hmset (Key, field1, value1,..., field N, Value N) of field I in the hash named key : Add an element to a hash named key field I<->value I

hincrby (Key, field, integer): Adds an integer

to the value of field in the hash named key Hexists (Key, field): A hash with the name key for field

Hdel (Key, field): Delete The field with the key field in the hash named key

Hlen (key) : Returns the number of elements in a hash named key

Hkeys (key): Returns all keys Hvals (key) in a hash with the name Key

: Returns the value hgetall for all keys in the hash named key

(key ): Returns all keys (field) and their corresponding value in a hash with the name key
Three, the various data types corresponding to the application scenario 1. Application Scenarios of String type
String is the most common type of data, common key/value storage.
2. List Type Application Scenario

Comparisons apply to List-type storage and are relatively fixed in order, for example:

List of provinces, cities list

brands, manufacturers, cars, models and other lists of

car-dismantling workshops ...
3. Set type Application Scenario
Set provides functionality that is similar to list, when you need to store a list of data, and you do not want duplicate data, you can choose the set
4, Zset (sorted set) type of application scenario
Zset's usage scenario is similar to set, except that the set is not automatically ordered, and Zset can sort the members by providing an additional priority (score) parameter for the user, and is inserted in an ordered, automatic sort. When you need a list of ordered and non-repeating collections, Then you can choose the Zset data structure. Example:

list of popular car models based on PV sorted

by Time
5. Application scenario of hash type
Storage of data required to store

page views similar to table records
Iv. Specific Use reference examples
private void TestKey () {System.out.println ("=============key==========================");
      Emptying Data System.out.println (JEDIS.FLUSHDB ());
      System.out.println (Jedis.echo ("foo"));
      Determine if key is present System.out.println (shardedjedis.exists ("foo"));
      Shardedjedis.set ("Key", "values");
  System.out.println (shardedjedis.exists ("key"));
      } private void TestString () {System.out.println ("=============string==========================");
      Emptying Data System.out.println (JEDIS.FLUSHDB ());
      Store Data shardedjedis.set ("foo", "Bar");
      System.out.println (Shardedjedis.get ("foo"));
      If key does not exist, then store shardedjedis.setnx ("foo", "Foo not exits");
      System.out.println (Shardedjedis.get ("foo"));
      Overwrite data shardedjedis.set ("foo", "foo update");
      System.out.println (Shardedjedis.get ("foo"));
      Append Data shardedjedis.append ("foo", "Hello, World");
      System.out.println (Shardedjedis.get ("foo")); Set key to haveand store Data Shardedjedis.setex ("foo", 2, "Foo not exits");
      System.out.println (Shardedjedis.get ("foo"));
      try {thread.sleep (3000);
      } catch (Interruptedexception e) {} System.out.println (Shardedjedis.get ("foo"));
      Get and change data shardedjedis.set ("foo", "foo update");
      System.out.println (Shardedjedis.getset ("foo", "foo Modify"));
      Intercept value System.out.println (Shardedjedis.getrange ("foo", 1, 3));
      System.out.println (Jedis.mset ("Mset1", "mvalue1", "Mset2", "Mvalue2", "Mset3", "Mvalue3", "Mset4", "mvalue4"));
      System.out.println (Jedis.mget ("Mset1", "Mset2", "Mset3", "Mset4"));
  System.out.println (Jedis.del (new string[] {"foo", "Foo1", "Foo3"});
      } private void Testlist () {System.out.println ("=============list==========================");
      Emptying Data System.out.println (JEDIS.FLUSHDB ());
      Add Data Shardedjedis.lpush ("lists", "vector"); Shardedjedis.lpush ("lists", "ArraylisT ");
      Shardedjedis.lpush ("lists", "LinkedList");
      Array length System.out.println (Shardedjedis.llen ("lists"));
      Sort System.out.println (Shardedjedis.sort ("lists"));
      String System.out.println (Shardedjedis.lrange ("lists", 0, 3));
      Modifies a single value in the list Shardedjedis.lset ("lists", 0, "Hello list!");
      Gets a list that specifies the value of the System.out.println (Shardedjedis.lindex ("lists", 1));
      The Delete list specifies the underlying value System.out.println (Shardedjedis.lrem ("lists", 1, "vector"));
      Delete data outside the interval System.out.println (Shardedjedis.ltrim ("lists", 0, 1));
      List out Stack System.out.println (Shardedjedis.lpop ("lists"));

  The entire list value System.out.println (Shardedjedis.lrange ("lists", 0,-1));
      } private void Testset () {System.out.println ("=============set==========================");
      Emptying Data System.out.println (JEDIS.FLUSHDB ());
      Add Data Shardedjedis.sadd ("Sets", "HashSet");
      Shardedjedis.sadd ("Sets", "SortedSet"); SharDedjedis.sadd ("Sets", "TreeSet");
      Determines whether value is System.out.println in the list (Shardedjedis.sismember ("Sets", "TreeSet"));
      ;
      The entire list value System.out.println (Shardedjedis.smembers ("sets"));
      Deletes the specified element System.out.println (Shardedjedis.srem ("Sets", "SortedSet"));
      Out Stack System.out.println (Shardedjedis.spop ("sets"));
      System.out.println (Shardedjedis.smembers ("sets"));
      Shardedjedis.sadd ("Sets1", "HashSet1");
      Shardedjedis.sadd ("Sets1", "SortedSet1");
      Shardedjedis.sadd ("Sets1", "TreeSet");
      Shardedjedis.sadd ("Sets2", "HashSet2");
      Shardedjedis.sadd ("Sets2", "SortedSet1");
      Shardedjedis.sadd ("Sets2", "TreeSet1");
      Intersection System.out.println (Jedis.sinter ("Sets1", "sets2"));
      and set System.out.println (Jedis.sunion ("Sets1", "sets2")); Difference Set System.out  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.