2016-06-28 93 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()); 
    } 
}