Using Redis for Java

Source: Internet
Author: User
Tags redis tutorial

Installation

Before we start using Redis in Java, we need to make sure that we have the Redis service and the Java Redis driver installed and that Java is working properly on your machine. Java installation configuration can refer to our Java Development environment configuration next let's install the Java Redis driver:

    • First you need to download the driver package, download the Jedis.jar, and make sure to download the latest driver package.
    • Include the driver package in your classpath.
Connect to the Redis service
  1. Import Redis. Clients. Jedis. Jedis;
  2. Public class Redisjava {
  3. public static void main(String[] args) {
  4. //Connect to local Redis service
  5. Jedis Jedis = new Jedis("localhost");
  6. System. Out. println("Connection to Server sucessfully");
  7. //See if the service is running
  8. System. Out. println("Server is running:"+Jedis. Ping());
  9. }
  10. }

Compile the above Java program to ensure that the path to the driver package is correct.

    1. $javac Redisjava. Java
    2. $java Redisjava
    3. Connection to server sucessfully
    4. Server is running: PONG
    5. Redis Java String Example
Redis Java String (string) instance
  1. Import Redis. Clients. Jedis. Jedis;
  2. Public class Redisstringjava {
  3. public static void main(String[] args) {
  4. //Connect to local Redis service
  5. Jedis Jedis = new Jedis("localhost");
  6. System. Out. println("Connection to Server sucessfully");
  7. //Set Redis string data
  8. Jedis. Set("W3ckey", "Redis Tutorial");
  9. //Get stored data and output
  10. System. Out. println("Stored string in Redis::"+ Jedis. Get("W3ckey"));
  11. }
  12. }

Compile the above program.

    1. $javac Redisstringjava. Java
    2. $java Redisstringjava
    3. Connection to server sucessfully
    4. Stored string in redis:: redis tutorial
Redis Java list (list) instance
  1. Import Redis. Clients. Jedis. Jedis;
  2. Public class Redislistjava {
  3. public static void main(String[] args) {
  4. //Connect to local Redis service
  5. Jedis Jedis = new Jedis("localhost");
  6. System. Out. println("Connection to Server sucessfully");
  7. //Store data in the list
  8. Jedis. Lpush("Tutorial-list", "Redis");
  9. Jedis. Lpush("Tutorial-list", "Mongodb");
  10. Jedis. Lpush("Tutorial-list", "Mysql");
  11. //Get stored data and output
  12. list<string > list = Jedis.lrange (, Span class= "lit" >0 ,5
  13. for(int i=0; i<list. Size(); i+ +) {
  14. System. Out. println("Stored string in Redis::"+list. ) Get(i));
  15. }
  16. }
  17. }

Compile the above program.

  1. $javac Redislistjava. Java
  2. $java Redislistjava
  3. Connection to server sucessfully
  4. Stored string in redis:: redis
  5. Stored string in redis:: Mongodb
  6. Stored string in redis:: Mysql
Redis Java Keys Instance
  1. Import Redis. Clients. Jedis. Jedis;
  2. Public class Rediskeyjava {
  3. public static void main(String[] args) {
  4. //Connect to local Redis service
  5. Jedis Jedis = new Jedis("localhost");
  6. System. Out. println("Connection to Server sucessfully");
  7. //Get data and output
  8. list<String> list = Jedis. Keys("*");
  9. for(int i=0; i<list. Size(); i+ +) {
  10. System. Out. println("List of stored keys::"+List. ) Get(i));
  11. }
  12. }
  13. }

Compile the above program.

    1. $javac Rediskeyjava. Java
    2. $java Rediskeyjava
    3. Connection to server sucessfully
    4. List of stored keys:: Tutorial-name
    5. List of stored keys:: Tutorial-list

Using Redis for Java

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.