在Spring Data Redis中是否有@Query?

huangapple go评论58阅读模式
英文:

Is there a @Query in spring Data Redis?

问题

我一直在阅读关于Spring Data Redis的内容,它与Spring Data JPA非常相似。
我有一个USER类,其键为用户ID,值为User对象。我正在尝试从存储这些数据的"/user"哈希中查找所有用户ID(键)。

Spring Data有一个很酷的特性,可以使用@Query,但显然在Spring Data Redis中无法工作。

@Query("Select c.key from User c")
Set<String> getAllUserKey();

我尝试了上面的代码,但它给我报错,说未识别属性Key。我已经查阅了文档和Google,我认为在spring-data redis中不能用这种方式实现。

只是想确认我的理解,还想了解一下处理这种情况的最佳方法是什么。我想收集关于在哈希下存储了多少键以及其他信息的统计数据,这在使用@Query时会非常容易实现。

英文:

I have been reading about spring Data Redis which is quite similar to spring data JPA.
I have a USER class that has key has user id and value as the User object. I am trying to find all the user id's (key) from the "/user" hash where this data is stored.

Spring data had this cool feature of using @Query which is apparently not working with spring data redis.

@Query(&quot;Select c.key from User c&quot;)
Set&lt;String&gt; getAllUserKey();

I have tried the above code but it is giving me error that unrecognized property Key. I have gone through the documentation and google and don't think so this can be done using spring-data redis.

Just wanted to confirm my understanding and also would like to know what is the best way to approach this use case. I want to gather statistics around how many keys are stored under a hash and other things which would have been very easy to do with @Query

答案1

得分: 1

我认为你可以使用 redisTemplate 和 redisTemplate.keys(" * ")。

英文:

I think you can use redisTemplate and redisTemplate.keys("*")

答案2

得分: 1

在Spring Data Redis中,搜索功能仅限于在Redis SETs中创建/维护的二级索引上进行搜索。Spring Data Redis提供了“自动实现仓库接口,包括支持自定义查询方法”的功能。因此,您可以使用简单的存储库支持的存储库搜索方法,例如findByXXXandYYY

要获得完整的搜索功能,Redis引入了RediSearch(https://redis.io/docs/stack/search/quick_start/),这是一个与Redis Stack(https://redis.io/docs/stack/)打包在一起的搜索引擎,可用于在Redis中存储的HASH和JSON文档上进行搜索。

要从Spring中使用Redis Stack功能,可以使用Redis OM Spring(https://github.com/redis/redis-om-spring)。这是一个扩展Spring Data Redis以支持Redis Stack功能的库。尽管仍在早期开发阶段,但接近1.0版本发布(免责声明:我是该库的主要贡献者)。

使用OM,您可以为集合(Hash/JSON)创建搜索索引,并使用存储库方法进行更复杂的搜索,或者使用EntityStream类以类似Java流的API进行搜索。

英文:

No, in Spring Data Redis, the searching capability is limited to searching over secondary indices (created/maintained in Redis SETs). Spring Data Redis provides "Automatic implementation of Repository interfaces including support for custom query methods." So you can do simple repository-powered repo search methods like findByXXXandYYY.

To get full-search capabilities, Redis introduces RediSearch (https://redis.io/docs/stack/search/quick_start/), a search engine packaged with Redis Stack (https://redis.io/docs/stack/) that enables searching over HASHes and JSON documents stored in Redis.

To use Redis Stack features from Spring, there is Redis OM Spring (https://github.com/redis/redis-om-spring). A library that extends Spring Data Redis with Redis Stack functionality. It's still early in its development but getting close to a 1.0 release (disclaimer: I'm the main contributor to the library).

With OM you can create search indices for a collection (Hash/JSON) and user repositories methods to do more complex searching, or use the EntityStream class to search with a Java-streams-like API.

huangapple
  • 本文由 发表于 2023年5月30日 00:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定