POSH Geek
POSH Geek

Reputation: 174

Build relationships in NEO4J

I'm going to preface this that I am a total database pleb. I have 0 experience with any form of databases so I know that I'm in way over my head.

Background: I do Active Directory consulting for my company so I routinely look at client's group membership of their active directory accounts. Currently, I have a PowerShell script that will run my analytics, however, I'm finding that it takes way too long in larger organizations. I'm thinking "There has to be a better way" so I have jumped into looking at databases. NEO4J seems to be a good possible solution as I should be able to to link a user account or group as a member of another group. However, after browsing documentation and forums, I have no idea how to create those links.

I have two CSVs that I have successfully imported with the following information:

Users = DistinguishedName, SAMACCOUNTNAME, MemberOf

Groups = DistinguishedName, SAMACCOUNTNAME, MemberOf, Members

What I want to do is match a string from all users and groups (DistinguishedName) to a string in the group node's property of members. Members is a concatenated string of all DistinguishedName's (whether user or group). So if a node with a DistinguishedName matches part of a string in a group's "members" property, I want to build a one way relationship like so:

user -[memberof] - > group

The best I could rack my brain on this is the following code but I have no idea if I'm even close:

    Match(n)
    Match(u:user) WHERE n.Members CONTAINS u.DN
    Create (u)-[MS:Memberof]->((match)})

In PowerShell, I know how I would accomplish this (loosely translated to relate to the NEO4J world):

   $groups = (all-groups)
   $AllUsersAndGroups = (all-objs)
   foreach ($line in $groups) {
   $line.relationship = $line | where {$_.members -contains    $AllUsersAndGrups.DistinguishedName}
    }

So at last, I'm stuck right now. I will continue to look into it but I figure I would ask the community as you guys have the experience and stuff.

Upvotes: 0

Views: 81

Answers (1)

cybersam
cybersam

Reputation: 66999

Here is an example of how you should have imported your data (notice that the redundant Members column is not actually needed):

  • Import (in batches of 5000, to avoid resource issues) each user, and create a unique relationship to its group:

    USING PERIODIC COMMIT 5000
    LOAD CSV WITH HEADERS FROM "file:///users.csv" AS u
    MERGE (u:User {DistinguishedName: u.DistinguishedName, SAMACCOUNTNAME: u.SAMACCOUNTNAME})
    MERGE (g:Group {DistinguishedName: u.MemberOf})
    MERGE (u)-[:Memberof]->(g);
    
  • Import each group, and create a unique relationship to its parent group, if any:

    USING PERIODIC COMMIT 5000
    LOAD CSV WITH HEADERS FROM "file:///groups.csv" AS g1
    MERGE (:Group {DistinguishedName: g1.DistinguishedName, SAMACCOUNTNAME: g1.SAMACCOUNTNAME})
    MERGE (g2:Group {DistinguishedName: g1.MemberOf})
    MERGE (g1)-[:Memberof]->(g2);
    

Upvotes: 1

Related Questions