Cayley Graph Database

repository·master·Indexed 12 days ago

https://github.com/cayleygraph/cayley

An open-source graph database designed for Linked Data, inspired by Google's Knowledge Graph. Cayley features a modular architecture supporting multiple query languages including Gizmo, GraphQL, and MQL, and can be integrated as a Go library or deployed via CLI, Kubernetes, and Google App Engine.

Tokens
31.5K
Snippets
159
Records
203
Agent score
94%

What's inside Cayley

  1. Overview of Cayley Linked Data Database

    master
    Cayley is an open-source database designed for Linked Data. It is inspired by the graph database architecture used in Google's Knowledge Graph. Cayley is modular, allowing developers to connect it to various programming languages and back-end storage engines. It is optimized for application performance and is suitable for production workloads.
  2. Introduction to Cayley

    master
    Cayley is an open-source graph database designed for ease of use and storing complex data. It supports multiple query languages and provides various interfaces for interaction, including a UI, HTTP, and specialized query languages like Gizmo API, GraphQL, and MQL.
  3. Find Cayley community resources

    master

    Cayley maintains several channels for communication, support, and contribution:

  4. Understand the Cayley UI Sidebar actions

    master

    The Cayley UI provides several views and actions accessible via the sidebar to interact with your graph data:

    • Run Query: Executes the current query.
    • Gizmo: A dropdown to select your query language. Cayley supports two languages: GizmoAPI and MQL.
    • Query: A request/response editor for the selected query language.
    • Query Shape: Provides a visualization of the structure/shape of the final query without executing it.
    • Visualize: Runs a query and generates a sigmajs graph view of the results (requires specific tagging).
    • Write: An interface for adding or removing individual quads or quad files.
    • Documentation: Access to the project documentation.
  5. What is a token in a quad store?

    master

    In the context of a graph.QuadStore, a token is a graph.Value. It is an opaque identifier that the backing store uses to represent a quad or a node. The specific implementation of a token depends on the backend:

    • Traditional graph databases: Might use int64 (e.g., GUIDs).
    • Direct graph implementations: Might use pointers to structs or the quads themselves.

    Base iterators pass these tokens around to identify elements within the graph.

  6. What is reification?

    master

    Reification is the process of treating a relationship (an edge) as an entity (a node).

    In graph modeling, you use reification when you need to add additional information (metadata) to a relationship. Instead of a direct link between two nodes, you create a new node that represents the statement itself, which then points to the subject and object. This effectively creates a 'metagraph' on top of your existing graph.

  7. What is a Shape in Cayley?

    master

    A Shape is an abstract representation of a query. It sits in the query hierarchy between Paths (higher level) and Iterators (lower level).

    Key characteristics:

    • It describes the structure of a query tree.
    • It allows for high-level operations such as traversing inbound/outbound predicates or finding unions and intersections.
    • Its primary purpose is to be transformed into a tree of Iterators via BuildIterator, which performs the actual mechanical processing of quads to find results.
    • It supports backend-agnostic optimizations via the Optimize method.
    type Shape interface {
        BuildIterator(qs graph.QuadStore) graph.Iterator
        Optimize(ctx context.Context, r Optimizer) (Shape, bool)
    }
  8. Use Morphisms to define reusable path chains

    master

    A Morphism is a prepared path chain that can be applied to other queries using .follow(path) or .followR(path). This allows you to define complex traversal patterns (like "friend of a friend") once and reuse them.

    • .follow(path): Applies the morphism chain in the forward direction.
    • .followR(path): Applies the morphism chain in the reverse direction (flips "In" and "Out").
    • .followRecursive(path): Applies the morphism chain recursively, returning all encountered nodes.
    var friendOfFriend = g
      .Morphism()
      .out("<follows>")
      .out("<follows>");
    
    // Use the morphism to find friends of friends of Charlie
    g.V("<charlie>")
      .follow(friendOfFriend)
      .all();
  9. Use Gizmo for graph traversal

    master

    Gizmo is a query language for Cayley inspired by Gremlin/TinkerPop. It uses a JavaScript-like syntax to traverse and filter named graphs.

    Key concepts:

    • .v(): Used to select a Vertex (node). For example, g.V() returns a list of all vertices in the graph.
    • Inbound/Outbound Predicates: Refers to the direction of a relation. If A follows B, follows is an outbound predicate for A and an inbound predicate for B. In Gizmo, you can traverse using .out("predicate") or .in("predicate").
    // Example: Find names of projects created by two friends
    g.V().match(
      as("a").out("knows").as("b"),
      as("a").out("created").as("c"),
      as("b").out("created").as("c"),
      as("c").in("created").count().is(2)).
        select("c").by("name")
  10. Use Raw mode for streaming quads to Gephi

    master

    In raw mode (the default), Cayley streams selected quads directly to Gephi. This mode is best for visualizing small subgraphs or graphs without metadata (like types and properties). If you use this mode on graphs with many common types, you will see many quads pointing to nodes describing those types.

    Parameters:

    • mode=raw - Sets the streaming mode.
    • limit - Maximal number of quads returned (default 10000; use -1 for no limit).
    • sub - Filter quads by Subject.
    • pred - Filter quads by Predicate.
    • obj - Filter quads by Object.
    • label - Filter quads by Label.
    /* Example: All quads */
    /gephi/gs?mode=raw&pred=<follows>&limit=-1
    
    /* Example: Links from <bob> via <follows> or <status> */
    /gephi/gs?mode=raw&sub=<bob>&pred=<follows>,<status>&limit=-1
  11. Understand the Iterator model

    master

    Graph queries in Cayley are represented as a tree of iterators (implementing graph.Iterator).

    • An iterator is a stand-in for a set of items matching a specific part of the graph.
    • Subiterators are the branches and leaves of the iterator tree. Evaluation occurs by repeatedly calling Next() on the root iterator.

    Key Iterator Types

    HasA Iterator

    An iterator that takes a subiterator of links and acts as an iterator of nodes in a specific direction. It effectively says a link "HasA" a certain component (e.g., a link has a subject).

    LinksTo Iterator

    An iterator that takes a subiterator of nodes and returns the links that "link to" those nodes in a given direction. It is the dual of the HasA iterator.

  12. Use the `graph` object to generate Gizmo queries

    master

    The graph object (aliased as g) is the primary entry point for the Gizmo API. It is used to generate query objects which are compiled into a Go iterator tree for execution. All queries originate from this object.

    // 'g' is the common alias for 'graph'
    var query = g.V().out("friend");