Home > Database > Redis > body text

How to use Java to operate Redis database

WBOY
Release: 2023-05-27 17:52:06
forward
1020 people have browsed it

Redis is a memory-based database that interacts with Redis to greatly improve the speed of operation.

First let us create a normal Maven project and add the corresponding dependencies

<dependencies>
		<dependency>
		    <groupId>redis.clients</groupId>
		    <artifactId>jedis</artifactId>
		    <version>3.3.0</version>
		</dependency>
		
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.72</version>
		</dependency>
  </dependencies>
Copy after login

Then we can use

to test the connection (here I use the local reids service)

//定义主机号、端口号
		HostAndPort hostAndPort = new HostAndPort("127.0.0.1",6379);
		//连接redis服务
		Jedis jedis=new Jedis(hostAndPort);
		//ping一下
		System.out.println("服务正在运行: "+jedis.ping())
		//关闭
		jedis.close();
Copy after login
控制台输出PONG代表连接成功
Copy after login

Next let’s look at the basic operations of Redis’s five major data types

  • String type

System.out.println("存入一个数据:"+jedis.set("age", "20"));
		System.out.println("存入多个数据:"+jedis.mset("name","zhangsan","sex","男"));
		
		System.out.println("获取一个数据:"+jedis.get("age"));
		System.out.println("获取多个数据:"+jedis.mget("name","sex"));
		
		System.out.println("将指定的字符串拼接在指定数据之后:"+jedis.append("name",",你好"));
		
		System.out.println("查看某个数据的长度:"+jedis.strlen("name"));
		
		System.out.println("修改某个数据的值并返回修改之前的值:"+jedis.getSet("name", "lisi"));
		
		System.out.println("判断某个数据是否存在:"+jedis.exists("name"));
		
		System.out.println("为某个数据设置失效时间(单位/s):"+jedis.expire("name", 20));
		
		System.out.println("查看某个数据的剩余生存时间(s):"+jedis.ttl("name"));
		
		System.out.println("删除一个或多个数据:"+jedis.del("name","sex"));
Copy after login

View print results

  • Set type

System.out.println("向集合添加一个或多个元素:"+jedis.sadd("key1", "v1","v2","v3"));
		
		System.out.println("获取集合的元素个数:"+jedis.scard("key1"));
		
		System.out.println("返回集合中的所有元素:"+jedis.smembers("key1"));
		
		System.out.println("判断指定元素是否存在集合中:"+jedis.sismember("key1", "v1"));
		
		System.out.println("移除集合中指定的元素:"+jedis.srem("key1", "v3"));
		
		//这里我们在创建一个集合
		System.out.println("向集合添加一个或多个元素:"+jedis.sadd("key2", "v2","v3","v4"));
		
		System.out.println("返回集合key1与key2的差集:"+jedis.sdiff("key1","key2"));
		System.out.println("返回集合key1与key2的交集:"+jedis.sinter("key1","key2"));
		System.out.println("返回集合key1与key2的并集:"+jedis.sunion("key1","key2"));
Copy after login

View results

  • Hash type

	Map map=new HashMap<>();
		map.put("name", "zhangsan");
		map.put("age", "20");
		map.put("sex", "男");
		
		System.out.println("创建一个哈希表存储一个用户对象:"+jedis.hmset("user",map));
		
		System.out.println("获取哈希表中用户的name:"+jedis.hget("user", "name"));
		
		System.out.println("查看哈希表中,指定的字段是否存在:"+jedis.hexists("user", "name"));
		
		System.out.println("获取哈希表中字段的数量:"+jedis.hlen("user"));
		
		System.out.println("获取哈希表中所有字段:"+jedis.hkeys("user"));
		
		System.out.println("获取哈希表中所有字段的值:"+jedis.hvals("user"));
		
		System.out.println("获取在哈希表中的所有字段和值:"+jedis.hgetAll("user"));
		
		System.out.println("删除一个或多个哈希表字段:"+jedis.hdel("user","name","age","sex"));
Copy after login

View results

  • List type

System.out.println("将一个值插入到列表头部(可以多个值):"+jedis.lpush("city","北京","上海"));
		
		
		System.out.println("将一个值插入到列表尾部(可以多个值):"+jedis.rpush("city","济南","南京"));
		
		
		System.out.println("获取列表指定范围内的元素:"+jedis.lrange("city", 0, -1));
		
		
		System.out.println("获取列表长度:"+jedis.llen("city"));
		
		
		System.out.println("移出列表的第一个元素,并输出值:"+jedis.lpop("city"));
		
		
		System.out.println("移除列表的最后一个元素,并输出值:"+jedis.rpop("city"));
		
		
		System.out.println("修改列表中指定索引位置元素的值:"+jedis.lset("city", 0, "西藏"));
		
		
		System.out.println("让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除:"+jedis.ltrim("city", 1, 2));
Copy after login

View results

  • Sorted Set type

Map<String,Double> scoreMembers=new HashMap<>();
		scoreMembers.put("member1", 1D);
		scoreMembers.put("member2", 2D);
		scoreMembers.put("member3", 3D);
		
		System.out.println("向有序集合添加一个或多个元素,或者更新已存在元素的分数:"+jedis.zadd("member", scoreMembers));
		
		System.out.println("获取有序集合的元素个数:"+jedis.zcard("member"));
		
		System.out.println("计算在有序集合中指定区间([socre1,socre2])分数的成员数:"+jedis.zcount("member", 0D, 3D));
		
		System.out.println("通过索引区间返回有序集合指定区间内的元素,从低到高:"+jedis.zrange("member", 0,-1));
		
		System.out.println("返回索引区间返回有序集合指定区间内的元素,从高到低:"+jedis.zrevrange("member", 0,-1));
		
		System.out.println("返回有序集中指定元素的分数值:"+jedis.zscore("member", "member1"));
		
		System.out.println("移除有序集合中的一个或多个元素:"+jedis.zrem("member", "member1","member2"));
		
		System.out.println("返回有序集合中指定元素的索引:"+jedis.zrank("member", "member3"));
Copy after login

View results

Finally we briefly look at how to operate the database

	System.out.println("清空当前数据库:"+jedis.flushDB());
		
		System.out.println("清空所有数据库:"+jedis.flushAll());
		
		System.out.println("查看当前数据库存储数据的多少:"+jedis.dbSize());
		
		Set<String> keys = jedis.keys("*");
		System.out.println("查看当前数据库存储所有键值:"+keys);
		
		System.out.println("选择某个数据库:"+jedis.select(0));
Copy after login

View results

In fact, Redis also has three special storage types

  • Geospatial                                                   

Mainly used to store geographical location information and operate on the stored information, based on Sorts Set ordered collection

  • HyperLogLog

is used for cardinality statistics

  • Bitmap

## records only two states by operating binary (0,1) Information

The above is the detailed content of How to use Java to operate Redis database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!