2016-06-28 79 views
1

我想弄清楚如何查询Hazelcast中的分层树结构。比方说,我有一个组织类:查询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的一部分?

任何帮助表示赞赏。

+0

我不能够准确可视化的数据结构。你可以请张贴一些样本数据吗?此外,组织和用户的预期数量? –

+0

无法真正将其格式化...假设您有一个拥有2个子组织的父组织,其中包括child1和child2。 Child1还有一个孩子组织child1_1。鉴于父母组织,我希望找到父母,孩子1,孩子2和孩子1_1中的所有用户。 –

+0

谢谢。此外,有多少这样的上级组织以及层级结构的数量是多少? –

回答

1

我最终构建了一个非规范化的多图,所以我可以找到给定组织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()); 
    } 
}