Redis Java Connection using __java

Source: Internet
Author: User
Tags redis redis server

Description

After the Redis stand-alone deployment is complete, developers need to perform some business operations on the Java Client Connection Redis, the following are some of the basic Java Operational code:


Package com;


Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;


Import Redis.clients.jedis.Jedis;


/**
* @ClassName: Jedistest
* @Description: Redis stand-alone Test
* @author Admin
* @version V1.0
*/
public class Jredistest {
Private Jedis Jedis;
Public Jedis Getjedis () {
return Jedis;
}
public void Setjedis (Jedis Jedis) {
This.jedis = Jedis;
}



public void Setup () {
Connect Redis Server, 192.168.0.100:6379
Jedis = new Jedis ("172.168.10.250", 6379);
Authority authentication
Jedis.auth ("123456");
}




Test
public static void Main (string[] args) {


try {
Jredistest test = new Jredistest ();
Test.setup ();
SYSTEM.OUT.PRINTLN ("Connection successful!");
Test.teststring ();
Test.testlist ();
Test.testmap ();
Test.testset ();
Test.testsortedset ();
catch (Exception e) {
E.printstacktrace ();
}
}


/**
* Redis Storage string
*/


public void teststring () {
-----Add Data----------
SYSTEM.OUT.PRINTLN ("-----Add data---------string-");
Jedis.set ("name", "Honghong"), and/or into the key-->name into the Value-->xinxin
System.out.println (Jedis.get ("name"));/Execution Result: xinxin


Jedis.append ("name", "Is My Lover"); Stitching
System.out.println (Jedis.get ("name"));


Jedis.del ("name"); Delete a key
System.out.println (Jedis.get ("name"));


Set multiple key-value pairs
Jedis.mset ("name", "LiuLing", "Age", "the", "QQ", "476777389");
JEDIS.INCR ("Age"); Add 1 to the operation
System.out.println (Jedis.get ("name") + "-" + jedis.get ("age") + "-" + jedis.get ("QQ"));


}


/**
* Redis Operation Map
*/


public void TestMap () {
-----Add Data----------
SYSTEM.OUT.PRINTLN ("-----Add data---------hash-");
map<string, string> map = new hashmap<string, string> ();
Map.put ("name", "Xinxin");
Map.put ("Age", "22");
Map.put ("QQ", "123456");


Map.put ("name", "Xinxin2");
Map.put ("Age", "22");
Map.put ("QQ", "123456");


Jedis.hmset ("user", map);
Remove name from user and perform result:[minxr]--> note the result is a generic list
The first parameter is the key of the map object in Redis, followed by the key of the object in the map, followed by the key can be followed by multiple, is a variable parameter
list<string> Rsmap = jedis.hmget ("User", "name", "Age", "QQ");
System.out.println (RSMAP);


Delete a key value from the map
Jedis.hdel ("User", "age");
System.out.println (Jedis.hmget ("User", "age")); The return is NULL because it was deleted
System.out.println (Jedis.hlen ("user")); Returns the number of values stored in the key for user 2
System.out.println (jedis.exists ("user"));/whether there is a record with key of user returns True
System.out.println (Jedis.hkeys ("user"))//Return all keys in the Map object
System.out.println (jedis.hvals ("user"))//Return all value in the Map object


Iterator<string> iter = Jedis.hkeys ("user"). iterator ();
while (Iter.hasnext ()) {
String key = Iter.next ();
SYSTEM.OUT.PRINTLN (key + ":" + jedis.hmget ("user", key));
}
}


/**
* Jedis Operation List
*/


public void Testlist () {
SYSTEM.OUT.PRINTLN ("-----Add data---------list-");
Before you start, remove all the content
Jedis.del ("Java framework");
System.out.println (Jedis.lrange ("Java framework", 0,-1));
First store three data in the key Java framework
Jedis.lpush ("Java framework", "Spring");
Jedis.lpush ("Java framework", "struts");
Jedis.lpush ("Java framework", "Hibernate");
And then remove all the data jedis.lrange is removed by range,
The first is the key, the second is the starting position, the third is the end position, and the jedis.llen gets the length-1 means to get all
System.out.println (Jedis.lrange ("Java framework", 0,-1));


Jedis.del ("Java framework");
Jedis.rpush ("Java framework", "Spring");
Jedis.rpush ("Java framework", "struts");
Jedis.rpush ("Java framework", "Hibernate");
System.out.println (Jedis.lrange ("Java framework", 0,-1));
}


/**
* Jedis Operation Set
*/


public void Testset () {
SYSTEM.OUT.PRINTLN ("-----Add data---------set-");
Add to
Jedis.sadd ("Userset", "Moqingxiong");
Jedis.sadd ("Userset", "xinxin");
Jedis.sadd ("Userset", "Ling");
Jedis.sadd ("Userset", "zhangxinxin");
Jedis.sadd ("Userset", "who");
Remove Noname
Jedis.srem ("Userset", "who");
System.out.println (Jedis.smembers ("Userset"));//Get all added value
System.out.println (Jedis.sismember ("Userset", "who"))/judge who
is the element of the user collection
System.out.println (Jedis.srandmember ("Userset"));
System.out.println (Jedis.scard ("Userset"))//Returns the number of elements in the collection
}




/**
* Jedis operation Sorted Set
*/
private void Testsortedset () {
-----Add Data----------
SYSTEM.OUT.PRINTLN ("-----Add data---------Sorted set-");
Emptying data
System.out.println (Jedis.flushdb ());
Add data
Jedis.zadd ("Zset", 10.1, "Hello");
Jedis.zadd ("Zset", 10.0, ":");
Jedis.zadd ("Zset", 9.0, "Zset");
Jedis.zadd ("Zset", 11.0, "zset!");
Number of elements
System.out.println (Jedis.zcard ("Zset"));
Element subscript
System.out.println (Jedis.zscore ("Zset", "Zset"));
Set subset
System.out.println (Jedis.zrange ("Zset", 0,-1));
Delete Element
System.out.println (Jedis.zrem ("Zset", "zset!"));
System.out.println (Jedis.zcount ("Zset", 9.5, 10.5));
Entire Set value
System.out.println (Jedis.zrange ("Zset", 0,-1));
}


public void Test () throws Interruptedexception {
Jedis sort
Note that the Rpush and Lpush here are the operations of the list. is a doubly linked list (but from the performance perspective)
Jedis.del ("a")//Clear data first, then add data to test
Jedis.rpush ("A", "1");
Jedis.lpush ("A", "6");
Jedis.lpush ("A", "3");
Jedis.lpush ("A", "9");
System.out.println (Jedis.lrange ("A", 0,-1));//[9, 3, 6, 1]
System.out.println (Jedis.sort ("a")); [1, 3, 6, 9]//input sort result
System.out.println (Jedis.lrange ("A", 0,-1));
}




}

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.