Installation
To use Redis in a Java program, you need to make sure that there are redis Java drivers and Java settings on the machine. You can check out the Java tutorial-Learn how to install Java on your machine. Now, let's look at how to set up a Redis Java driver.
Connect to a Redis server
Import Redis.clients.jedis.jedis;public class Redisjava {public static void main (string[] args) {//connecting to Redis server on localhost Jedis jedis = new Jedis ("localhost"); System.out.println ("Connection to Server sucessfully"); Check whether server is running or not SYSTEM.OUT.PRINTLN ("server is running:" +jedis.ping ());}}
Now, let's compile and run the program above to test the connection to the Redis server. You can change the path according to the actual situation. This assumes that the current version of Jedis.jar is available in the current path
$javac Redisjava.java$java redisjavaconnection to server sucessfullyserver is Running:pong
Redis and Java string instances
Import redis.clients.jedis.jedis;public class redisstringjava { public static void main (String[] args) { //connecting to redis server on localhost jedis jedis = new jedis ("localhost"); system.out.println ("Connection to server sucessfully "); //set the data in redis string jedis.set ("Tutorial-name", "Redis Tutorial "); // get the stored data and print It system.out.println ("stored string in redis:: " + Jedis.get ("Tutorial-name")); }}
Now, let's compile and run the above program.
$javac Redisstringjava.java$java redisstringjavaconnection to server sucessfullystored string in Redis:: Redis Tutorial
Examples of Redis and Java column representations
Import redis.clients.jedis.jedis;public class redislistjava { public static void main (String[] args) { //connecting to redis server on localhost jedis jedis = new jedis ("localhost"); system.out.println ("Connection to server sucessfully "); //store data in Redis list jedis.lpush ("Tutorial-list", "Redis"); jedis.lpush ("Tutorial-list", "Mongodb"); Jedis.lpush ("Tutorial-list", "Mysql"); // get the stored data and print it List<String> list = Jedis.lrange ("Tutorial-list", 0 ,5); for (Int i=0; i<list.size (); i++) { system.out.println ("stored string in redis:: " +list.get (i)); } }}
$javac Redislistjava.java$java redislistjavaconnection to server sucessfullystored string in Redis:: redisstored string I N Redis:: mongodbstored string in Redis:: Mysql
Key instances for Redis and Java
import redis.clients.jedis.jedis;public class rediskeyjava { public Static void main (String[] args) { //connecting to redis server on localhost jedis jedis = new jedis ("localhost"); system.out.println ("Connection to server sucessfully "); //store data in redis list // get the stored data and print it list<string> list = jedis.keys ("*"); for (Int i=0; i<list.size (); i++) { System.out.println ("list of stored keys:: " +list.get (i)); } }}
Now, let's compile and run the above program.
$javac Rediskeyjava.java$java rediskeyjavaconnection to server sucessfullylist of stored keys:: Tutorial-namelist of Stor Ed Keys:: Tutorial-list
This article is from the "Wind Trace _ Snow Tiger" blog, please be sure to keep this source http://snowtiger.blog.51cto.com/12931578/1932727
Redis series--6, Redis Java connection operation