UltipaDocs
Try Playground
  • Introduction
    • Installation
    • Connection
      • Overview and Request Configuration
      • UQL Execution
      • GQL Execution
      • Graph
      • Schema and Property
      • Data Insertion
      • Query Acceleration
      • HDC Graph and Algorithm
      • Data Export
      • Process and Job
      • Access Control
    • Result Processing
    • Driver Data Classes
    • Installation
    • Connection
      • Overview and Request Configuration
      • UQL Execution
      • GQL Execution
      • Graph
      • Schema and Property
      • Data Insertion
      • Query Acceleration
      • HDC Graph and Algorithm
      • Data Export
      • Process and Job
      • Access Control
    • Result Processing
    • Driver Data Classes
    • Quick Start
    • Connect to Database
    • Query the Database
      • Graph
      • Schema and Property
      • Data Insertion
      • Query Acceleration
      • HDC Graph and Algorithm
      • Data Export
      • Process and Job
      • Access Control
    • Process Query Results
    • Data Structures
    • Quick Start
    • Connect to Database
    • Query the Database
      • Graph
      • Schema and Property
      • Data Insertion
      • Query Acceleration
      • HDC Graph and Algorithm
      • Data Export
      • Process and Job
      • Access Control
    • Process Query Results
    • Data Structures
    • Installation
    • Connection
    • Request Configuration
    • UQL Execution
    • GQL Execution
    • Graphset Management
    • Schema and Property Management
    • Data Insertion and Deletion
    • Query Acceleration
    • Algorithm Management
    • Downloads and Exports
    • Process and Task Management
    • Access Management
    • Server Statistics
    • Result Processing
    • Types Mapping Ultipa and C#
  • RESTful API
  1. Docs
  2. /
  3. Ultipa Drivers
  4. /
  5. Convenience Methods

Data Insertion

This section introduces methods for the insertion of nodes and edges.

MethodsMechanismUse CaseNote
insertNodes()
insertEdges()
Uses UQL under the hood.Inserts a small number of nodes or edges.
insertNodesBatchBySchema()
insertEdgesBatchBySchema()
Uses gRPC to send data directly to the server.Inserts large volumes of nodes or edges of the same schema.The property values must be assigned using Go data types that correspond to the Ultipa supported property types (see Property Type Mapping).
insertNodesBatchAuto()
insertEdgesBatchAuto()
Inserts large volumes of nodes or edges of different schemas.

Property Type Mapping

The mappings between Ultipa property types and Go data types are as follows:

Ultipa Property Type
Go Data Type
Examples
INT32int3218
UINT32uint3218
INT64int6418
UINT64uint6418
FLOATfloat32170.5
DOUBLEfloat6465.32
DECIMALint32, int64, float32, float64, uint32, uint64, or string18, 170.5, "65.32"
STRINGstring"John Doe"
TEXTstring"John Doe"
LOCAL_DATETIMEstring[1]"1993-05-06 09:11:02"
ZONED_DATETIMEstring[1]"1993-05-06 09:11:02-0800"
DATEstring[1]"1993-05-06"
LOCAL_TIMEstring[1]"09:11:02"
ZONED_TIMEstring[1]"09:11:02-0800"
DATETIMEstring[2]"1993-05-06"
TIMESTAMPstring[2], or int"1993-05-06", "1715169600"
YEAR_TO_MONTHstringP2Y5M, -P1Y5M
DAY_TO_SECONDstringP3DT4H, -P1DT2H3M4.12S
BOOLBooleantrue, false
BOOLbooltrue or false
POINTstring, NewPoint"point({latitude: 132.1, longitude: -1.5})", types.NewPoint(132.1, -1.5)
LISTslice["tennis", "violin"]
SETslice[2004, 3025, 1025]

[1] Supported date formats include YYYY-MM-DD and YYYYMMDD. Supported time formats include HH:MM:SS[.fraction] and HHMMSS[.fraction]. Date and time components are joined by either a space or the letter T. Supported timezone formats include ±HH:MM and ±HHMM.

[2] Supported date string formats include [YY]YY-MM-DD HH:MM:SS, [YY]YY-MM-DD HH:MM:SSZ, [YY]YY-MM-DDTHH:MM:SSZ, [YY]YY-MM-DDTHH:MM:SSXX, [YY]YY-MM-DDTHH:MM:SSXXX, [YY]YY-MM-DD HH:MM:SS.SSS and their variations.

Example Graph Structure

The examples in this section demonstrate the insertion and deletion of nodes and edges in a graph based on the following schema and property definitions:

To create this graph structure, see the example provided here.

InsertNodes()

Inserts nodes to a schema in the graph.

Parameters

  • schemaName: string: Schema name.
  • nodes: []*structs.Node: The list of nodes to be inserted.
  • config: *configuration.InsertRequestConfig (Optional): Request configuration.

Returns

  • Response: Response of the request.
  • error: An error object that contains details about any issues encountered during the operation. If the operation succeeds, nil is returned.
Go
// Inserts two 'user' nodes into the graph 'social'

requestConfig := &configuration.RequestConfig{
    Graph: "social",
}

insertRequestConfig := &configuration.InsertRequestConfig{
    RequestConfig: requestConfig,
}

nodes := []*structs.Node{
    {
    	ID: "U1",
    	Values: &structs.Values{
      		Data: map[string]interface{}{
        		"name":            "Alice",
        		"age":             18,
        		"score":           65.32,
        		"birthday":        "1993-05-04",
        		"active":          0,
        		"location":        "point({latitude: 132.1, longitude: -1.5})",
        		"interests":       []string{"tennis", "violin"},
        		"permissionCodes": []int32{2004, 3025, 1025},
      		},
    	},
    },
    {
    	ID: "U2",
    	Values: &structs.Values{
      		Data: map[string]interface{}{
        		"name": "Bob",
      		},
    	},
  	},
}

response, _ := driver.InsertNodes("user", nodes, insertRequestConfig)
if response.Status.Code.String() == "SUCCESS" {
    fmt.Println(response.Status.Code)
} else {
    fmt.Println(response.Status.Message)
}
Output
SUCCESS

InsertEdges()

Inserts edges to a schema in the graph.

Parameters

  • schemaName: string: Schema name.
  • edges: []*structs.Edge: The list of edges to be inserted; the fields From and To of each Edge are mandatory.
  • config: *configuration.InsertRequestConfig (Optional): Request configuration.

Returns

  • Response: Response of the request.
  • error: An error object that contains details about any issues encountered during the operation. If the operation succeeds, nil is returned.
Go
// Inserts two 'follows' edges to the graph 'social'

requestConfig := &configuration.RequestConfig{
    Graph: "social",
}

insertRequestConfig := &configuration.InsertRequestConfig{
    RequestConfig: requestConfig,
}

edges := []*structs.Edge{
    {
    	From: "U1",
    	To:   "U2",
    	Values: &structs.Values{
      		Data: map[string]interface{}{
        		"createdOn": "2024-5-6",
        		"weight":    3.2,
      		},
    	},
  	},
  	{
    	From: "U2",
    	To:   "U1",
    	Values: &structs.Values{
      		Data: map[string]interface{}{
        		"createdOn": 1715169600,
      		},
    	},
  	},
}

response, _ := driver.InsertEdges("follows", edges, insertRequestConfig)
if response.Status.Code.String() == "SUCCESS" {
    fmt.Println(response.Status.Code)
} else {
    fmt.Println(response.Status.Message)
}
Output
SUCCESS

InsertNodesBatchBySchema()

Inserts nodes to a schema in the graph through gRPC. This method is optimized for bulk insertion.

Parameters

  • schema: *structs.Schema: The target schema; the fields Name is mandatory, Properties includes partial or all properties defined for the corresponding schema in the graph.
  • nodes: []*structs.Node: The list of nodes to be inserted.
  • config: *configuration.InsertRequestConfig (Optional): Request configuration.

Returns

  • InsertResponse: Result of the request.
  • error: An error object that contains details about any issues encountered during the operation. If the operation succeeds, nil is returned.
Go
// Inserts two 'user' nodes into the graph 'social'

requestConfig := &configuration.RequestConfig{
  Graph: "social",
}

insertRequestConfig := &configuration.InsertRequestConfig{
  RequestConfig: requestConfig,
}

schema := &structs.Schema{
  Name: "user",
  Properties: []*structs.Property{
    {Name: "name", Type: ultipa.PropertyType_STRING},
    {Name: "age", Type: ultipa.PropertyType_INT32},
    {Name: "score", Type: ultipa.PropertyType_DECIMAL, DecimalExtra: &structs.DecimalExtra{Precision: 25, Scale: 10}},
    {Name: "birthday", Type: ultipa.PropertyType_DATE},
    {Name: "active", Type: ultipa.PropertyType_BOOL},
    {Name: "location", Type: ultipa.PropertyType_POINT},
    {Name: "interests", Type: ultipa.PropertyType_LIST, SubTypes: []ultipa.PropertyType{ultipa.PropertyType_STRING}},
    {Name: "permissionCodes", Type: ultipa.PropertyType_SET, SubTypes: []ultipa.PropertyType{ultipa.PropertyType_INT32}},
  },
}

nodes := []*structs.Node{
  {
    ID: "U1",
    Values: &structs.Values{
      Data: map[string]interface{}{
        "name":            "Alice",
        "age":             18,
        "score":           65.32,
        "birthday":        "1993-05-04",
        "active":          false,
        "location":        types.NewPoint(132.1, -1.5),
        "interests":       []string{"tennis", "violin"},
        "permissionCodes": []int32{2004, 3025, 1025},
      },
    },
  },
  {
    ID: "U2",
    Values: &structs.Values{
      Data: map[string]interface{}{
        "name": "Bob",
      },
    },
  },
}

insertResponse, err := driver.InsertNodesBatchBySchema(schema, nodes, insertRequestConfig)
if err != nil {
  log.Fatalf("Insert failed: %v", err)
}

if insertResponse != nil && len(insertResponse.ErrorItems) > 0 {
  fmt.Println("Error items:", insertResponse.ErrorItems)
} else {
  fmt.Println("All nodes inserted successfully")
}
Output
All nodes inserted successfully

InsertEdgesBatchBySchema()

Inserts edges to a schema in the graph through gRPC. This method is optimized for bulk insertion.

Parameters

  • schema: *structs.Schema: The target schema; the fields Name is mandatory, Properties includes partial or all properties defined for the corresponding schema in the graph.
  • edges: []*structs.Edge: The list of edges to be inserted; the fields From and To of each Edge are mandatory.
  • config: *configuration.InsertRequestConfig (Optional): Request configuration.

Returns

  • InsertResponse: Result of the request.
  • error: An error object that contains details about any issues encountered during the operation. If the operation succeeds, nil is returned.
Go
// Inserts two 'follows' edges into the graph 'social'

requestConfig := &configuration.RequestConfig{
  Graph: "social",
}

insertRequestConfig := &configuration.InsertRequestConfig{
  RequestConfig: requestConfig,
}

schema := &structs.Schema{
  Name: "follows",
  Properties: []*structs.Property{
    {Name: "createdOn", Type: ultipa.PropertyType_TIMESTAMP},
    {Name: "weight", Type: ultipa.PropertyType_FLOAT},
  },
}

edges := []*structs.Edge{
  {
    From: "U1",
    To:   "U2",
    Values: &structs.Values{
      Data: map[string]interface{}{
        "createdOn": "2024-5-6",
        "weight":    float32(3.2),
      },
    },
  },
  {
    From:   "U2",
    To:     "U1",
    Values: &structs.Values{},
  },
}

insertResponse, err := driver.InsertEdgesBatchBySchema(schema, edges, insertRequestConfig)
if err != nil {
  log.Fatalf("Insert failed: %v", err)
}

if insertResponse != nil && len(insertResponse.ErrorItems) > 0 {
  fmt.Println("Error items:", insertResponse.ErrorItems)
} else {
  fmt.Println("All edges inserted successfully")
}
Output
All edges inserted successfully

InsertNodesBatchAuto()

Inserts nodes to one or multipe schemas in the graph through gRPC. This method is optimized for bulk insertion.

Parameters

  • nodes: []*structs.Node: The list of nodes to be inserted; the field Schema of each Node are mandatory, the Values includes partial or all properties defined for the corresponding schema in the graph.
  • config: *configuration.InsertRequestConfig (Optional): Request configuration.

Returns

  • map[string]*http.InsertResponse: The schema name, and response of the insertion request.
  • error: An error object that contains details about any issues encountered during the operation. If the operation succeeds, nil is returned.
Go
// Inserts two 'user' nodes and a 'product' node into the graph 'social'

requestConfig := &configuration.RequestConfig{
  Graph: "social",
}

insertRequestConfig := &configuration.InsertRequestConfig{
  RequestConfig: requestConfig,
}

nodes := []*structs.Node{
  {
    Schema: "user",
    ID:     "U1",
    Values: &structs.Values{
      Data: map[string]interface{}{
        "name":            "Alice",
        "age":             18,
        "score":           65.32,
        "birthday":        "1993-05-04",
        "active":          false,
        "location":        types.NewPoint(132.1, -1.5),
        "interests":       []string{"tennis", "violin"},
        "permissionCodes": []int32{2004, 3025, 1025},
      },
    },
  },
  {
    Schema: "user",
    ID:     "U2",
    Values: &structs.Values{
      Data: map[string]interface{}{
        "name": "Bob",
      },
    },
  },
  {
    Schema: "product",
    Values: &structs.Values{
      Data: map[string]interface{}{
        "name":  "Wireless Earbud",
        "price": float32(93.2),
      },
    },
  },
}

result, err := driver.InsertNodesBatchAuto(nodes, insertRequestConfig)
if err != nil {
  log.Fatalf("Insert failed: %v", err)
}

for schemaName, insertResponse := range result {
  if len(insertResponse.ErrorItems) > 0 {
    fmt.Println("Error items of", schemaName, "nodes:", insertResponse.ErrorItems)
  } else {
    fmt.Println("All", schemaName, "nodes inserted successfully")
  }
}
Output
All product nodes inserted successfully
All user nodes inserted successfully

InsertEdgesBatchAuto()

Inserts edges to one or multipe schemas in the graph through gRPC. This method is optimized for bulk insertion.

Parameters

  • edges: []*structs.Edge: The list of edges to be inserted; the fields Schema, From, and To of each Edge are mandatory, Values includes partial or all properties defined for the corresponding schema in the graph.
  • config: *configuration.InsertRequestConfig (Optional): Request configuration.

Returns

  • map[string]*http.InsertResponse: The schema name, and response of the insertion request.
  • error: An error object that contains details about any issues encountered during the operation. If the operation succeeds, nil is returned.
Go
// Inserts two 'follows' edges and a 'purchased' edge into the graph 'social'

requestConfig := &configuration.RequestConfig{
    Graph: "social",
}

insertRequestConfig := &configuration.InsertRequestConfig{
  	RequestConfig: requestConfig,
}

edges := []*structs.Edge{
  	{
    	Schema: "follows",
    	From:   "U1",
    	To:     "U2",
    	Values: &structs.Values{
      		Data: map[string]interface{}{
        		"createdOn": "2024-5-6",
        		"weight":    float32(3.2),
      		},
    	},
  	},
  	{
    	Schema: "follows",
    	From:   "U2",
    	To:     "U1",
    	Values: &structs.Values{
      		Data: map[string]interface{}{
        		"createdOn": 1715169600,
      		},
    	},
  	},
  	{
    	Schema: "purchased",
    	From:   "U2",
    	To:     "684bd6a70000020020000001",
  	},
}

result, err := driver.InsertEdgesBatchAuto(edges, insertRequestConfig)
if err != nil {
  	log.Fatalf("Insert failed: %v", err)
}

for schemaName, insertResponse := range result {
  	if len(insertResponse.ErrorItems) > 0 {
    	fmt.Println("Error items of", schemaName, "edges:", insertResponse.ErrorItems)
  	} else {
    	fmt.Println("All", schemaName, "edges inserted successfully")
  	}
}
Output
All follows edges inserted successfully
All purchased edges inserted successfully

Full Example

Go
package main

import (
	"fmt"
	"log"

	"github.com/ultipa/ultipa-go-driver/v5/sdk"
	"github.com/ultipa/ultipa-go-driver/v5/sdk/configuration"
	"github.com/ultipa/ultipa-go-driver/v5/sdk/structs"
	"github.com/ultipa/ultipa-go-driver/v5/sdk/types"
)

func main() {
	config := &configuration.UltipaConfig{
		// URI example:	Hosts: []string{"mqj4zouys.us-east-1.cloud.ultipa.com:60010"},
		Hosts:    []string{"192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"},
		Username: "<usernmae>",
		Password: "<password>",
	}

	driver, err := sdk.NewUltipaDriver(config)
	if err != nil {
		log.Fatalln("Failed to connect to Ultipa:", err)
	}

	// Inserts two 'user' nodes, a 'product' node, two 'follows' edges, and a 'purchased' edge into the graph 'social'

	requestConfig := &configuration.RequestConfig{
		Graph: "social",
	}

	insertRequestConfig := &configuration.InsertRequestConfig{
		RequestConfig: requestConfig,
	}

	nodes := []*structs.Node{
		{
			Schema: "user",
			ID:     "U1",
			Values: &structs.Values{
				Data: map[string]interface{}{
					"name":            "Alice",
					"age":             18,
					"score":           65.32,
					"birthday":        "1993-05-04",
					"active":          false,
					"location":        types.NewPoint(132.1, -1.5),
					"interests":       []string{"tennis", "violin"},
					"permissionCodes": []int32{2004, 3025, 1025},
				},
			},
		},
		{
			Schema: "user",
			ID:     "U2",
			Values: &structs.Values{
				Data: map[string]interface{}{
					"name": "Bob",
				},
			},
		},
		{
			Schema: "product",
			ID:     "P1",
			Values: &structs.Values{
				Data: map[string]interface{}{
					"name":  "Wireless Earbud",
					"price": float32(93.2),
				},
			},
		},
	}

	edges := []*structs.Edge{
		{
			Schema: "follows",
			From:   "U1",
			To:     "U2",
			Values: &structs.Values{
				Data: map[string]interface{}{
					"createdOn": "2024-5-6",
					"weight":    float32(3.2),
				},
			},
		},
		{
			Schema: "follows",
			From:   "U2",
			To:     "U1",
			Values: &structs.Values{
				Data: map[string]interface{}{
					"createdOn": 1715169600,
				},
			},
		},
		{
			Schema: "purchased",
			From:   "U2",
			To:     "P1",
		},
	}

	result_n, err := driver.InsertNodesBatchAuto(nodes, insertRequestConfig)
	if err != nil {
		log.Fatalf("Insert failed: %v", err)
	}

	for schemaName, insertResponse := range result_n {
		if len(insertResponse.ErrorItems) > 0 {
			fmt.Println("Error items of", schemaName, "nodes:", insertResponse.ErrorItems)
		} else {
			fmt.Println("All", schemaName, "nodes inserted successfully")
		}
	}

	result_e, err := driver.InsertEdgesBatchAuto(edges, insertRequestConfig)
	if err != nil {
		log.Fatalf("Insert failed: %v", err)
	}

	for schemaName, insertResponse := range result_e {
		if len(insertResponse.ErrorItems) > 0 {
			fmt.Println("Error items of", schemaName, "edges:", insertResponse.ErrorItems)
		} else {
			fmt.Println("All", schemaName, "edges inserted successfully")
		}
	}
}