github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/examples/database/database.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "github.com/milvus-io/milvus-sdk-go/v2/client" 9 "github.com/milvus-io/milvus-sdk-go/v2/entity" 10 ) 11 12 func main() { 13 fmt.Println("using default database...") 14 ctx := context.Background() 15 cfg := client.Config{ 16 Address: "localhost:19530", 17 Username: "root", 18 Password: "Milvus", 19 } 20 clientDefault := mustConnect(ctx, cfg) 21 defer clientDefault.Close() 22 createCollection(ctx, clientDefault, "col1") 23 if err := clientDefault.CreateDatabase(ctx, "db1"); err != nil { 24 log.Fatalf("create db1 failed, %+v", err) 25 } 26 dbs, err := clientDefault.ListDatabases(ctx) 27 if err != nil { 28 log.Fatalf("list database failed: %+v", err) 29 } 30 fmt.Println("db:", dbs) 31 32 dbInfo, err := clientDefault.DescribeDatabase(ctx, "db1") 33 if err != nil { 34 log.Fatalf("describe default db failed") 35 } 36 fmt.Println("db props", dbInfo.Properties) 37 clientDefault.AlterDatabase(ctx, "db1", entity.DatabaseReplica(3), entity.DatabaseResourceGroups([]string{"rg1", "rg2", "rg3"})) 38 dbInfo, err = clientDefault.DescribeDatabase(ctx, "db1") 39 if err != nil { 40 log.Fatalf("describe default db failed") 41 } 42 fmt.Println("db props", dbInfo.Properties) 43 44 fmt.Println("using db1...") 45 cfg.DBName = "" 46 cfg.Address = "localhost:19530/db1" 47 clientDB1 := mustConnect(ctx, cfg) 48 defer clientDB1.Close() 49 createCollection(ctx, clientDB1, "col1") 50 createCollection(ctx, clientDB1, "col2") 51 52 fmt.Println("create collection col1 in default database...") 53 collections, err := clientDefault.ListCollections(ctx) 54 if err != nil { 55 log.Fatalf("list on default database failed: %+v", err) 56 } 57 if len(collections) != 1 { 58 log.Fatalf("unexpected count of default database, %d", len(collections)) 59 } 60 if collections[0].Name != "col1" { 61 log.Fatalf("unexpected db name of default database, %s", collections[0].Name) 62 } 63 64 fmt.Println("create collection col1, col2 in db1...") 65 collections, err = clientDB1.ListCollections(ctx) 66 if err != nil { 67 log.Fatalf("list on db1 failed: %+v", err) 68 } 69 fmt.Println("collections:", collections) 70 71 fmt.Println("create db2...") 72 if err := clientDefault.CreateDatabase(ctx, "db2"); err != nil { 73 log.Fatalf("create db2 failed, %+v", err) 74 } 75 dbs, err = clientDefault.ListDatabases(ctx) 76 if err != nil { 77 log.Fatalf("list database failed: %+v", err) 78 } 79 fmt.Println("db:", dbs) 80 81 fmt.Println("connect to db2 with existing client...") 82 clientDefault.UsingDatabase(ctx, "db2") 83 fmt.Println("create and drop collection on db2...") 84 createCollection(ctx, clientDefault, "col1") 85 if err := clientDefault.DropCollection(ctx, "col1"); err != nil { 86 log.Fatalf("drop col1 at db2 failed, %+v", err) 87 } 88 89 err = clientDefault.UsingDatabase(ctx, "") 90 if err != nil { 91 log.Fatalf("using database failed: %+v", err) 92 } 93 94 fmt.Println("drop db2: drop empty database should be always success...") 95 if err := clientDefault.DropDatabase(ctx, "db2"); err != nil { 96 log.Fatalf("drop db2 failed, %+v", err) 97 } 98 dbs, err = clientDefault.ListDatabases(ctx) 99 if err != nil { 100 log.Fatalf("list database failed: %+v", err) 101 } 102 fmt.Println("db:", dbs) 103 104 fmt.Println("drop db1: drop non-empty database should be fail...") 105 if err := clientDB1.DropDatabase(ctx, "db1"); err == nil { 106 log.Fatalf("drop an non empty db success") 107 } 108 109 fmt.Println("drop db1: drop all collection before drop db...") 110 if err := clientDB1.DropCollection(ctx, "col1"); err != nil { 111 log.Fatalf("drop col1 at db1 failed, %+v", err) 112 } 113 if err := clientDB1.DropCollection(ctx, "col2"); err != nil { 114 log.Fatalf("drop col2 at db1 failed, %+v", err) 115 } 116 if err := clientDB1.DropDatabase(ctx, "db1"); err != nil { 117 log.Fatalf("drop db1 failed, %+v", err) 118 } 119 120 fmt.Println("Drop collection on default database...") 121 if err := clientDefault.DropCollection(ctx, "col1"); err != nil { 122 log.Fatalf("drop col1 at default db failed, %+v", err) 123 } 124 125 fmt.Println("drop default: drop default database should be fail...") 126 if err := clientDefault.DropDatabase(ctx, ""); err == nil { 127 log.Fatalf("drop an default db success") 128 } 129 dbs, err = clientDefault.ListDatabases(ctx) 130 if err != nil { 131 log.Fatalf("list database failed: %+v", err) 132 } 133 fmt.Println("db:", dbs) 134 } 135 136 func mustConnect(ctx context.Context, cfg client.Config) client.Client { 137 c, err := client.NewClient(ctx, cfg) 138 if err != nil { 139 log.Fatalf("connect to database failed, %+v", err) 140 } 141 return c 142 } 143 144 func createCollection(ctx context.Context, c client.Client, collectionName string) { 145 ok, err := c.HasCollection(ctx, collectionName) 146 if err != nil { 147 log.Fatalf("%v", err) 148 } 149 if ok { 150 c.DropCollection(ctx, collectionName) 151 } 152 schema := &entity.Schema{ 153 CollectionName: collectionName, 154 Description: "database demo", 155 AutoID: false, 156 Fields: []*entity.Field{ 157 { 158 Name: "id", 159 DataType: entity.FieldTypeInt64, 160 PrimaryKey: true, 161 AutoID: false, 162 }, 163 { 164 Name: "data", 165 DataType: entity.FieldTypeDouble, 166 PrimaryKey: false, 167 AutoID: false, 168 }, 169 { 170 Name: "embeddings", 171 DataType: entity.FieldTypeFloatVector, 172 TypeParams: map[string]string{ 173 entity.TypeParamDim: fmt.Sprintf("%d", 128), 174 }, 175 }, 176 }, 177 } 178 if err := c.CreateCollection(ctx, schema, entity.DefaultShardNumber); err != nil { 179 log.Fatalf("create collection failed, err: %v", err) 180 } 181 }