Use the Session Pool to execute queries
masterThe SessionPool is the primary way to interact with NebulaGraph. It manages a pool of sessions to execute queries efficiently.
Key Constraints:
- Space Requirement: An existing space must be defined in the database before initializing the session pool.
- 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.
- Execution Context: Every call to
sessionPool.Execute()runs within the space configured during initialization. - 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)
}