我想弄清楚如何查询Hazelcast中的分层树结构。假设我有一个组织类:
public class Organization {
private long id;
private long parentId;
}
和我有一个User类:
public class NestupUser extends BaseEntity {
private long id;
private String firstName;
private String lastName;
private String email;
private String password;
private long organizationId;
}
现在,给定一个organizationId,我想要查找该组织的所有用户以及将该组织作为父级的所有组织,以及将这些组织作为父级的所有组织,等等
我假设这将作为某种MapReduce工作,但是有可能启动更多的MapReduce任务作为一个MapReduce的一部分吗?
感谢任何帮助。
我最终构建了一个非规范化的multimap,这样我就可以为给定的org id找到所有可访问的组织。这是在启动时设置结构的代码,如果它还没有被另一个节点设置。该类还实现了条目侦听器接口,以便在发生变化时获得回调,以保持结构同步(没有显示,但不难做到):
@PostConstruct
public void init() {
IMap<String, Organization> organizationMap = organizationService.getMap();
listenerRegistration = organizationMap.addLocalEntryListener(this);
MultiMap<String, String> orgStructureMap = getOrgStructureMap();
if (orgStructureMap.keySet().size() == 0) {
Collection<Organization> all = organizationService.getAll(null);
Set<String> visited = new HashSet<>();
for (Organization next : all) {
if (!visited.contains(next.getId())) {
while (next != null && next.getParentId() != null && !visited.contains(next.getParentId())) {
next = next.getParentOrganization();
}
recurseReferences(visited, next);
}
}
}
}
private void recurseReferences(Set<String> visited, Organization org) {
addAllReferences(org);
visited.add(org.getId());
Set<Organization> childOrganizations = org.getChildOrganizations();
for (Organization child : childOrganizations) {
recurseReferences(visited, child);
}
}
private void addAllReferences(Organization organization) {
MultiMap<String, String> orgStructureMap = getOrgStructureMap();
String parentId = organization.getParentId();
if (parentId != null) {
Set<Map.Entry<String, String>> entries = orgStructureMap.entrySet();
for (Map.Entry<String, String> next : entries) {
if (next.getValue().equals(parentId)) {
orgStructureMap.put(next.getKey(),organization.getId());
}
}
}
orgStructureMap.put(organization.getId(), organization.getId());
}
private void removeAllReferences(Organization organization) {
MultiMap<String, String> orgStructureMap = getOrgStructureMap();
Set<String> keys = orgStructureMap.keySet();
for (String key : keys) {
orgStructureMap.remove(key, organization.getId());
}
}