Bolt client

TuGraph implements Neo4j’s bolt protocol, you can use Neo4j’s client to access TuGraph.

Enable bolt port

The Bolt port is enabled by default on port 7687. If you need to change the port, you can do so in the configuration file. For services deployed using the Docker method, the configuration file is located within the container at /usr/local/etc/lgraph.json; for services deployed using the RPM package, the configuration file is on the server at /usr/local/etc/lgraph.json. To apply the port change, you will need to restart the service. Specific instructions for restarting the service can be found in Tugraph Running. Additionally, for a detailed explanation of the configuration options available in the configuration file, see the “Configuration parameters” section in Tugraph Running.

Use case example

Add Maven dependency

<dependency>
    <groupId>org.neo4j.driver</groupId>
    <artifactId>neo4j-java-driver</artifactId>
    <version>4.4.2</version>
</dependency>

Instantiate the client as follows:

    Driver driver = GraphDatabase.driver("bolt://ip:port", AuthTokens.basic("admin", "73@TuGraph"));

Common statements:

        //Create a session through the driver object, setting the session to connect to a specific database, to execute Cypher statements.
        Session session = driver.session(SessionConfig.forDatabase("default"));
        //Clear the graph project, please do not attempt this lightly as it will delete both the schema and data of the selected graph project.
        session.run("CALL db.dropDB()");
        //Create vertex
        session.run("CALL db.createVertexLabel('person', 'id' , 'id' ,INT32, false, 'name' ,STRING, false)");
        //Create edge
        session.run("CALL db.createEdgeLabel('is_friend','[[\"person\",\"person\"]]')");
        //Create index
        session.run("CALL db.addIndex(\"person\", \"name\", false)");
        //Insert vertex data
        session.run("create (n1:person {name:'jack',id:1}), (n2:person {name:'lucy',id:2})");
        //Insert edge data
        session.run("match (n1:person {id:1}), (n2:person {id:2}) create (n1)-[r:is_friend]->(n2)");
        //Query vertices and edges
        Result res = session.run("match (n)-[r]->(m) return n,r,m");
        //Parameterized Query
        String cypherQuery = "MATCH (n1:person {id:$id})-[r]-(n2:person {name:$name}) RETURN n1, r, n2";
        Result result1 = session.run(cypherQuery, parameters("id", 1, "name", "lucy"));
        while (result1.hasNext()) {
        Record record = result1.next();
        System.out.println("n1: " + record.get("n1").asMap());
        System.out.println("r: " + record.get("r").asMap());
        System.out.println("n2: " + record.get("n2").asMap());
                }        
        //Delete vertex data
        session.run("match (n1:person {id:1}) delete n1");
        //Delete edge data
        session.run("match (n1:person {id:1})-[r]-(n2:person{id:2}) delete r");
        //Delete edge
        session.run("CALL db.deleteLabel('edge', 'is_friend')");
        //Delete vertex
        session.run("CALL db.deleteLabel('vertex', 'person')");

Details on the use of Cypher and stored procedures can be found in Cypher

Limitations on use

Currently, our system does not fully support all advanced features of the Neo4j Bolt protocol. In particular, immediate transaction processing and flexible weak schema, two unique capabilities of the Neo4j client, are not yet supported. Therefore, when using the client, please make sure to avoid operations that involve these features to ensure smooth running of the application and consistency of data.

Example of Client Usage

In the code directory under demo/Bolt, there are examples in Golang, JavaScript, Java, JavaScript, Python, and Rust. To learn more, see Example of Client Usage