Insert Data
This topic describes how to insert data in Milvus via client.
You can also migrate data to Milvus with MilvusDM, an open-source tool designed specifically for importing and exporting data with Milvus.
The following example inserts 2,000 rows of randomly generated data as the example data (Milvus CLI example uses a pre-built, remote CSV file containing similar data). Real applications will likely use much higher dimensional vectors than the example. You can prepare your own data to replace the example.
Prepare data
First, prepare the data to insert. Data type of the data to insert must match the schema of the collection, otherwise Milvus will raise exception.
import random
data = [
[i for i in range(2000)],
[i for i in range(10000, 12000)],
[[random.random() for _ in range(2)] for _ in range(2000)],
]
const data = Array.from({ length: 2000 }, (v,k) => ({
"book_id": k,
"word_count": k+10000,
"book_intro": Array.from({ length: 2 }, () => Math.random()),
}));
# Prepare your data in a CSV file. Milvus CLI only supports importing data from local or remote files.
Insert data to Milvus
Insert the data to the collection.
By specifying partition_name
, you can optionally decide to which partition to insert the data.
from pymilvus import Collection
collection = Collection("book") # Get an existing collection.
mr = collection.insert(data)
const mr = await milvusClient.dataManager.insert({{
collection_name: "book",
fields_data: data,
});
import -c book 'https://raw.githubusercontent.com/milvus-io/milvus_cli/main/examples/user_guide/search.csv'
Parameter | Description |
---|---|
data |
Data to insert into Milvus. |
partition_name (optional) |
Name of the partition to insert data into. |
Parameter | Description |
---|---|
collection_name |
Name of the collection to insert data into. |
partition_name (optional) |
Name of the partition to insert data into. |
fields_data |
Data to insert into Milvus. |
Option | Description |
---|---|
-c | Name of the collection to insert data into. |
-p (Optional) | Name of the partition to insert data into. |
After the data are inserted, Milvus returns the MutationResult
as an object. You can check the value of the MutationResult
, which contains the corresponding primary keys of the inserted data. As for Milvus CLI, it automatically returns the row count of the successfully inserted data after the data is inserted.
mr.primary_keys
console.log(mr.IDs)
Reading file from remote URL.
Reading csv file... [####################################] 100%
Column names are ['pk', 'example_field']
Processed 2001 lines.
Inserting ...
Insert successfully.
-------------------------- ------------------
Total insert entities: 2000
Total collection entities: 2000
Milvus timestamp: 425790736918318406
-------------------------- ------------------
Limits
Feature | Maximum limit |
---|---|
Dimensions of a vector | 32,768 |
What's next
- Learn more basic operations of Milvus:
- Explore API references for Milvus SDKs: