查询 Hazelcast 分层树结构
Querying Hazelcast hierarchical tree structures
我正在尝试了解如何在 Hazelcast 中查询分层树结构。假设我有一个组织 class:
public class Organization {
private long id;
private long parentId;
}
我有一个用户 class:
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 任务?
感谢任何帮助。
我最终构建了一个非规范化的多图,因此我可以找到给定组织 ID 的所有可访问组织。这是在启动时设置结构的代码,如果它还没有被另一个节点设置的话。 class 还实现了入口侦听器接口,以便在事情发生变化时获得回调以保持结构同步(未显示,但不难做到):
@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());
}
}
我正在尝试了解如何在 Hazelcast 中查询分层树结构。假设我有一个组织 class:
public class Organization {
private long id;
private long parentId;
}
我有一个用户 class:
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 任务?
感谢任何帮助。
我最终构建了一个非规范化的多图,因此我可以找到给定组织 ID 的所有可访问组织。这是在启动时设置结构的代码,如果它还没有被另一个节点设置的话。 class 还实现了入口侦听器接口,以便在事情发生变化时获得回调以保持结构同步(未显示,但不难做到):
@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());
}
}