Redis 中文文档 Redis 中文文档
指南
redis.io (opens new window)
指南
redis.io (opens new window)
  • 关于
    • Redis 开源治理
    • Redis 发布周期
    • Redis 赞助商
  • 入门
  • 数据类型
  • Redis Stack
  • 命令
  • 手册

Jedis


Integration

What is Jedis?


Jedis is a Java client for Redis designed for performance and ease of use.

Are you looking for a high-level library to handle object mapping? See redis-om-spring !

Contributing


We'd love your contributions!

Bug reportsare always welcome! You can open a bug report on GitHub.

You can also contribute documentation-- or anything to improve Jedis. Please see contribution guideline for more details.

Supported Redis versions


The most recent version of this library supports redis version 5.0, 6.0, 6.2, and 7.0.

The table below highlights version compatibility of the most-recent library versions and Redis versions. Compatibility means communication features, and Redis command capabilities.

Library version Supported redis versions
:--- :---
3.9+ 5.0 and 6.2 Family of releases
>= 4.0 Version 5.0 to current

Getting started


To get started with Jedis, first add it as a dependency in your Java project. If you're using Maven, that looks like this:

  1. ``` xml
  2. <dependency>
  3.     <groupId>redis.clients</groupId>
  4.     <artifactId>jedis</artifactId>
  5.     <version>4.3.0</version>
  6. </dependency>
  7. ```

To use the cutting-edge Jedis, check here.

Next, you'll need to connect to Redis. Consider installing a redis-stack docker:

  1. ``` shell
  2. docker run -p 6379:6379 -it redis/redis-stack:latest
  3. ```

For many applications, it's best to use a connection pool. You can instantiate a Jedis connection pool like so:

  1. ``` java
  2. JedisPool pool = new JedisPool("localhost", 6379);
  3. ```

With a JedisPool instance, you can use a try-with-resources block to get a connection and run Redis commands.

Here's how to run a single SET command within a try-with-resourcesblock:

  1. ``` java
  2. try (Jedis jedis = pool.getResource()) {
  3.   jedis.set("clientName", "Jedis");
  4. }
  5. ```

Jedis instances implement most Redis commands. See the Jedis Javadocs for the complete list of supported commands.

Easier way of using connection pool


Using a try-with-resourcesblock for each command may be cumbursome, so you may consider using JedisPooled.

  1. ``` java
  2. JedisPooled jedis = new JedisPooled("localhost", 6379);
  3. ```

Now you can send commands like sending from Jedis.

  1. ``` java
  2. jedis.sadd("planets", "Venus");
  3. ```

Connecting to a Redis cluster


Jedis lets you connect to Redis Clusters, supporting the Redis Cluster Specification. To do this, you'll need to connect using JedisCluster. See the example below:

  1. ``` java
  2. Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
  3. jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
  4. jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7380));
  5. JedisCluster jedis = new JedisCluster(jedisClusterNodes);
  6. ```

Now you can use the JedisCluster instance and send commands like you would with a standard pooled connection:

  1. ``` java
  2. jedis.sadd("planets", "Mars");
  3. ```

Documentation


The Jedis wiki contains several useful articles for using Jedis.

You can also check the latest Jedis Javadocs.

Some specific use-case examples can be found in redis.clients.jedis.examplespackage of the test source codes.

Using Redis modules


Jedis includes support for Redis modules such as RedisJSON and RediSearch.

See the RedisJSON Jedis or RediSearch Jedis for details.

Troubleshooting


If you run into trouble or have any questions, we're here to help!

Hit us up on the Redis Discord Server or open an issue on GitHub.

You can also find help on the Jedis mailing list or the GitHub Discussions.

License


Jedis is licensed under the MIT license.

Sponsorship


Redis Logo
Last Updated: 2023-09-03 19:17:54