How to model Data in Redis for values Complex Data structure?












3














I've taken a reference of the link : http://panuoksala.blogspot.com/2015/09/redis-many-to-many.html to developed below code. I've implemented some code, looks like nothing is achievable so far as



Get User 1 groups:



hget User:1 Groups


Does not yeild the results.



I wanted to get answer more from the Data Modelling perspectives. I've modeled code like below, but its not working as per my requirement -



Group.java



@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("groups")
public class Group {
@Id
private Long groupId;
private String name;

private List<User> users;
}


And User.java



@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("users")
public class User {
@Id
private Long userId;
private String name;
List<Group> groups;
}


UserGroupTest.java



@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserGroupTest extends RepositoryTestSupport{
@Autowired UserRepository userRepository;
@Autowired GroupRepository groupRepository;

@Before
public void setUp() {

// User -> Group relations
Group group1 = Group.builder().groupId(1L).name("Nature-Group").build();
Group group2 = Group.builder().groupId(2L).name("Music-Group").build();
Group group3 = Group.builder().groupId(3L).name("Sports-Group").build();

User user1 = User.builder().userId(1L).name("John").groups(Arrays.asList(group1, group2)).build();
User user2 = User.builder().userId(2L).name("Sally").groups(Arrays.asList(group2, group3)).build();
User user3 = User.builder().userId(3L).name("Chris").groups(Arrays.asList(group3)).build();

userRepository.save(user1);
userRepository.save(user2);
userRepository.save(user3);


// second contains Group -> User
User myuser1 = User.builder().userId(1L).name("John").build();
User myuser2 = User.builder().userId(2L).name("Sally").build();
User myuser3 = User.builder().userId(3L).name("Chris").build();

Group mygroup1 = Group.builder().groupId(1L).name("Nature-Group").users(Arrays.asList(myuser1)).build();
Group mygroup2 = Group.builder().groupId(2L).name("Music-Group").users(Arrays.asList(myuser1, myuser2)).build();
Group mygroup3 = Group.builder().groupId(3L).name("Sports-Group").users(Arrays.asList(myuser2, myuser3)).build();

groupRepository.save(mygroup1);
groupRepository.save(mygroup2);
groupRepository.save(mygroup3);
}

@Test
public void test() {

}
}


To solve this problem we can use two different sets to keep track of relations.



First we store users and groups into sets.
Then we store relations into two separate hash "structures". First one contains User -> Group relations and second contains Group -> User relations.



enter image description here



127.0.0.1:6379> HGET users:1 groups
(nil)
127.0.0.1:6379> HGET users:1 Groups
(nil)
127.0.0.1:6379> HGET users:1 Group
(nil)


How to model the POJO classes so that result of all this can be achieved ?



hmset User:1 Groups 1,2  (Store user to database)
hmset Group1 Users 1 (Store group to database)

Get User 1 groups:
hget User:1 Groups

Remove user from group:
hmset User:1 Group ""
hmset Group:1 Users: ""


?










share|improve this question
























  • jira.spring.io/browse/DATAREDIS-893
    – PAA
    Nov 21 at 18:18
















3














I've taken a reference of the link : http://panuoksala.blogspot.com/2015/09/redis-many-to-many.html to developed below code. I've implemented some code, looks like nothing is achievable so far as



Get User 1 groups:



hget User:1 Groups


Does not yeild the results.



I wanted to get answer more from the Data Modelling perspectives. I've modeled code like below, but its not working as per my requirement -



Group.java



@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("groups")
public class Group {
@Id
private Long groupId;
private String name;

private List<User> users;
}


And User.java



@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("users")
public class User {
@Id
private Long userId;
private String name;
List<Group> groups;
}


UserGroupTest.java



@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserGroupTest extends RepositoryTestSupport{
@Autowired UserRepository userRepository;
@Autowired GroupRepository groupRepository;

@Before
public void setUp() {

// User -> Group relations
Group group1 = Group.builder().groupId(1L).name("Nature-Group").build();
Group group2 = Group.builder().groupId(2L).name("Music-Group").build();
Group group3 = Group.builder().groupId(3L).name("Sports-Group").build();

User user1 = User.builder().userId(1L).name("John").groups(Arrays.asList(group1, group2)).build();
User user2 = User.builder().userId(2L).name("Sally").groups(Arrays.asList(group2, group3)).build();
User user3 = User.builder().userId(3L).name("Chris").groups(Arrays.asList(group3)).build();

userRepository.save(user1);
userRepository.save(user2);
userRepository.save(user3);


// second contains Group -> User
User myuser1 = User.builder().userId(1L).name("John").build();
User myuser2 = User.builder().userId(2L).name("Sally").build();
User myuser3 = User.builder().userId(3L).name("Chris").build();

Group mygroup1 = Group.builder().groupId(1L).name("Nature-Group").users(Arrays.asList(myuser1)).build();
Group mygroup2 = Group.builder().groupId(2L).name("Music-Group").users(Arrays.asList(myuser1, myuser2)).build();
Group mygroup3 = Group.builder().groupId(3L).name("Sports-Group").users(Arrays.asList(myuser2, myuser3)).build();

groupRepository.save(mygroup1);
groupRepository.save(mygroup2);
groupRepository.save(mygroup3);
}

@Test
public void test() {

}
}


To solve this problem we can use two different sets to keep track of relations.



First we store users and groups into sets.
Then we store relations into two separate hash "structures". First one contains User -> Group relations and second contains Group -> User relations.



enter image description here



127.0.0.1:6379> HGET users:1 groups
(nil)
127.0.0.1:6379> HGET users:1 Groups
(nil)
127.0.0.1:6379> HGET users:1 Group
(nil)


How to model the POJO classes so that result of all this can be achieved ?



hmset User:1 Groups 1,2  (Store user to database)
hmset Group1 Users 1 (Store group to database)

Get User 1 groups:
hget User:1 Groups

Remove user from group:
hmset User:1 Group ""
hmset Group:1 Users: ""


?










share|improve this question
























  • jira.spring.io/browse/DATAREDIS-893
    – PAA
    Nov 21 at 18:18














3












3








3


1





I've taken a reference of the link : http://panuoksala.blogspot.com/2015/09/redis-many-to-many.html to developed below code. I've implemented some code, looks like nothing is achievable so far as



Get User 1 groups:



hget User:1 Groups


Does not yeild the results.



I wanted to get answer more from the Data Modelling perspectives. I've modeled code like below, but its not working as per my requirement -



Group.java



@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("groups")
public class Group {
@Id
private Long groupId;
private String name;

private List<User> users;
}


And User.java



@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("users")
public class User {
@Id
private Long userId;
private String name;
List<Group> groups;
}


UserGroupTest.java



@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserGroupTest extends RepositoryTestSupport{
@Autowired UserRepository userRepository;
@Autowired GroupRepository groupRepository;

@Before
public void setUp() {

// User -> Group relations
Group group1 = Group.builder().groupId(1L).name("Nature-Group").build();
Group group2 = Group.builder().groupId(2L).name("Music-Group").build();
Group group3 = Group.builder().groupId(3L).name("Sports-Group").build();

User user1 = User.builder().userId(1L).name("John").groups(Arrays.asList(group1, group2)).build();
User user2 = User.builder().userId(2L).name("Sally").groups(Arrays.asList(group2, group3)).build();
User user3 = User.builder().userId(3L).name("Chris").groups(Arrays.asList(group3)).build();

userRepository.save(user1);
userRepository.save(user2);
userRepository.save(user3);


// second contains Group -> User
User myuser1 = User.builder().userId(1L).name("John").build();
User myuser2 = User.builder().userId(2L).name("Sally").build();
User myuser3 = User.builder().userId(3L).name("Chris").build();

Group mygroup1 = Group.builder().groupId(1L).name("Nature-Group").users(Arrays.asList(myuser1)).build();
Group mygroup2 = Group.builder().groupId(2L).name("Music-Group").users(Arrays.asList(myuser1, myuser2)).build();
Group mygroup3 = Group.builder().groupId(3L).name("Sports-Group").users(Arrays.asList(myuser2, myuser3)).build();

groupRepository.save(mygroup1);
groupRepository.save(mygroup2);
groupRepository.save(mygroup3);
}

@Test
public void test() {

}
}


To solve this problem we can use two different sets to keep track of relations.



First we store users and groups into sets.
Then we store relations into two separate hash "structures". First one contains User -> Group relations and second contains Group -> User relations.



enter image description here



127.0.0.1:6379> HGET users:1 groups
(nil)
127.0.0.1:6379> HGET users:1 Groups
(nil)
127.0.0.1:6379> HGET users:1 Group
(nil)


How to model the POJO classes so that result of all this can be achieved ?



hmset User:1 Groups 1,2  (Store user to database)
hmset Group1 Users 1 (Store group to database)

Get User 1 groups:
hget User:1 Groups

Remove user from group:
hmset User:1 Group ""
hmset Group:1 Users: ""


?










share|improve this question















I've taken a reference of the link : http://panuoksala.blogspot.com/2015/09/redis-many-to-many.html to developed below code. I've implemented some code, looks like nothing is achievable so far as



Get User 1 groups:



hget User:1 Groups


Does not yeild the results.



I wanted to get answer more from the Data Modelling perspectives. I've modeled code like below, but its not working as per my requirement -



Group.java



@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("groups")
public class Group {
@Id
private Long groupId;
private String name;

private List<User> users;
}


And User.java



@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("users")
public class User {
@Id
private Long userId;
private String name;
List<Group> groups;
}


UserGroupTest.java



@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserGroupTest extends RepositoryTestSupport{
@Autowired UserRepository userRepository;
@Autowired GroupRepository groupRepository;

@Before
public void setUp() {

// User -> Group relations
Group group1 = Group.builder().groupId(1L).name("Nature-Group").build();
Group group2 = Group.builder().groupId(2L).name("Music-Group").build();
Group group3 = Group.builder().groupId(3L).name("Sports-Group").build();

User user1 = User.builder().userId(1L).name("John").groups(Arrays.asList(group1, group2)).build();
User user2 = User.builder().userId(2L).name("Sally").groups(Arrays.asList(group2, group3)).build();
User user3 = User.builder().userId(3L).name("Chris").groups(Arrays.asList(group3)).build();

userRepository.save(user1);
userRepository.save(user2);
userRepository.save(user3);


// second contains Group -> User
User myuser1 = User.builder().userId(1L).name("John").build();
User myuser2 = User.builder().userId(2L).name("Sally").build();
User myuser3 = User.builder().userId(3L).name("Chris").build();

Group mygroup1 = Group.builder().groupId(1L).name("Nature-Group").users(Arrays.asList(myuser1)).build();
Group mygroup2 = Group.builder().groupId(2L).name("Music-Group").users(Arrays.asList(myuser1, myuser2)).build();
Group mygroup3 = Group.builder().groupId(3L).name("Sports-Group").users(Arrays.asList(myuser2, myuser3)).build();

groupRepository.save(mygroup1);
groupRepository.save(mygroup2);
groupRepository.save(mygroup3);
}

@Test
public void test() {

}
}


To solve this problem we can use two different sets to keep track of relations.



First we store users and groups into sets.
Then we store relations into two separate hash "structures". First one contains User -> Group relations and second contains Group -> User relations.



enter image description here



127.0.0.1:6379> HGET users:1 groups
(nil)
127.0.0.1:6379> HGET users:1 Groups
(nil)
127.0.0.1:6379> HGET users:1 Group
(nil)


How to model the POJO classes so that result of all this can be achieved ?



hmset User:1 Groups 1,2  (Store user to database)
hmset Group1 Users 1 (Store group to database)

Get User 1 groups:
hget User:1 Groups

Remove user from group:
hmset User:1 Group ""
hmset Group:1 Users: ""


?







spring spring-boot redis jedis spring-data-redis






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 14:24

























asked Nov 12 at 15:41









PAA

2,46021935




2,46021935












  • jira.spring.io/browse/DATAREDIS-893
    – PAA
    Nov 21 at 18:18


















  • jira.spring.io/browse/DATAREDIS-893
    – PAA
    Nov 21 at 18:18
















jira.spring.io/browse/DATAREDIS-893
– PAA
Nov 21 at 18:18




jira.spring.io/browse/DATAREDIS-893
– PAA
Nov 21 at 18:18

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265496%2fhow-to-model-data-in-redis-for-values-complex-data-structure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265496%2fhow-to-model-data-in-redis-for-values-complex-data-structure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python