AI

Create a Milvus Collection Using Golang

Milvus is an open-source vector database that is designed to store and retrieve high-dimensional vectors. It provides efficient storage and retrieval capabilities for large-scale machine learning and data analytics applications.

In this tutorial, we will learn how to create a collection in Milvus using the Milvus Go SDK. We will also cover how to connect to a Milvus cluster using Go and creating a basic collection with a defined schema.

Prerequisites:

Before we begin, make sure that you have the following prerequisites in place:

  1. An installed Go compiler on your machine
  2. A running instance of Milvus. You can use a locally hosted one or a provisioned one in the cloud.

We can proceed with the tutorial once you set up Go and Milvus.

Install the Milvus Go SDK

We need to install the Milvus Go SDK to interact with Milvus using Go.

Open your terminal or command prompt and run the following command to install the SDK on your machine:

$ go get -u github.com/milvus-io/milvus-sdk-go/v2

This command donwloads and installs the Milvus Go SDK and its dependencies.

Import the Required Packages

Next, create a new Go project and add a new file to store your source code. Next, add the import statement to include the SDK in your project:

package main

import (
    "context"
    "fmt"
    "github.com/milvus-io/milvus-sdk-go/v2/client"
    "github.com/milvus-io/milvus-sdk-go/v2/entity"
)

In the previous example, we import the context, fmt, entity, and Milvus packages from the Milvus Go SDK.

Establish a Connection with Milvus

To create a collection in Milvus, we must first establish a connection to the Milvus server. Add the following code to your Go file:

const (
    milvusAddr     = `localhost:19530`
)
func main() {
    ctx := context.Background()

    fmt.Printf(msgFmt, "start connecting to Milvus")
    c, err := client.NewClient(ctx, client.Config{
        Address: milvusAddr,
    })
    if err != nil {
        log.Fatalf("failed to connect to milvus, err: %v", err)
    }
    defer c.Close()
}

The previous code defines the variables that we need to use for the connection. In this case, we define the address to the Milvus server and the collection that we wish to create.

Finally, we use the context package to connect to the Milvus server using the specified address. Finally, we defer how to close the client connection using the defer c.Close() to ensure that the connection is properly closed when we’re done.

Create a Collection

We can create a collection now that we established a connection with Milvus. The following code shows how to create a simple collection in Milvus using Go.

The first step is to define the schema for the collection. In this case, we wish to create a collection that stores the film information. We can add the code as follows:

var (
collectionName = "film"
)
schema := &entity.Schema{
CollectionName: collectionName,
Description: "Film information",
Fields: []*entity.Field{
{
Name: "film_id",
DataType: entity.FieldTypeInt64,
PrimaryKey: true,
AutoID: false,
},
{
Name: "title",
DataType: entity.FieldTypeKeyword,
PrimaryKey: false,
AutoID: false,
},
{
Name: "genre",
DataType: entity.FieldTypeKeyword,
PrimaryKey: false,
AutoID: false,
},
{
Name: "release_year",
DataType: entity.FieldTypeInt64,
PrimaryKey: false,
AutoID: false,
},
{
Name: "rating",
DataType: entity.FieldTypeFloat,
PrimaryKey: false,
AutoID: false,
},
},
EnableDynamicField: true,
}

Once we defined the collection schema, we can use the “CreateCollection” method to define a new collection using the schema as follows:

err = milvusClient.CreateCollection(

context.Background(), // ctx

schema,

2, // shardNum

)

if err != nil {

log.Fatal("failed to create collection:", err.Error())

}

The previous code should create a new film collection with the previously-coded schema.

Conclusion

We described how to use the Milvus Go SDK to create a new collection in Milvus using a defined schema.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list