Redis Java connection operation connected to 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
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));}}}
Now, let's compile and run the above program.
$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
Redis Java Operations