Original address: http://www.yiibai.com/redis/redis_java.html
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.
- Need to download Jedis.jar. Be sure to download the latest version of it.
- You need to include Jedis.jar into your classpath.
Connect to a Redis server
ImportRedis.clients.jedis.Jedis; Public classRedisjava { Public Static voidMain (string[] args) {//connecting to Redis server on localhostJedis Jedis =NewJedis ("localhost"); System.out.println ("Connection to Server sucessfully"); //Check whether server is running or notSystem.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
ImportRedis.clients.jedis.Jedis; Public classRedisstringjava { Public Static voidMain (string[] args) {//connecting to Redis server on localhostJedis Jedis =NewJedis ("localhost"); System.out.println ("Connection to Server sucessfully"); //set the data in Redis stringJedis.set ("Tutorial-name", "Redis Tutorial"); //Get the stored data and print itSystem.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
ImportRedis.clients.jedis.Jedis; Public classRedislistjava { Public Static voidMain (string[] args) {//connecting to Redis server on localhostJedis Jedis =NewJedis ("localhost"); System.out.println ("Connection to Server sucessfully"); //store data in Redis listJedis.lpush ("Tutorial-list", "Redis"); Jedis.lpush ("Tutorial-list", "Mongodb"); Jedis.lpush ("Tutorial-list", "Mysql"); //Get the stored data and print itlist<string> list = Jedis.lrange ("Tutorial-list", 0, 5); for(inti=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
ImportRedis.clients.jedis.Jedis; Public classRediskeyjava { Public Static voidMain (string[] args) {//connecting to Redis server on localhostJedis Jedis =NewJedis ("localhost"); System.out.println ("Connection to Server sucessfully"); //store data in Redis list//Get the stored data and print itlist<string> list = Jedis.keys ("*"); for(inti=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 stored keys:: Tutorial-list
Redis Java Connection operation (RPM)