Nishant
Nishant

Reputation: 61

Neo4j server plugin basic questions

I am using Neo4J v2.1.5 and creating a server plugin.

  1. How to create a unique node i.e. guarantee uniqueness of a property?
  2. Is there a hook where in the plugin lifecycle, constraints and indexes can be created?
  3. Returning a node returns the complete database. How can I return just a node or a pojo list as JSON? Are there any working examples or explanation of Representation available?

I am using Java API and not Cypher.

Upvotes: 2

Views: 262

Answers (2)

Mohit
Mohit

Reputation: 311

Hi with cypher i can sugesst you few thing,

Q How to create a unique node i.e. guarantee uniqueness of a property? Ans. first chosse a property that could be unique for that node , same like Primary key in of your relational database system, i.e Id now you merge to create a node, MERGE (u:User { Id:1 }) set u.Name='Charlie' RETURN u if the user with Id will not exist it will create it, then using set Clause you can set other property or hole obejct as well,

Q Returning a node returns the complete database. How can I return just a node or a pojo list as JSON? Are there any working examples or explanation of Representation available? Ans. Same way to match if you pass the unique id and try to search it will return you only that particualr node only i.e match(u:User { Id:1 }) return u

to create such Id , i will suggest you to go with GUID created in programing lunguaze like C#, but with neo4j 3.x you also used autoincremented propery as well.

Upvotes: 0

Mark Needham
Mark Needham

Reputation: 2128

How to create a unique node i.e. guarantee uniqueness of a property?

You can create a unique constraint on a (label, property) pair which will ensure the uniqueness of that property.

e.g.

CREATE UNIQUE CONSTRAINT ON :Person(name)

Would ensure you can't have two people nodes with the same name. If you want to do that from the Java API you'd do something like this:

try ( Transaction tx = graphdb.beginTx() )
{
    graphdb.schema()
        .constraintFor( DynamicLabel.label( "Person" ) )
        .assertPropertyIsUnique( "name" )
        .create();
    tx.success();
}

Is there a hook where in the plugin lifecycle, constraints and indexes can be created?

You can do that in a transaction but IIRC you can only create one index/constraint per transaction.

Returning a node returns the complete database. How can I return just a node or a pojo list? Are there any working examples or explanation of Representation available?

Do you mean from cypher? A simple query which will only return one node would be this:

MATCH (n)
RETURN n
LIMIT 1

In cypher land that will return you a map of the properties that the node has on it. If you want to get something more specific you could try this:

MATCH (n:Person)
RETURN n.name AS personName
LIMIT 1

So then you'd get a String back for that column in the result set.

-- Updating for Java API --

From the Java API you can write your own traversals which will give you back 'Node' and 'Relationship' objects. From those you'd then have to extract any properties that you're interested in.

try ( Transaction tx = graphDatabaseService.beginTx() )
{
    ResourceIterable<Node> people = GlobalGraphOperations.at( graphDatabaseService ).getAllNodesWithLabel( DynamicLabel.label( "Person" ) );

    for ( Node node : people )
    {
        String name = (String) node.getProperty( "name" );
    }

    tx.success();
}

Upvotes: 2

Related Questions