nebula-go Go Client for NebulaGraph

repository·master·Indexed 41 days ago

https://github.com/vesoft-inc/nebula-go

The official Go client for NebulaGraph, communicating via the fbthrift protocol. It provides high-level abstractions such as Session Pools for managing connections and executing graph queries, as well as tools for generating nGQL statements for Tag and Edge schemas. Supports version mappings from v1.0.0 through v3.5.x and the master branch for NebulaGraph service compatibility.

Tokens
12K
Snippets
40
Records
49
Agent score
100%

What's inside nebula-go

  1. Use the Session Pool to execute queries

    master

    The SessionPool is the primary way to interact with NebulaGraph. It manages a pool of sessions to execute queries efficiently.

    Key Constraints:

    1. Space Requirement: An existing space must be defined in the database before initializing the session pool.
    2. Scope: Each session pool is tied to a single USER and a single Space. To query different spaces or use different users, you must create multiple session pools.
    3. Execution Context: Every call to sessionPool.Execute() runs within the space configured during initialization.
    4. Security Restriction: Do NOT execute commands that alter passwords or drop users via a session pool.

    To map query results to Go structs, use struct tags with the nebula key (e.g., `nebula:"name"`) and call resultSet.Scan(&target).

    package main
    
    import (
      nebula "github.com/vesoft-inc/nebula-go/v3"
    )
    
    type Person struct {
      Name     string  `nebula:"name"`
      Age      int     `nebula:"age"`
      Likeness float64 `nebula:"likeness"`
    }
    
    func main() {
      hostAddress := nebula.HostAddress{Host: "127.0.0.1", Port: 3699}
    
      config, err := nebula.NewSessionPoolConf(
        "user",
        "password",
        []nebula.HostAddress{hostAddress},
        "space_name",
      )
    
      sessionPool, err := nebula.NewSessionPool(*config, nebula.DefaultLogger{})
    
      query := `GO FROM 'Bob' OVER like YIELD
        $^.person.name AS name,
        $^.person.age AS age,
        like.likeness AS likeness`
    
      resultSet, err := sessionPool.Execute(query)
      if err != nil {
        panic(err)
      }
    
      var personList []Person
      resultSet.Scan(&personList)
    }
  2. Install nebula-go

    master

    You can install the nebula-go client using go get. It is recommended to specify a version tag to ensure stability and avoid checksum mismatches (e.g., avoid v2.5.0, use v2.5.1 instead).

    To install the latest version from the master branch:

    go get -u -v github.com/vesoft-inc/nebula-go/v3@master

    To install a specific version for v3:

    go get -u -v github.com/vesoft-inc/nebula-go/v3@v3.4.0

    To install a specific version for v2:

    go get -u -v github.com/vesoft-inc/nebula-go/v2@v2.6.0
    go get -u -v github.com/vesoft-inc/nebula-go/v3@master
  3. How ConnectionPool manages connections

    master

    The ConnectionPool manages two internal queues to optimize performance and resource usage:

    1. Idle Connection Queue: Stores connections that are authenticated and ready for reuse. The pool uses a background connectionCleaner to remove connections that have exceeded the IdleTime defined in PoolConfig, provided the pool size stays above MinConnPoolSize.
    2. Active Connection Queue: Stores connections currently being used by active sessions.

    Key Behaviors:

    • Round-Robin: When creating new connections, the pool selects hosts from the provided addresses list in a round-robin fashion.
    • Capacity Management: The pool respects MaxConnPoolSize. If the total number of idle and active connections reaches this limit, GetSession will fail to create new connections.
    • Connection Validation: Before handing an idle connection to a user, the pool performs a ping() to ensure the connection is still valid. If a connection is found to be invalid, it is closed and removed from the queue.
  4. Manage database sessions with SessionPool

    master

    The SessionPool manages a pool of active Nebula Graph sessions to optimize connection reuse and handle concurrency. It maintains both idleSessions and activeSessions to fulfill requests efficiently.

    Key Lifecycle:

    1. Construct: Create the pool using NewSessionPool with a SessionPoolConf and a Logger.
    2. Execute: Use methods like Execute or ExecuteWithParameter to run queries. The pool automatically handles session acquisition, execution, and returning the session to the idle list.
    3. Release: Call Close() to log out all sessions and close all underlying connections.

    Important Behaviors:

    • Default Space: All queries are executed in the default space specified in the SessionPoolConf. If a query changes the space (e.g., via USE <space>), the pool automatically attempts to reset the session to the default space after execution.
    • Automatic Cleanup: If idleTime is configured, the pool runs a background cleaner to release expired idle sessions.
    • Retry Logic: The pool automatically retries queries if a session is found to be invalid (ErrorCode_E_SESSION_INVALID).
    import (
    	"github.com/vesoft-inc/nebula-go/v3/nebula"
    	"github.com/vesoft-inc/nebula-go/v3/nebula/graph"
    )
    
    // Example workflow
    sessionPool, err := NewSessionPool(conf, logger)
    if err != nil {
    	// handle error
    }
    defer sessionPool.Close()
    
    result, err := sessionPool.Execute("MATCH (v) RETURN v LIMIT 10")
    if err != nil {
    	// handle error
    }
  5. How Session handles reconnection

    master
    The Session implementation includes automatic reconnection logic. If a query execution fails due to a connection issue, the session attempts to reConnect() by acquiring a new idle connection from its ConnectionPool and then retries the execution once. This transparently handles transient connection drops during query execution.
  6. Configure SSL for Nebula services via Docker Compose

    master

    When deploying Nebula using the provided docker-compose.yaml, you can enable SSL by providing environment variables for the certificate paths and the SSL enablement flag. These settings are passed to the metad, storaged, and graphd services via command-line arguments.

    Required environment variables:

    • ca_path: Path to the CA certificate.
    • cert_path: Path to the service certificate.
    • key_path: Path to the private key.
    • enable_ssl: Boolean flag to enable or disable SSL.
    # Example environment variables to set in your shell or .env file
    ca_path: ./secrets/ca.crt
    cert_path: ./secrets/server.crt
    key_path: ./secrets/server.key
    enable_ssl: true
  7. Reference of generated secret files

    master

    After running the run.sh script for the various roles, the following files are generated in the directory:

    • client.cnf, client.crt, client.csr, client.key: Client credentials and configuration.
    • server.cnf, server.crt, server.csr, server.key: Server credentials and configuration.
    • root.cnf, root.crt, root.csr, root.key, root.srl: Root CA/credentials and serial file.
    • run.sh: The generation script itself.
    • readme.md: Documentation file.
    .
    ├── client.cnf
    ├── client.crt
    ├── client.csr
    ├── client.key
    ├── readme.md
    ├── root.cnf
    ├── root.crt
    ├── root.csr
    ├── root.key
    ├── root.srl
    ├── run.sh
    ├── server.cnf
    ├── server.crt
    ├── server.csr
    ├── server.key
  8. Use ValueWrapper for type-safe query result access

    master

    The ValueWrapper struct provides a type-safe way to interact with nebula.Value objects returned from query results. It allows you to check the underlying type of a value and convert it into specific Go types or specialized wrapper types (like TimeWrapper, DateTimeWrapper, Node, or Relationship).

    Type Checking

    You can verify the type of a ValueWrapper using several Is[Type] methods:

    • IsEmpty(): Returns true if the type is empty.
    • IsNull(): Returns true if the value is null.
    • IsBool(), IsInt(), IsFloat(), IsString(), IsTime(), IsDate(), IsDateTime(), IsList(), IsSet(), IsMap(), IsVertex(), IsEdge(), IsPath(), IsGeography(), IsDuration().
    • GetType(): Returns the type as a string (e.g., `
  9. Match client versions with NebulaGraph service versions

    master

    To ensure compatibility, match your nebula-go client version with your NebulaGraph service version using the following mapping:

    Client versionNebula Service Version
    v1.0.01.x.x
    v2.0.0-ga2.0.0-ga, 2.0.1
    v2.5.12.5.0
    v2.6.02.6.0
    v3.0.03.0.0
    v3.1.x3.1.x
    v3.2.x3.1.x-3.2.x
    v3.3.x3.1.x-3.3.x
    v3.4.x3.1.x-3.4.x
    v3.5.x3.1.x-3.5.x
    master3.x-nightly
  10. Implement or use the Logger interface

    master

    The Logger interface defines the standard logging contract used within the nebula_go package. You can implement this interface to redirect logs to your own logging framework (such as Zap, Logrus, or a custom system) or use the provided DefaultLogger for standard output logging.

    type Logger interface {
    	Info(msg string)
    	Warn(msg string)
    	Error(msg string)
    	Fatal(msg string)
    }
  11. Work with Nodes and Relationships

    master

    When query results contain graph entities, you can interact with them using Node and Relationship types.

    Node

    A Node represents a vertex. You can:

    • Get the vertex ID using GetID().
    • Check for specific tags using HasTag(label).
    • Retrieve tag names using GetTags().
    • Access properties of a specific tag using Properties(tagName) or Values(tagName).
    • Get all property keys for a tag using Keys(tagName).

    Relationship

    A Relationship represents an edge. You can:

    • Get the source vertex ID using GetSrcVertexID().
    • Get the destination vertex ID using GetDstVertexID().
    • Get the edge name using GetEdgeName().
    • Get the edge ranking using GetRanking().
    • Access edge properties using Properties(), Keys(), or Values().