使用milvus-sdk-go的迭代器导出数据
创始人
2025-01-10 12:37:46
0

使用milvus-sdk-go的迭代器导出数据

迭代器是一种功能强大的工具,可帮助您使用主键值和布尔表达式迭代集合中的大量数据或所有数据。这可以显著改善您检索数据的方式。与传统的offsetlimit参数用法不同,后者可能会随着时间的推移而变得效率低下,而迭代器提供了更具可扩展性的解决方案。

当表数据很大,需要全量导出,我们可以使用迭代器,例如每次只查询1000行数据,直到所有数据查询完成,同时也可以减少服务器压力。

需要注意的是迭代器是一个客户端实现。

下面列举一个例子:写入3000条数据,每次读取100条,直至完全读完完毕。

package main  import ( 	"context" 	"fmt" 	"io" 	"log" 	"math/rand" 	"strconv"  	"github.com/milvus-io/milvus-sdk-go/v2/client" 	"github.com/milvus-io/milvus-sdk-go/v2/entity" )  const ( 	milvusAddr     = `192.168.230.71:19530` 	nEntities, dim = 3000, 128 	collectionName = "hello_iterator"  	msgFmt                                     = "==== %s ====\n" 	idCol, randomCol, addressCol, embeddingCol = "ID", "random", "address", "embeddings" 	topK                                       = 3 )  func main() { 	ctx := context.Background()  	log.Printf(msgFmt, "start connecting to Milvus") 	c, err := client.NewClient(ctx, client.Config{ 		Address: milvusAddr, 	}) 	if err != nil { 		log.Fatal("failed to connect to milvus, err: ", err.Error()) 	} 	defer c.Close()  	// delete collection if exists 	has, err := c.HasCollection(ctx, collectionName) 	if err != nil { 		log.Fatalf("failed to check collection exists, err: %v", err) 	} 	if has { 		c.DropCollection(ctx, collectionName) 	}  	// create collection 	log.Printf(msgFmt, fmt.Sprintf("create collection, `%s`", collectionName)) 	schema := entity.NewSchema().WithName(collectionName).WithDescription("hello_milvus is the simplest demo to introduce the APIs"). 		WithField(entity.NewField().WithName(idCol).WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true).WithIsAutoID(false)). 		WithField(entity.NewField().WithName(randomCol).WithDataType(entity.FieldTypeDouble)). 		WithField(entity.NewField().WithName(addressCol).WithDataType(entity.FieldTypeVarChar).WithTypeParams(entity.TypeParamMaxLength, "50")). 		WithField(entity.NewField().WithName(embeddingCol).WithDataType(entity.FieldTypeFloatVector).WithDim(dim))  	if err := c.CreateCollection(ctx, schema, entity.DefaultShardNumber); err != nil { // use default shard number 		log.Fatalf("create collection failed, err: %v", err) 	}  	// build index 	log.Printf(msgFmt, "start creating index IVF_FLAT") 	idx, err := entity.NewIndexIvfFlat(entity.L2, 128) 	if err != nil { 		log.Fatalf("failed to create ivf flat index, err: %v", err) 	} 	if err := c.CreateIndex(ctx, collectionName, embeddingCol, idx, false); err != nil { 		log.Fatalf("failed to create index, err: %v", err) 	}  	log.Printf(msgFmt, "start loading collection") 	err = c.LoadCollection(ctx, collectionName, false) 	if err != nil { 		log.Fatalf("failed to load collection, err: %v", err) 	}  	// insert data 	log.Printf(msgFmt, "start inserting random entities") 	idList, randomList := make([]int64, 0, nEntities), make([]float64, 0, nEntities) 	addressList := make([]string, 0, nEntities) 	embeddingList := make([][]float32, 0, nEntities)  	// generate data 	for i := 0; i < nEntities; i++ { 		idList = append(idList, int64(i)) 	} 	for i := 0; i < nEntities; i++ { 		randomList = append(randomList, rand.Float64()) 	} 	for i := 0; i < nEntities; i++ { 		addressList = append(addressList, "wuhan"+strconv.Itoa(i)) 	} 	for i := 0; i < nEntities; i++ { 		vec := make([]float32, 0, dim) 		for j := 0; j < dim; j++ { 			vec = append(vec, rand.Float32()) 		} 		embeddingList = append(embeddingList, vec) 	} 	idColData := entity.NewColumnInt64(idCol, idList) 	randomColData := entity.NewColumnDouble(randomCol, randomList) 	addressColData := entity.NewColumnVarChar(addressCol, addressList) 	embeddingColData := entity.NewColumnFloatVector(embeddingCol, dim, embeddingList)  	if _, err := c.Insert(ctx, collectionName, "", idColData, randomColData, addressColData, embeddingColData); err != nil { 		log.Fatalf("failed to insert random data into `hello_milvus, err: %v", err) 	}  	if err := c.Flush(ctx, collectionName, false); err != nil { 		log.Fatalf("failed to flush data, err: %v", err) 	}     // 使用迭代器,每次读取100行数据 	itr, err := c.QueryIterator(ctx, client.NewQueryIteratorOption(collectionName).WithOutputFields(idCol, randomCol, embeddingCol).WithBatchSize(100)) 	if err != nil { 		log.Fatal("failed to query iterator: ", err.Error()) 	} 	for { 		rs, err := itr.Next(ctx) 		if err != nil { 			if err == io.EOF { 				log.Println("iterator reach EOF") 				break 			} 			log.Fatal("failed to query iterator. next: ", err.Error()) 		} 		var idlist []int64 		var randomlist []float64 		for _, col := range rs { 			if col.Name() == idCol { 				idColumn := col.(*entity.ColumnInt64) 				for i := 0; i < col.Len(); i++ { 					val, err := idColumn.ValueByIdx(i) 					if err != nil { 						log.Fatal(err) 					} 					idlist = append(idlist, val) 				} 			} 			if col.Name() == randomCol { 				randomColumn := col.(*entity.ColumnDouble) 				for i := 0; i < col.Len(); i++ { 					val, err := randomColumn.ValueByIdx(i) 					if err != nil { 						log.Fatal(err) 					} 					randomlist = append(randomlist, val) 				} 			} 		} 		log.Printf("\tids: %#v\n", idlist) 		log.Printf("\trandoms: %#v\n", randomlist) 	}  	// drop collection 	log.Printf(msgFmt, "drop collection `hello_milvus`") 	if err := c.DropCollection(ctx, collectionName); err != nil { 		log.Fatalf("failed to drop collection, err: %v", err) 	} } 

相关内容

热门资讯

专做真人转动漫视频AI——Do... 家人们!今天给大家推荐一款近期火爆外网的真人视频转动漫的AI工具——DomoAI,只需提供一张图片,...
教学辅助挂(wepoKe)软件... 教学辅助挂(wepoKe)软件透明挂,(Wepoke安卓版本)外挂辅助助手,详细教程(有挂透明挂);...
人工智能在软件开发中的角色:助... 💝💝💝欢迎来到我的博客,很高兴能够在这...
人工智能 - 人脸识别:发展历... 目录一、人脸识别技术的发展历程早期探索:20世纪60至80年代技术价值点:...
实测交流!wpk安卓版(wPk... 实测交流!wpk安卓版(wPk)透视辅助!(辅助透视)详细教程(2024已更新)(哔哩哔哩);超受欢...
第3方教程(pokermast... 第3方教程(pokermaster)软件透明挂(pokermaster)透明挂辅助挂(2021已更新...
超详细Django+vue+v... 文章目录一、Django后端搭建1.1 创建项目和app1.2 注册app1.3 运行项目1.4 配...
20240618 每日AI必读...  📹北大快手联合推视频生成框架VideoTetris- VideoTetris框架成...
重大推荐!WPK程序(wpK)... 您好,WPK这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多玩家在这款游戏中...
明白辅助挂!(微扑克AI)外挂... 明白辅助挂!(微扑克AI)外挂辅助器挂!(透视)详细教程(2023已更新)(哔哩哔哩)是一款可以让一...