用于多对多关系的Redis数据建模

时间:2018-11-11 17:51:32

标签: spring redis jedis spring-data-redis

查看链接-how to have relations many to many in redis。我尝试使用Spring Data Redis HashRedis类对此进行建模。此示例模型....

# Here are my categories
> hmset category:1 name cinema  ... more fields ...
> hmset category:2 name music   ... more fields ...
> hmset category:3 name sports  ... more fields ...
> hmset category:4 name nature  ... more fields ...

# Here are my users
> hmset user:1 name Jack   ... more fields ...
> hmset user:2 name John   ... more fields ...
> hmset user:3 name Julia  ... more fields ...

# Let's establish the many-to-many relationship
# Jack likes cinema and sports
# John likes music and nature
# Julia likes cinema, music and nature

# For each category, we keep a set of reference on the users
> sadd category:1:users 1 3
> sadd category:2:users 2 3
> sadd category:3:users 1
> sadd category:4:users 2 3

# For each user, we keep a set of reference on the categories
> sadd user:1:categories 1 3
> sadd user:2:categories 2 4
> sadd user:3:categories 1 2 4

一旦有了这种数据结构,就很容易使用集合代数查询它:

朱莉娅的类别

> smembers user:3:categories
1) "1"
2) "2"
3) "4"

# Users interested by music
> smembers category:2:users
1) "2"
2) "3"

# Users interested by both music and cinema
> sinter category:1:users category:2:users
1) "3"

User.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@RedisHash("users")
public class User {
    @Id
    private String userId;
    private String firstName;
    private String emailId;
    private List<Category> categories;
}

Category.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@RedisHash("category")
public class Category {
    @Id
    private String categoryId;
    private String name;
    private String type;

    private List<User> users;
}

RedisExampleBootApplication.java

@SpringBootApplication
public class RedisExampleBootApplication implements CommandLineRunner{
    @Autowired CategoryRepository categoryRepository;
    @Autowired UserRepository userRepository;

    public static void main(String[] args) {
        SpringApplication.run(RedisExampleBootApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {  

        User jack = User.builder().firstName("Jack").emailId("jack@gmail.com").build();
        User john = User.builder().firstName("John").emailId("john@gmail.com").build();
        User julia  = User.builder().firstName("Julia ").emailId("julia @gmail.com").build();

        Category cinema = Category.builder().name("Cinema").type("Entertainment").users(Arrays.asList(jack, julia)).build();
        Category sports = Category.builder().name("Sports").type("Play Game").users(Arrays.asList(jack)).build();

        Category music = Category.builder().name("Music").type("Sounds").users(Arrays.asList(john, julia)).build();
        Category nature = Category.builder().name("Nature").type("Wild Life").users(Arrays.asList(john, julia)).build();

        categoryRepository.save(cinema);
        categoryRepository.save(sports);
        categoryRepository.save(music);
        categoryRepository.save(nature);



        Category myCinema = Category.builder().name("Cinema").name("Entertainment").build();
        Category mySport = Category.builder().name("Sports").name("Play Game").build();
        Category myMusic = Category.builder().name("Music").name("Sound").build();
        Category myNature = Category.builder().name("Nature").name("Wild Life").build();


        User myJack = User.builder().firstName("Jack").emailId("jack@gmail.com").categories(Arrays.asList(myCinema, mySport)).build();
        User myJohn = User.builder().firstName("John").emailId("john@gmail.com").categories(Arrays.asList(myMusic, myNature)).build();
        User myJulia  = User.builder().firstName("Julia ").emailId("julia @gmail.com").categories(Arrays.asList(myCinema,myMusic, myNature)).build();

        userRepository.save(myJack);
        userRepository.save(myJohn);
        userRepository.save(myJulia);
    }
}

这是数据建模的结果

127.0.0.1:6379> KEYS *
1) "category:0bcba339-9a3e-46e3-b33c-877f8d15595f"
2) "category:4d2b8d10-83de-41fa-8a33-93f30a6f9ffc"
3) "category:f756f18c-53e4-4a20-9a42-ad08b537f380"
4) "category"
5) "users:644d0adc-f0cf-4ba1-8d6b-d6f47145e5e7"
6) "users:8aa5c578-4e05-4ae9-8f80-d10b789e0877"
7) "users:825ab11b-803a-4e4f-a72d-e6b42b98007c"
8) "category:54a56102-5836-41bf-86a8-c82b3f12d3cf"
9) "users"
127.0.0.1:6379>

我没有在任何地方使用@Indexed,因为当前我没有使用任何findBy方法,但这不是问题。我只需要确认如何对数据建模?还是POJO建模正确?

  

HGETALL用户:8aa5c578-4e05-4ae9-8f80-d10b789e0877

userId 8aa5c578-4e05-4ae9-8f80-d10b789e0877
_class com.example.model.User
firstName Jack
emailId jack@gmail.com
categories.[1].name Play Game
categories.[0].name Entertainment

2 个答案:

答案 0 :(得分:1)

我在同一链接上发布了另一个答案。由于我尚未收到任何专家的任何评论。如果有人这样做,我会非常有帮助。

我使模型类保持如此简单,并刚刚创建了一个密钥来保存它们之间的关系。我需要Redis专家们的指导,以继续进行下去。

这很简单,我以为命令在做什么,我们也在做同样的事情。

// Category Details
Category c1 = Category.builder().id("c1").name("Cinema").build();
Category c2 = Category.builder().id("c2").name("Sports").build();
Category c3 = Category.builder().id("c3").name("Music").build();
Category c4 = Category.builder().id("c4").name("Nature").build();

redisTemplate.opsForSet().add("category:1", new ObjectMapper().writeValueAsString(c1));
redisTemplate.opsForSet().add("category:2", new ObjectMapper().writeValueAsString(c2));
redisTemplate.opsForSet().add("category:3", new ObjectMapper().writeValueAsString(c3));
redisTemplate.opsForSet().add("category:4", new ObjectMapper().writeValueAsString(c4));

// User 
User u1 = User.builder().id("u1").firstName("Chris").emailId("chris.rogers@gmail.com").build(); //1
User u2 = User.builder().id("u2").firstName("John").emailId("john.doe@gmail.com").build();  //2
User u3 = User.builder().id("u3").firstName("Julia").emailId("julia.cox@gmail.com").build(); //3

redisTemplate.opsForSet().add("user:1", new ObjectMapper().writeValueAsString(u1));
redisTemplate.opsForSet().add("user:2", new ObjectMapper().writeValueAsString(u2));
redisTemplate.opsForSet().add("user:3", new ObjectMapper().writeValueAsString(u3));

redisTemplate.opsForSet().add("category:1:users", "1","3");
redisTemplate.opsForSet().add("category:2:users", "2","3");
redisTemplate.opsForSet().add("category:3:users", "1");
redisTemplate.opsForSet().add("category:4:users", "2","3");

redisTemplate.opsForSet().add("user:1:categories", "1","3");
redisTemplate.opsForSet().add("user:2:categories", "2", "4" );
redisTemplate.opsForSet().add("user:3:categories", "1", "2", "4");

enter image description here

注意:您可以使用UUID动态管理密钥。

答案 1 :(得分:0)

我能够解决这个问题。您只需将关系保持在单独的@RedishHash类中,然后提供PKPK中没有Redis,而是说unique key)并使用Category中的PK和来自用户的PK,并用@Indexed进行注释,以便您可以使用Repository模式进行自定义搜索。

通过这种方式,您只维护单个Categories的{​​{1}}列表,还维护单个User的{​​{1}}。只是在这里使用了单个映射。

就像为Users保存数据时一样,为该category保存Category例如-假设userCategory感兴趣,User-1Category-1感兴趣,User-2

感兴趣
Category-1 and 2

完成!这很好。这是仅使用User-3模式而没有Category-3 Category-1 save User-1 Category-1 save User-2 Category-2 save User-2 Category-2 save User-3 的另一种方法。