Using Redis with Java
Installation
Before starting to use Redis in Java, we need to ensure that the redis service and the Java redis driver are installed, and that Java works properly on your machine. For Java installation and configuration, please refer to our Java Development Environment Configuration Next, let us install the Java redis driver:
- First, you need to download the driver package Download jedis.jar, making sure to download the latest driver package.
- Include the driver package in your classpath.
This site provides version 2.9.0 jar for download: jedis-2.9.0.jar
Connect to the redis service
Example
import
redis
.
clients
.
jedis
.
Jedis
;
public
class
RedisJava
{
public
static
void
main
(
String
[
]
args
)
{
//
Connect to the local Redis service
Jedis
jedis
=
new
Jedis
(
"
localhost
"
)
;
//
If the Redis service has a password set, you need the line below; otherwise it is not needed
//
jedis.auth("123456");
System
.
out
.
println
(
"
Connection successful
"
)
;
//
Check whether the service is running
System
.
out
.
println
(
"
Service is running:
"
jedis
.
ping
(
)
)
;
}
}
Compile the Java program above, making sure the path to the driver package is correct.
连接成功
服务正在运行: PONG
Redis Java String Example
Example
import
redis
.
clients
.
jedis
.
Jedis
;
public
class
RedisStringJava
{
public
static
void
main
(
String
[
]
args
)
{
//
Connect to the local Redis service
Jedis
jedis
=
new
Jedis
(
"
localhost
"
)
;
System
.
out
.
println
(
"
Connection successful
"
)
;
//
Set redis string data
jedis
.
set
(
"
runoobkey
"
,
"
www.runoob.com
"
)
;
//
Get the stored data and output it
System
.
out
.
println
(
"
The string stored in redis is:
"
jedis
.
get
(
"
runoobkey
"
)
)
;
}
}
Compile the program above.
连接成功
redis 存储的字符串为: www.runoob.com
Redis Java List Example
Example
import
java
.
util
.
List
;
import
redis
.
clients
.
jedis
.
Jedis
;
public
class
RedisListJava
{
public
static
void
main
(
String
[
]
args
)
{
//
Connect to the local Redis service
Jedis
jedis
=
new
Jedis
(
"
localhost
"
)
;
System
.
out
.
println
(
"
Connection successful
"
)
;
//
Store data into the list
jedis
.
lpush
(
"
site-list
"
,
"
Runoob
"
)
;
jedis
.
lpush
(
"
site-list
"
,
"
"
)
;
jedis
.
lpush
(
"
site-list
"
,
"
Taobao
"
)
;
//
Get the stored data and output it
List
<
String
list
=
jedis.
lrange
(
"
site-list
"
,
0
,
2
)
;
for
(
int
i
=
0
;
i
<
list
.
size
(
)
;
i
++
)
{
System
.
out
.
println
(
"
List items are:
"
list
.
get
(
i
)
)
;
}
}
}
Compile the program above.
连接成功
列表项为: Taobao
列表项为: Google
列表项为: Runoob
Redis Java Keys Example
Example
import
java
.
util
.
Iterator
;
import
java
.
util
.
Set
;
import
redis
.
clients
.
jedis
.
Jedis
;
public
class
RedisKeyJava
{
public
static
void
main
(
String
[
]
args
)
{
//
Connect to the local Redis service
Jedis
jedis
=
new
Jedis
(
"
localhost
"
)
;
System
.
out
.
println
(
"
Connection successful
"
)
;
//
Get the data and output it
Set
<
String
keys
=
jedis.
keys
(
"
"
)
;
Iterator
<
String
it
=
keys.
iterator
(
)
;
while
(
it
.
hasNext
(
)
)
{
String
key
=
it
.
next
(
)
;
System
.
out
.
println
(
key
)
;
}
}
}
Compile the program above.
连接成功
runoobkey
site-list