Basic commands for 02-neo4j

1. Insert nodes.Insert a node of Person category that has an attribute name with an attribute value of Andres

CREATE (n:Person {name : 'Liang Chuan Chuan'});

CREATE (n:Person {name : 'Quiet and quiet'});
CREATE (n:Person {name : 'Nicholas Tse',relation : "oldest maternal uncle"});
CREATE (n:Person {name : 'Thank you 2',relation : "2 Uncle"});
CREATE (n:Person {name : 'Thank you 3',relation : "3 Uncle"});
CREATE (n:Person {name : 'Thank you 4',relation : "4 Uncle"});

2. Insert an edge.Insert a directed edge from a to b with a category of Follow followed by a relationship

MATCH (a:Person),(b:Person)
WHERE a.name = 'Liang Chuan Chuan' AND b.name = 'Quiet and quiet'
CREATE (a)-[r:like]->(b);

MATCH (a:Person),(b:Person)
WHERE a.name = 'Quiet and quiet' AND b.name = 'Nicholas Tse'
CREATE (a)-[r:oldest maternal uncle]->(b);

MATCH (a:Person),(b:Person)
WHERE a.name = 'Quiet and quiet' AND b.name = 'Thank you 2'
CREATE (a)-[r:Second Uncle]->(b);

MATCH (a:Person),(b:Person)
WHERE a.name = 'Quiet and quiet' AND b.name = 'Thank you 3'
CREATE (a)-[r:Uncle Three]->(b);

MATCH (a:Person),(b:Person)
WHERE a.name = 'Quiet and quiet' AND b.name = 'Thank you 4'
CREATE (a)-[r:weight4]->(b);

CREATE (a)-[r:4]->(b);  r Can't be followed directly by a number
CREATE (a)-[r:"4"]->(b); Neither double quotes nor single quotes work
CREATE (a)-[r:weight4]->(b); This way

3. Update the node.Update a node of Person category and set a new name.

MATCH (n:Person { name: 'Thank you 4' })
SET n.name = 'Xie Guibo';
  1. Delete the node.Delete the connection of this node to this node
MATCH (n:Person { name:'Thank you 3' })
DETACH DELETE n;

CREATE (n:Person {name : 'Uncle Xie',hobby : "Read a Book"});
CREATE (n:Person {name : 'Xie Meimei',hobby : "Play a game"});

MATCH (a:Person),(b:Person)
WHERE a.name = 'Quiet and quiet' AND b.name = 'Uncle Xie'
CREATE (a)-[r:Uncle Three]->(b);

MATCH (a:Person),(b:Person)
WHERE a.name = 'Uncle Xie' AND b.name = 'Xie Meimei'
CREATE (a)-[r:daughter]->(b);

MATCH (n:Person { name:'Uncle Xie' })
DETACH DELETE n;


MATCH (a:Person),(b:Person)
WHERE a.name = 'Quiet and quiet' AND b.name = 'Xie Meimei'
CREATE (a)-[r:Younger female cousin]->(b);

5. Delete edges

MATCH (a:Person)-[r:Follow]->(b:Person)
WHERE a.name = 'Liang Chuan Chuan' AND b.name = 'Quiet and quiet'
DELETE r;

6. Query for the shortest path.

MATCH (ms:Person { name:'Liang Chuan Chuan' }),(cs:Person { name:'Quiet and quiet' }), p = shortestPath((ms)-[r:like]-(cs)) RETURN p;

CREATE (n:Person {name : 'Xie Meiyan',hobby : "Read a Book"});

MATCH (a:Person),(b:Person)
WHERE a.name = 'Xie Meimei' AND b.name = 'Xie Meiyan'
CREATE (a)-[r:Sisters]->(b);

MATCH (ms:Person { name:'Liang Chuan Chuan' }),(cs:Person { name:'Xie Meiyan' }), p = shortestPath((ms)-[r:Sisters]-(cs)) RETURN p;
(no changes, no records)

There's a different relationship in between

7. Query the relationship between two nodes.

MATCH (a:Person { name:'Liang Chuan Chuan' })-[r]->(b:Person { name:'Xie Meiyan' })
RETURN type(r);
(no changes, no records)

MATCH (a:Person { name:'Liang Chuan Chuan' })-[r]->(b:Person { name:'Quiet and quiet' })
RETURN type(r);
"like"

8. Query all Follows of a node.

**MATCH (:Person { name:'Quiet and quiet' })-[r:like]->(Person)
RETURN Person.name;
(no changes, no records)
MATCH (:Person { name:'Liang Chuan Chuan' })-[r:like]->(Person)
RETURN Person.name;
"Quiet and quiet"**

Relationships are one-way

java version of the first program

New maven project

It's not easy to download drive from the official website. I queried it directly from maven repository

    <!-- https://mvnrepository.com/artifact/org.neo4j.driver/neo4j-java-driver -->
    <dependency>
        <groupId>org.neo4j.driver</groupId>
        <artifactId>neo4j-java-driver</artifactId>
        <version>1.4.5</version>
    </dependency>

Create node from code

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        Create();
    }

    private static void Create() {

        // Driver driver =GraphDatabase.driver("Bolt://127.0.0.1",AuthTokens.basic(username, password);
        Driver driver = GraphDatabase.driver( "bolt://127.0.0.1", AuthTokens.basic( "neo4j", "541711153" ) );
        Session session = driver.session();

        session.run( "CREATE (n:Person {name : 'Chen Haojie',hobby : 'Read a Book'})" );

        session.close();
        driver.close();
    }
}

Execute Visible Page http://127.0.0.1:7474/browser/ One more node

Query nodes through code

 private static void Query() {
        Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "541711153" ) );//Call Address, User Name and Password
        Session session = driver.session();

        StatementResult result = session.run( "MATCH (a: Person) WHERE a.name = 'Quiet and quiet' RETURN a.name AS name" );
        while ( result.hasNext() )
        {
            Record record = result.next();
            System.out.println( record.get("name").asString() );
        }

        session.close();
        driver.close();
    }

=================Never tried
Create a uniqueness constraint with an indexing effect similar to a primary key in a relational database.Note that this can only be set if data has not been inserted into the database.
CREATE CONSTRAINT ON (a:Person) ASSERT a.name IS UNIQUE;
Create an index.
CREATE INDEX ON :Person(name);
View the number of nodes and edges
MATCH (n) RETURN count(n);
MATCH ()–>() RETURN count(*);

Tags: Session Java Attribute Maven

Posted on Sun, 24 May 2020 09:43:40 -0700 by duankr