Connecting to PuppyGraph and making graph queries in openCypher
PuppyGraph supports openCypher, the widely used implementation of the Cypher graph query language.
Graph Notebook
Graph Notebook is an open-source tool that enables users to interact with and visualize graph databases directly within a Jupyter Notebook environment.
PuppyGraph comes with a bundled Graph Notebook. To access it, click the Graph Notebook tab on the Query page. A new page will open with the Graph Notebook in it.
It comes with an example.ipynb file with example queries.
Cypher Console
PuppyGraph provides an interactive command line, which is based on Gremlin Console and cypher-for-gremlin plugin. To access it, click the Cypher Console tab on the Query page.
Be sure to add:> before the cypher query. See the screenshot below.
Client Drivers
PuppyGraph supports connecting and executing queries through the Bolt protocol of the following programming languages:
Python
Install the Neo4j Python Driver:
pipinstallneo4j
Here is an example of how to establish a connection to PuppyGraph and run a query:
from neo4j import GraphDatabase# Initialize connection to the PuppyGraph.uri ="bolt://localhost:7687"username ="puppygraph"password ="puppygraph123"driver = GraphDatabase.driver(uri, auth=(username, password))session = driver.session()# Get all nodes from the graph.query ="MATCH (n) RETURN n"nodes = session.run(query)print("All nodes in the graph:")for record in nodes: node = record["n"]print({"id": node.element_id,"labels": list(node.labels),"properties": dict(node._properties) })
Go
Install the Neo4j Go Driver:
goget"github.com/neo4j/neo4j-go-driver/v4/neo4j"
Here is an example of how to establish a connection to PuppyGraph and run a query:
packagemainimport ("fmt""github.com/neo4j/neo4j-go-driver/v4/neo4j")funcmain() {// Creating the connection to the PuppyGraph. uri :="bolt://localhost:7687" username :="puppygraph" password :="puppygraph123" driver, err := neo4j.NewDriver(uri, neo4j.BasicAuth(username, password, ""))if err !=nil { fmt.Println("Error creating driver:", err)return }defer driver.Close()// Open a new session using the driver sessionConfig :=neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead} session := driver.NewSession(sessionConfig)defer session.Close()// Get all the vertices in the Graph. query :="MATCH (n) RETURN n" fmt.Println("All vertices in the graph:") results, err := session.Run(query, nil)if err !=nil { fmt.Println("Error executing query:", err)return }for results.Next() { fmt.Println(results.Record().Values) }if err = results.Err(); err !=nil { fmt.Println("Error with query results:", err) }}
Java
Install the Neo4j Java Driver:
The example uses Maven. You need to add the following dependencies to the pom.xml file of your project.
Here is an example of how to establish a connection to PuppyGraph and run a query:
importorg.neo4j.driver.AuthTokens;importorg.neo4j.driver.Driver;importorg.neo4j.driver.GraphDatabase;importorg.neo4j.driver.Record;importorg.neo4j.driver.Session;importorg.neo4j.driver.Result;importorg.neo4j.driver.types.Node;publicclassExample {publicstaticvoidmain(String[] args) {// Connect to PuppyGraphDriver driver =GraphDatabase.driver("bolt://localhost:7687",AuthTokens.basic("puppygraph","puppygraph123"));Session session =driver.session();// Get all the vertices in the Graph.Result result =session.run("MATCH (n) RETURN n");System.out.println("All vertices in the graph:");while (result.hasNext()) {Record record =result.next();Node node =record.get("n").asNode();System.out.println(String.format("Node ID: %s, Labels: %s, Properties: %s",node.id(),node.labels(),node.asMap())); }// Close resources.driver.close(); }}
Javascript
Install the Neo4j JavaScript Driver:
npminstallneo4j-driver
Here is an example of how to establish a connection to PuppyGraph and run a query:
constneo4j=require('neo4j-driver');consturi='bolt://localhost:7687';constuser='puppygraph';constpassword='puppygraph123';// Connect to PuppyGraphconstdriver=neo4j.driver(uri,neo4j.auth.basic(user, password));// Create a session to run Cypher statements inconstsession=driver.session();// Get all the vertices in the Graphconstquery='MATCH (n) RETURN n';// Run the Cypher querysession.run(query).then(result => {result.records.forEach(record => {console.log(record.get('n')); }); }).catch(error => {console.error('Error:', error); }).finally(() => {returnsession.close(); // Close the session }).then(() => {returndriver.close(); // Close the driver connection });