日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过Jedis API使用排序集

發布時間:2023/12/3 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过Jedis API使用排序集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在上一篇文章中,我們開始研究Jedis API和Java Redis Client。 在本文中,我們將研究Sorted Set(zsets)。

排序集的工作方式類似于集,因為它不允許重復的值。 最大的區別在于,在“排序集”中,每個元素都有一個分數,以便保持元素的排序。

我們可以在下面看到一些命令:

import java.util.HashMap; import java.util.Map;import redis.clients.jedis.Jedis; public class TestJedis {public static void main(String[] args) {String key = "mostUsedLanguages";Jedis jedis = new Jedis("localhost");//Adding a value with score to the setjedis.zadd(key,100,"Java");//ZADD//We could add more than one value in one callingMap<Double, String> scoreMembers = new HashMap<Double, String>();scoreMembers.put(90d, "Python");scoreMembers.put(80d, "Javascript");jedis.zadd(key, scoreMembers);//We could get the score for a memberSystem.out.println("Number of Java users:" + jedis.zscore(key, "Java"));//We could get the number of elements on the setSystem.out.println("Number of elements:" + jedis.zcard(key));//ZCARD} }

在上面的示例中,我們看到了一些Zset命令。 為了將元素添加到zet中,我們設置了zadd方法,不同之處在于我們還傳遞了該元素的得分。 有一個重載版本,我們可以使用映射傳遞許多值。 zadd可用于添加和更新現有元素的分數。

我們可以使用zcard命令使用zscore和元素數量獲得給定元素的分數。

下面我們可以看到zsets的其他命令:

import java.util.Set;import redis.clients.jedis.Jedis; import redis.clients.jedis.Tuple; public class TestJedis {public static void main(String[] args) {String key = "mostUsedLanguages";Jedis jedis = new Jedis("localhost");//get all the elements sorted from bottom to topSystem.out.println(jedis.zrange(key, 0, -1));//get all the elements sorted from top to bottomSystem.out.println(jedis.zrevrange(key, 0, -1));//We could get the elements with the associated scoreSet<Tuple> elements = jedis.zrevrangeWithScores(key, 0, -1);for(Tuple tuple: elements){System.out.println(tuple.getElement() + "-" + tuple.getScore());}//We can increment a score for a element using ZINCRBYSystem.out.println("Score before zincrby:" + jedis.zscore(key, "Python"));//Incrementing the element scorejedis.zincrby(key, 1, "Python");System.out.println("Score after zincrby:" + jedis.zscore(key, "Python"));} }

使用zrange,我們可以獲取給定范圍的元素。 它返回從下到上排序的元素。 我們可以使用zrevrrange方法從上到下獲取元素。 Redis還允許我們獲取具有相關分數的元素。 在redis中,我們傳遞選項“ withscores ”。 通過Jedis API,我們使用方法zrevrangeWithScores返回一個元組對象集。 其他有用的命令是zincrby ,我們可以增加集合中某個成員的分數。

zsets還有其他命令,本文僅旨在顯示Jedis API的一些基本用法。 我們可以在這篇文章中找到排序集的好用例。

下篇再見。

參考: XICO JUNIOR'S WEBLOG博客中JCG合作伙伴 Francisco Ribeiro Junior的使用帶有Jedis API的排序集 。

翻譯自: https://www.javacodegeeks.com/2013/11/using-sorted-sets-with-jedis-api.html

總結

以上是生活随笔為你收集整理的通过Jedis API使用排序集的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。