DataItem
使用 UQLResponse 的 get()
、alias()
方法时将获得 DataItem 结构。
DataItem 的方法:
方法 | 类型 | 说明 |
---|---|---|
AsNodes() | []*structs.Node, map[string]*structs.Schema, error | 将 NODE 类型的 *DataItem 转为 []*structs.Node 等 |
AsFirstNode() | *structs.Node, error | 取出 NODE 类型的 *DataItem 中的第一个 *structs.Node |
AsEdges() | []*structs.Edge, map[string]*structs.Schema, error | 将 EDGE 类型的 *DataItem 转为 []*structs.Edge 等 |
AsFirstEdge() | *structs.Edge, error | 取出 EDGE 类型的 *DataItem 中的第一个 *structs.Edge |
AsPaths() | []*structs.Path, error | 将 PATH 类型的 *DataItem 转为 []*structs.Path |
AsGraphs() | []*structs.Graph, error | 将默认别名 _graph 的 *DataItem 转为 []*structs.Graph |
AsSchemas() | []*structs.Schema, error | 将默认别名 _nodeSchema、_edgeSchema 的 *DataItem 转为 []*structs.Schema |
AsProperties() | []*structs.Property, error | 将默认别名 _nodeProperty、_edgeProperty 的 *DataItem 转为 []*structs.Property |
AsAlgos() | []*structs.Algo, error | 将默认别名 _algoList 的 *DataItem 转为 []*structs.Algo |
AsTable() | *structs.Table, error | 将 TABLE 类型的 *DataItem 转为 *structs.Table |
AsArray() | *structs.Array, error | 将 ARRAY 类型的 *DataItem 转为 *structs.Array |
AsAttr() | *structs.Attr, error | 将 ATTR 类型的 *DataItem 转为 *structs.Attr |
也可以使用方法
asTable()
将默认别名 _graph、_nodeSchema、_edgeSchema 等的 *DataItem 转为 *structs.Table。
Node
structs.Node 的字段:
字段 | 类型 | 说明 |
---|---|---|
ID | types.ID | Node 的 ID |
UUID | types.UUID | Node 的 UUID |
Schema | string | Node 的 Schema |
Values | *Value | Node 的自定义属性 |
Name | string | Node 的别名 |
structs.Node 的方法:
方法 | 类型 | 说明 |
---|---|---|
GetID() | types.ID | 获取当前 Node 的 ID |
GetUUID() | types.UUID | 获取当前 Node 的 UUID |
GetSchema() | string | 获取当前 Node 的 Schema |
GetValues() | *Value | 获取当前 Node 的 Values(自定义属性) |
Get(key string) | interface{} | 获取当前 Node 的某个自定义属性 |
GetBytes(key string) | []byte, error | 获取当前 Node 的某个自定义属性的字节数组 |
Set(key string, value interface{}) | error | 设置当前 Node 的某个自定义属性,属性名不存在时将则添加该键值对 |
示例:发送 UQL 语句查询一列点,获取第二个点的 ID,将第一个点的 rating 改为 8
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("find().nodes({@movie}) as nodes return nodes{*} limit 5", nil)
nodes, schemas, _ := resp.Alias("nodes").AsNodes()
firstNode, _ := resp.Alias("nodes").AsFirstNode()
printers.PrintNodes(nodes, schemas)
fmt.Println("ID of 2nd node is: ", nodes[1].GetID())
fmt.Println("rating of 1st node was: ", firstNode.Get("rating"))
firstNode.Set("rating", int32(8))
fmt.Println("rating of 1st node now is: ", firstNode.Get("rating"))
}
输出:
+------------------------+------+--------+--------+------+
| ID | UUID | Schema | rating | year |
+------------------------+------+--------+--------+------+
| ULTIPA80000000000003E9 | 1001 | movie | 9 | 1994 |
| ULTIPA80000000000003EA | 1002 | movie | 7 | 1993 |
| ULTIPA80000000000003EB | 1003 | movie | 6 | 1998 |
| ULTIPA80000000000003EC | 1004 | movie | 9 | 1994 |
| ULTIPA80000000000003ED | 1005 | movie | 9 | 1997 |
+------------------------+------+--------+--------+------+
ID of 2nd node is: ULTIPA80000000000003EA
rating of 1st node was: 9
rating of 1st node now is: 8
Edge
structs.Edge 的字段:
字段 | 类型 | 说明 |
---|---|---|
From | types.ID | Edge 起点的 ID |
To | types.ID | Edge 终点的 ID |
FromUUID | types.UUID | Edge 起点的 UUID |
ToUUID | types.UUID | Edge 终点的 UUID |
UUID | types.UUID | Edge 的 UUID |
Schema | string | Edge 的 Schema |
Values | *Value | Edge 的自定义属性 |
Name | string | Edge 的别名 |
structs.Edge 的方法:
方法 | 类型 | 说明 |
---|---|---|
GetFrom() | types.ID | 获取当前 Edge 起点的 ID |
GetTo() | types.ID | 获取当前 Edge 终点的 ID |
GetUUID() | types.UUID | 获取当前 Edge 的 UUID |
GetSchema() | string | 获取当前 Edge 的 Schema |
GetValues() | *Value | 获取当前 Edge 的 Values(自定义属性) |
Get(key string) | interface{} | 获取当前 Edge 的某个自定义属性 |
GetBytes(key string) | []byte, error | 获取当前 Edge 的某个自定义属性的字节数组 |
Set(key string, value interface{}) | error | 设置当前 Edge 的某个自定义属性,属性名不存在时将则添加该键值对 |
示例:发送 UQL 语句查询一列边,获取第二个边的起点的 ID,为第一个边添加属性 value 并设置为 8
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("find().edges({@default}) as edges return edges{*} limit 5", nil)
edges, schemas, _ := resp.Alias("edges").AsEdges()
firstEdge, _ := resp.Alias("edges").AsFirstEdge()
printers.PrintEdges(edges, schemas)
fmt.Println("ID of start node of 2nd edge is: ", edges[1].GetFrom())
fmt.Println("value of 1st edge was: ", firstEdge.Get("value"))
firstEdge.Set("value", int32(8))
fmt.Println("value of 1st edge now is: ", firstEdge.Get("value"))
}
输出:
+------+------------------------+------------------------+---------+
| UUID | FROM | TO | SCHEMA |
+------+------------------------+------------------------+---------+
| 21 | ULTIPA0000003 | ULTIPA0000004 | default |
| 24 | ULTIPA8000000000000001 | ULTIPA8000000000000002 | default |
| 29 | ULTIPA800000000000001A | ULTIPA800000000000001B | default |
+------+------------------------+------------------------+---------+
ID of start node of 2nd edge is: ULTIPA8000000000000001
value of 1st edge was: <nil>
value of 1st edge now is: 8
Path
structs.Path 的字段:
字段 | 类型 | 说明 |
---|---|---|
Nodes | []*Node | Path 中的 *Node 列表 |
Edges | []*Edge | Path 中的 *Edge 列表 |
NodeSchemas | map[string]*Schema | Path 中的所有点 *Schema 的映射 |
EdgeSchemas | map[string]*Schema | Path 中的所有边 *Schema 的映射 |
Name | string | Path 的别名 |
structs.Path 的方法:
方法 | 类型 | 说明 |
---|---|---|
GetLength() | int | 获取当前 Path 的长度,即 Edge 的数量 |
GetNodes() | []*Node | 获取当前 Path 的 *Node 列表 |
GetEdges() | []*Edge | 获取当前 Path 的 *Edge 列表 |
GetLastNode() | *Node | 获取当前 Path 的最后一个 *Node |
示例:发送 UQL 语句查询一列路径,获取第一个路径的点列
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("n().e()[2].n() as paths return paths{*} limit 3", nil)
paths, _ := resp.Alias("paths").AsPaths()
printers.PrintPaths(paths)
printers.PrintNodes(paths[0].GetNodes(), paths[0].NodeSchemas)
}
输出:
+---+---------------------------------------------------------+
| # | Path |
+---+---------------------------------------------------------+
| 0 | (CARD00001) <- [14] - (CARD00020) <- [25] - (CARD00019) |
| 1 | (CARD00001) <- [39] - (CARD00024) <- [22] - (CARD00045) |
| 2 | (CARD00001) <- [53] - (CARD00022) <- [13] - (CARD00042) |
+---+---------------------------------------------------------+
+-----------+------+---------+----------+-------+
| ID | UUID | Schema | balance | level |
+-----------+------+---------+----------+-------+
| CARD00001 | 1 | default | 4245.3 | 3 |
| CARD00020 | 20 | default | 2564.0 | 2 |
| CARD00019 | 19 | default | 244.8 | 2 |
+-----------+------+---------+----------+-------+
Graph
structs.Graph 的字段:
字段 | 类型 | 说明 |
---|---|---|
ID | types.ID | Graph 的 ID |
Name | string | Graph 的名称 |
Description | string | Graph 的描述 |
TotalNodes | uint64 | Graph 的点数量 |
TotalEdges | uint64 | Graph 的边数量 |
Status | string | Graph 的状态(MOUNTED、UNMOUNTED) |
示例:发送 UQL 语句查询图集列表
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("show().graph()", nil)
graphs, _ := resp.Alias("_graph").AsGraphs()
printers.PrintGraph(graphs)
}
输出:
+----------------+-------------+------------+------------+-----------+
| Name | Description | Total Node | Total Edge | Status |
+----------------+-------------+------------+------------+-----------+
| DeliveryCenter | | 30 | 77 | MOUNTED |
| GithubSocial | | 37700 | 289003 | MOUNTED |
| HB_POC | | 0 | 0 | UNMOUNTED |
| Industry | | 0 | 0 | MOUNTED |
+----------------+-------------+------------+------------+-----------+
Schema
structs.Schema 的字段:
字段 | 类型 | 说明 |
---|---|---|
Name | string | Schema 的名称 |
Properties | []*Property | Schema 的 *Property 列表 |
Desc | string | Schema 的描述 |
Type | string | Schema 的点/边类型 |
DBType | ultipa.DBType | Schema 的点/边类型 |
Total | int | Schema 的点/边数量 |
structs.Schema 的方法:
方法 | 类型 | 说明 |
---|---|---|
GetProperty(name string) | *Property | 按名称获取当前 Schema 的某个 *Property |
示例:发送 UQL 语句查询当前图集的点 schema 列表
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("show().node_schema()", nil)
nodeSchemas, _ := resp.Alias("_nodeSchema").AsSchemas()
printers.PrintSchema(nodeSchemas)
}
输出:
Schema Name: default ( 3 )
Description: default schema
-
Schema Name: movie ( 92 )
Description:
+-----------+-------------+-----------+-------+--------+
| Name | Description | Type | LTE | Schema |
+-----------+-------------+-----------+-------+--------+
| note | | string | false | movie |
| frating | | float | false | movie |
| timestamp | | timestamp | false | movie |
| drating | | double | false | movie |
| genre | | string | false | movie |
| datetime | | datetime | false | movie |
| name | | string | false | movie |
| rating | | int32 | false | movie |
| year | | int32 | true | movie |
+-----------+-------------+-----------+-------+--------+
-
Schema Name: country ( 23 )
Description:
+------+-------------+--------+-------+---------+
| Name | Description | Type | LTE | Schema |
+------+-------------+--------+-------+---------+
| name | | string | false | country |
+------+-------------+--------+-------+---------+
-
Property
structs.Property 的字段:
字段 | 类型 | 说明 |
---|---|---|
Name | string | Property 的名称 |
Desc | string | Property 的描述 |
Schema | string | Property 的 Schema |
Type | ultipa.PropertyType | Property 的数据类型 |
Lte | bool | Property 的 LTE 状态(true、false) |
示例:发送 UQL 语句查询当前图集的点属性列表
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("show().node_property()", nil)
nodeProperties, _ := resp.Alias("_nodeProperty").AsProperties()
printers.PrintProperty(nodeProperties)
}
输出:
+-----------+-------------+-----------+-------+-----------+
| Name | Description | Type | LTE | Schema |
+-----------+-------------+-----------+-------+-----------+
| note | | string | false | movie |
| frating | | float | false | movie |
| timestamp | | timestamp | false | movie |
| drating | | double | false | movie |
| genre | | string | false | movie |
| datetime | | datetime | false | movie |
| name | | string | false | movie |
| rating | | int32 | false | movie |
| year | | int32 | true | movie |
| name | | string | false | country |
| name | | string | false | celebrity |
| name | | string | false | account |
| industry | | string | false | account |
| gender | | string | false | account |
| year | | int32 | true | account |
+-----------+-------------+-----------+-------+-----------+
Algo
structs.Algo 的字段:
字段 | 类型 | 说明 |
---|---|---|
Name | string | Algo 的名称 |
Desc | string | Algo 的描述 |
Version | string | Algo 的版本 |
Params | map[string]*AlgoParam | Algo 的参数列表 |
structs.Algo 的方法:
方法 | 类型 | 说明 |
---|---|---|
ParamsToString() | string | 将当前 Algo 的 Params 转换成字符串 |
示例:发送 UQL 语句查询已安装的算法列表
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("show().algo()", nil)
algos, _ := resp.Alias("_algoList").AsAlgos()
printers.PrintAlgoList(algos)
}
输出:
+-------------------+-------------------------------------+---------+----------------------------------------------------------+
| Algo Name | Description | Version | Parameters |
+-------------------+-------------------------------------+---------+----------------------------------------------------------+
| triangle_counting | triangle counting | 1.0.2 | type : 1:count by edge 2:count by node |
| | | | limit : -1 for all triples |
| | | | result_type : 1:only number 2:triangles |
| | | | |
| sybil_rank | sybil rank | 1.0.1 | trust_seeds : array of nodes,required |
| | | | loop_num : size_t,required |
| | | | limit : optional,-1 for all results, >=0 partial results |
| | | | total_trust : float,required |
| | | | |
| subgraph | subgraph generated by certain nodes | 1.0.2 | ids : array of nodes,required |
| | | | limit : optional,-1 for all results, >=0 partial results |
| | | | |
+-------------------+-------------------------------------+---------+----------------------------------------------------------+
Table
structs.Table 的字段:
字段 | 类型 | 说明 |
---|---|---|
Name | string | Table 的别名 |
Headers | []*Property | Table 的表头列表 |
Rows | []*Row | Table 的所有行列表 |
structs.Table 的方法:
方法 | 类型 | 说明 |
---|---|---|
GetHeaders() | []*Property | 获取当前 Table 的表头列表 |
GetRows() | []*Row | 获取当前 Table 的所有行列表 |
ToKV() | []*Values | 将当前 Table 转换为 KV 列表 |
示例:发送 UQL 语句查询一个表格,获取该表格表头及第二行数据
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("find().nodes({@account}) limit 5 return table(nodes.gender, nodes.year) as myTable", nil)
table, _ := resp.Alias("myTable").AsTable()
printers.PrintTable(table)
printers.PrintProperty(table.GetHeaders())
fmt.Println(*table.GetRows()[0])
}
输出:
+--------------+------------+
| nodes.gender | nodes.year |
+--------------+------------+
| female | 1978 |
| female | 1989 |
| male | 1982 |
| female | 2007 |
| male | 1973 |
+--------------+------------+
+--------------+-------------+--------+-------+--------+
| Name | Description | Type | LTE | Schema |
+--------------+-------------+--------+-------+--------+
| nodes.gender | | string | false | |
| nodes.year | | int32 | false | |
+--------------+-------------+--------+-------+--------+
[female 1978]
Array
structs.Array 的字段:
字段 | 类型 | 说明 |
---|---|---|
Name | string | Array 的别名 |
Rows | []*Row | Array 的所有行列表 |
示例:发送 UQL 语句查询一列数组,获取第二个数组的第三个元素
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("find().nodes({@account}) as nodes group by nodes.industry return collect(nodes.name) as myArray limit 3", nil)
array, _ := resp.Alias("myArray").AsArray()
printers.PrintArray(array)
fmt.Println("3rd element of 2nd array is: ", (*array.Rows[1])[2])
}
输出:
+-------------------------------------------------------------+
| myArray |
+-------------------------------------------------------------+
| ["jibber-jabber", "jo", "CR.", "laofung", "pepe"] |
| ["Meng", "HolyC", "pixiedust", "sadmov"] |
| ["Unbeliever", "Ivo", "Nannobrycon", "auric", "Blair_self"] |
+-------------------------------------------------------------+
3rd element of 2nd array is: pixiedust
Attr
structs.Attr 的字段:
字段 | 类型 | 说明 |
---|---|---|
Name | string | Attr 的别名 |
Rows | Row | Attr 的列表 |
PropertyType | ultipa.PropertyType | Attr 的类型 |
示例:发送 UQL 语句查询一列属性,获取第二个属性
func TestMisc(t *testing.T) {
// 创建名为 conn 的连接,此部分代码省略
resp, _ := conn.UQL("find().nodes({@account}) as nodes return nodes.industry as myAttr limit 5", nil)
attribute, _ := resp.Alias("myAttr").AsAttr()
printers.PrintAttr(attribute)
fmt.Println("2nd attr is: ", attribute.Rows[1])
}
输出:
+---------------+
| myAttr |
+---------------+
| Manufacturing |
| Health |
| Other |
| Education |
| Publishing |
+---------------+
2nd attr is: Health