The GQLDB Java driver provides methods to create, delete, list, and manage graphs in the database.
GQLDB supports three graph types defined in the GraphType enum:
Javaimport com.gqldb.types.GraphType; GraphType.OPEN // Schema-less graph (default) GraphType.CLOSED // Schema-enforced graph GraphType.ONTOLOGY // Ontology-enabled graph
| Type | Description |
|---|---|
OPEN | Schema-less graph where any node/edge labels and properties are allowed |
CLOSED | Schema-enforced graph where labels and properties must be predefined |
ONTOLOGY | Graph with ontology support for semantic modeling |
Create a new graph in the database:
Javaimport com.gqldb.*; import com.gqldb.types.GraphType; public void createGraphExample(GqldbClient client) { // Create an open (schema-less) graph client.createGraph("myGraph", GraphType.OPEN, "My first graph"); System.out.println("Graph created"); // Create a closed (schema-enforced) graph client.createGraph("strictGraph", GraphType.CLOSED, "Schema-enforced graph"); // Create with default type (OPEN) and no description client.createGraph("simpleGraph"); }
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
name | String | required | Name of the graph |
graphType | GraphType | GraphType.OPEN | Type of the graph |
description | String | "" | Optional description |
Delete a graph from the database:
Javapublic void dropGraphExample(GqldbClient client) { // Drop a graph (throws exception if not found) client.dropGraph("myGraph"); System.out.println("Graph dropped"); // Drop if exists (no error if graph doesn't exist) client.dropGraph("maybeGraph", true); System.out.println("Graph dropped if it existed"); }
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
name | String | required | Name of the graph to delete |
ifExists | boolean | false | If true, don't error if graph doesn't exist |
Set the current graph for the session:
Javapublic void useGraphExample(GqldbClient client) { // Set the active graph client.useGraph("myGraph"); System.out.println("Now using myGraph"); // Subsequent queries will target this graph by default Response response = client.gql("MATCH (n) RETURN count(n)"); System.out.println("Node count: " + response.singleLong()); }
Retrieve all graphs in the database:
Javaimport com.gqldb.GraphInfo; import java.util.List; public void listGraphsExample(GqldbClient client) { List<GraphInfo> graphs = client.listGraphs(); System.out.println("Found " + graphs.size() + " graphs:"); for (GraphInfo graph : graphs) { System.out.println("- " + graph.getName() + " (" + graph.getGraphType() + "): " + graph.getDescription()); System.out.println(" Nodes: " + graph.getNodeCount() + ", Edges: " + graph.getEdgeCount()); } }
Javapublic class GraphInfo { String getName(); GraphType getGraphType(); String getDescription(); long getNodeCount(); long getEdgeCount(); }
Get detailed information about a specific graph:
Javapublic void getGraphInfoExample(GqldbClient client) { GraphInfo info = client.getGraphInfo("myGraph"); System.out.println("Graph Name: " + info.getName()); System.out.println("Type: " + info.getGraphType()); System.out.println("Description: " + info.getDescription()); System.out.println("Node Count: " + info.getNodeCount()); System.out.println("Edge Count: " + info.getEdgeCount()); }
Javaimport com.gqldb.*; public void safeGraphOperations(GqldbClient client) { try { // Try to create a graph client.createGraph("newGraph"); } catch (GraphExistsException e) { System.out.println("Graph already exists"); } catch (GqldbException e) { System.err.println("Failed to create graph: " + e.getMessage()); } try { // Try to get graph info GraphInfo info = client.getGraphInfo("unknownGraph"); } catch (GraphNotFoundException e) { System.out.println("Graph not found"); } try { // Try to drop a graph client.dropGraph("oldGraph"); } catch (GraphNotFoundException e) { System.out.println("Graph does not exist"); } catch (GqldbException e) { System.err.println("Failed to drop graph: " + e.getMessage()); } }
Javaimport com.gqldb.*; import com.gqldb.types.GraphType; import java.util.List; public class GraphManagementExample { public static void main(String[] args) { GqldbConfig config = GqldbConfig.builder() .hosts("192.168.1.100:9000") .build(); try (GqldbClient client = new GqldbClient(config)) { client.login("admin", "password"); // List existing graphs System.out.println("Existing graphs:"); List<GraphInfo> existingGraphs = client.listGraphs(); for (GraphInfo g : existingGraphs) { System.out.println(" - " + g.getName()); } // Create a new graph String graphName = "demoGraph"; try { client.createGraph(graphName, GraphType.OPEN, "Demo graph for testing"); System.out.println("Created graph: " + graphName); } catch (GraphExistsException e) { System.out.println("Graph " + graphName + " already exists"); } // Use the graph client.useGraph(graphName); // Get graph info GraphInfo info = client.getGraphInfo(graphName); System.out.println("Graph info: " + info.getName() + " (nodes: " + info.getNodeCount() + ", edges: " + info.getEdgeCount() + ")"); // Insert some data client.gql("INSERT (n:User {_id: \"u1\", name: \"Alice\"})"); client.gql("INSERT (n:User {_id: \"u2\", name: \"Bob\"})"); // Check updated counts GraphInfo updatedInfo = client.getGraphInfo(graphName); System.out.println("Graph now has " + updatedInfo.getNodeCount() + " nodes"); // Clean up - drop the demo graph client.dropGraph(graphName); System.out.println("Dropped graph: " + graphName); } catch (GqldbException e) { System.err.println("Error: " + e.getMessage()); } } }