本节为您介绍如何使用Connection
对象的方法下载算法结果文件,从图集导出点和边。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
DownloadAlgoResultFile()
下载当前图集一个算法任务中的一个结果文件。
参数:
string
:文件名称。string
:生成结果文件的算法任务ID。RequestConfig
(可选):请求的配置。如果填入nil
,则使用默认的配置。func
:接收请求结果的函数
返回值:
error
:一个包含发生问题详细信息的错误对象。如果操作成功,则返回nil
。
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "miniCircle",
}
// 在图集miniCircle上运行鲁汶算法并打印任务ID
_, err1 := conn.Uql("algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})",
requestConfig)
if err1 != nil {
println(err1)
}
time.Sleep(5 * time.Second)
myTask, _ := conn.ShowTask("louvain", structs.TaskStatusDone, requestConfig)
myTaskID := myTask[0].TaskInfo.TaskID
println("TaskID is:", myTaskID)
receive := func(data []byte) error {
var fileName = "communityID"
filePath := fileName
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return err
}
return nil
}
myDownload := conn.DownloadAlgoResultFile("communityID", utils.JSONString(myTaskID), requestConfig, receive)
if myDownload != nil {
println(myDownload)
} else {
println("File is downloaded")
}
TaskID is: 65483
File is downloaded
DownloadAllAlgoResultFile()
下载当前图集一个算法任务中的全部结果文件。
参数:
string
:生成结果文件的算法任务ID。RequestConfig
(可选):请求的配置。如果填入nil
,则使用默认的配置。func
:接收请求结果的函数
返回值:
error
:一个包含发生问题详细信息的错误对象。如果操作成功,则返回nil
。
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "miniCircle",
}
// 在图集miniCircle上运行鲁汶算法并打印任务ID
_, err1 := conn.Uql("algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})",
requestConfig)
if err1 != nil {
println(err1)
}
time.Sleep(5 * time.Second)
myTask, _ := conn.ShowTask("louvain", structs.TaskStatusDone, requestConfig)
myTaskID := myTask[0].TaskInfo.TaskID
println("TaskID is:", myTaskID)
receive := func(data []byte, filename string) error {
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.Write(data)
return nil
}
myDownload := conn.DownloadAllAlgoResultFile(utils.JSONString(myTaskID), requestConfig, receive)
if myDownload != nil {
println(myDownload)
} else {
println("Files are downloaded")
}
TaskID is: 65838
Files are downloaded
Export()
从当前图集导出点和边。
参数:
ExportRequest
:导出请求的配置信息,包括dbType:ULTIPA.DBType
,schemaName:string
,limit:number
和selectPropertiesName:string[]
.Listener
:导出过程的监听器。RequestConfig
(可选):请求的配置。如果填入nil
,则使用默认的配置。
返回值:
error
:一个包含发生问题详细信息的错误对象。如果操作成功,则返回nil
。
// 从图集miniCircle名为account的schema中导出10个点,并打印结果
type MyListener struct {
Properties []string
}
func (l *MyListener) ProcessNodes(nodes []*structs.Node) error {
file, err := os.Create("node.csv")
if err != nil {
panic(err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
if nodes != nil {
for _, node := range nodes {
var sliceString []string
for _, key := range l.Properties {
vv := ConvertDataToString(node.Values.Data[key])
sliceString = append(sliceString, vv)
}
err := writer.Write(sliceString)
if err != nil {
panic(err)
}
}
}
return nil
}
func (l *MyListener) ProcessEdges(edges []*structs.Edge) error {
file, err := os.Create("edge.csv")
if err != nil {
panic(err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
if edges != nil {
for _, node := range edges {
var sliceString []string
for _, key := range l.Properties {
vv := ConvertDataToString(node.Values.Data[key])
sliceString = append(sliceString, vv)
}
err := writer.Write(sliceString)
if err != nil {
panic(err)
}
}
}
return nil
}
func ConvertDataToString(value interface{}) string {
switch value.(type) {
case int32:
v := value.(int32)
str := strconv.FormatInt(int64(v), 10)
return str
case string:
v := value.(string)
return v
case int64:
v := value.(string)
return v
case []interface{}:
var slic []string
for _, val := range value.([]interface{}) {
if vv, ok := val.(string); ok {
slic = append(slic, vv)
}
if vv, ok := val.(int32); ok {
vvv := strconv.FormatInt(int64(vv), 10)
slic = append(slic, vvv)
}
}
result := strings.Join(slic, ",")
return result
}
return ""
}
func main() {
config, _ := configuration.NewUltipaConfig(&configuration.UltipaConfig{
Hosts: []string{"192.168.1.85:60611", "192.168.1.87:60611", "192.168.1.88:60611"},
Username: "root",
Password: "root",
})
conn, _ := sdk.NewUltipa(config)
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "miniCircle",
}
var request = ultipa.ExportRequest{
Schema: "account",
SelectProperties: []string{"_id", "_uuid", "year", "name"},
DbType: ultipa.DBType_DBNODE,
Limit: 10,
}
myExport := conn.Export(&request, &MyListener{request.SelectProperties}, requestConfig)
if myExport != nil {
println(myExport)
} else {
println("File is exported")
}
File is exported
完整示例
package main
import (
"os"
"time"
"github.com/ultipa/ultipa-go-sdk/sdk"
"github.com/ultipa/ultipa-go-sdk/sdk/configuration"
"github.com/ultipa/ultipa-go-sdk/sdk/structs"
"github.com/ultipa/ultipa-go-sdk/utils"
)
func main() {
// 设置连接
// URI 示例:Hosts:=[]string{"mqj4zouys.us-east-1.cloud.ultipa.com:60010"}
config, _ := configuration.NewUltipaConfig(&configuration.UltipaConfig{
Hosts: []string{"192.168.1.85:60061", "192.168.1.86:60061", "192.168.1.87:60061"},
Username: "***",
Password: "***",
})
// 建立与数据库的连接
conn, _ := sdk.NewUltipa(config)
// 配置请求
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "miniCircle",
}
// 运行鲁汶算法并打印任务ID
_, err1 := conn.Uql("algo(louvain).params({phase1_loop_num: 20, min_modularity_increase: 0.001}).write({file:{filename_community_id: 'communityID', filename_ids: 'ids', filename_num: 'num'}})",
requestConfig)
if err1 != nil {
println(err1)
}
time.Sleep(5 * time.Second)
myTask, _ := conn.ShowTask("louvain", structs.TaskStatusDone, requestConfig)
myTaskID := myTask[0].TaskInfo.TaskID
println("TaskID is:", myTaskID)
// 下载以上算法任务生成的全部文件,打印每个文件的内容和下载响应信息
receive := func(data []byte, filename string) error {
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.Write(data)
return nil
}
myDownload := conn.DownloadAllAlgoResultFile(utils.JSONString(myTaskID), requestConfig, receive)
if myDownload != nil {
println(myDownload)
} else {
println("Files are downloaded")
}
}