UltipaDocs
Try Playground
  • Introduction
    • Quick Start
    • Configuration
    • Connection and Session
    • Executing Queries
    • Graph Management
    • Transactions
    • Data Operations
    • Bulk Import
    • Data Export
    • Health and Administration
    • Response Processing
    • Data Types
    • Error Handling
    • Quick Start
    • Configuration
    • Connection and Session
    • Executing Queries
    • Graph Management
    • Transactions
    • Data Operations
    • Bulk Import
    • Data Export
    • Health and Administration
    • Response Processing
    • Data Types
    • Error Handling
    • Quick Start
    • Configuration
    • Connection and Session
    • Executing Queries
    • Graph Management
    • Transactions
    • Data Operations
    • Bulk Import
    • Data Export
    • Health and Administration
    • Response Processing
    • Data Types
    • Error Handling
    • Quick Start
    • Configuration
    • Connection and Session
    • Executing Queries
    • Graph Management
    • Transactions
    • Data Operations
    • Bulk Import
    • Data Export
    • Health and Administration
    • Response Processing
    • Data Types
    • Error Handling
  1. Docs
  2. /
  3. Ultipa Drivers
  4. /
  5. Java

Graph Management

The GQLDB Java driver provides methods to create, delete, list, and manage graphs in the database.

Graph Types

GQLDB supports three graph types defined in the GraphType enum:

Java
import com.gqldb.types.GraphType;

GraphType.OPEN      // Schema-less graph (default)
GraphType.CLOSED    // Schema-enforced graph
GraphType.ONTOLOGY  // Ontology-enabled graph
TypeDescription
OPENSchema-less graph where any node/edge labels and properties are allowed
CLOSEDSchema-enforced graph where labels and properties must be predefined
ONTOLOGYGraph with ontology support for semantic modeling

Creating Graphs

createGraph()

Create a new graph in the database:

Java
import 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:

ParameterTypeDefaultDescription
nameStringrequiredName of the graph
graphTypeGraphTypeGraphType.OPENType of the graph
descriptionString""Optional description

Deleting Graphs

dropGraph()

Delete a graph from the database:

Java
public 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:

ParameterTypeDefaultDescription
nameStringrequiredName of the graph to delete
ifExistsbooleanfalseIf true, don't error if graph doesn't exist

Using a Graph

useGraph()

Set the current graph for the session:

Java
public 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());
}

Listing Graphs

listGraphs()

Retrieve all graphs in the database:

Java
import 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());
    }
}

GraphInfo Class

Java
public class GraphInfo {
    String getName();
    GraphType getGraphType();
    String getDescription();
    long getNodeCount();
    long getEdgeCount();
}

Getting Graph Information

getGraphInfo()

Get detailed information about a specific graph:

Java
public 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());
}

Exception Handling

Java
import 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());
    }
}

Complete Example

Java
import 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());
        }
    }
}