milvus-logo
LFAI
< Docs
  • Python
    • MilvusClient

insert()

This method inserts entities into a collection. You can insert data through either a list of rows or a list of columns.

Invocation

insert(
    collection_name,
    data,
    batch_size,
    progress_bar,
    timeout
)

Parameters

ParameterDescriptionTypeRequired
collection_nameName of the collection to which data is inserted.StringTrue
dataA list of dictionaries to insert. Each dictionary represents an entity. All fields defined in the schema are required and should be included as keys in the dictionaries. You can also add dynamic keys for any fields that are not defined in the schema.list[Dictionary]True
batch_sizeSize of entities to insert in a batch.IntegerFalse
progress_barWhether to display a progress bar.BooleanFalse
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse

Return

A list of primary keys inserted.

Raises

DataNotMatchException: Error if the data has misssing fields.

MilvusException: Error if a general Milvus error occurred.

Example

  • Insert data through a list of rows:

    # Prepare data as a list of rows
    import json
    
    with open('/path/to/downloaded/dataset') as f:
        data = json.load(f)
        list_of_rows = data['rows'][0]
    
    # Create a MilvusClient instance
    from pymilvus import MilvusClient
    
    client = MilvusClient(uri, token)
    
    client.insert(list_of_rows)
    
  • Insert data through a list of columns:

    # Prepare data as a list of columns
    import json
    
    with open('/path/to/downloaded/dataset') as f:
        data = json.load(f)
        keys = data['rows'][0].keys()
        entity = data['rows'][0]
        list_of_columns = [ [entity.get(x)] for x in keys]
    
    # Create a MilvusClient instance
    from pymilvus import MilvusClient
    
    client = MilvusClient(uri, token)
    
    client.insert(list_of_columns)
    
Feedback

Was this page helpful?