Created
February 4, 2016 08:31
-
-
Save lutovich/27e4b8842982b16b8b07 to your computer and use it in GitHub Desktop.
Simple Neo4j Procedure to create/remove/list persons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.ThreadLocalRandom; | |
import java.util.stream.Stream; | |
import org.neo4j.graphdb.GraphDatabaseService; | |
import org.neo4j.graphdb.Node; | |
import org.neo4j.procedure.Context; | |
import org.neo4j.procedure.Procedure; | |
import static org.neo4j.graphdb.Label.label; | |
public class Person | |
{ | |
private static final int NAME_LENGTH = 3; | |
@Context | |
public GraphDatabaseService db; | |
@Procedure | |
public Stream<?> create() | |
{ | |
Node node = db.createNode( label( "Person" ) ); | |
String name = randomName(); | |
node.setProperty( "name", name ); | |
return Stream.of( "Created person with name: '" + name + "'" ); | |
} | |
@Procedure | |
public void removeAll() | |
{ | |
db.findNodes( label( "Person" ) ) | |
.forEachRemaining( Node::delete ); | |
} | |
@Procedure | |
public Stream<?> list() | |
{ | |
return db.findNodes( label( "Person" ) ) | |
.stream() | |
.map( node -> "Person[name=" + node.getProperty( "name" ) + "]" ); | |
} | |
private static String randomName() | |
{ | |
char[] chars = new char[NAME_LENGTH]; | |
for ( int i = 0; i < NAME_LENGTH; i++ ) | |
{ | |
chars[i] = (char) ThreadLocalRandom.current().nextInt( 65, 91 ); | |
} | |
return new String( chars ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment