milvus-logo
LFAI
< Docs
  • Java
    • v1

insert()

A MilvusClient interface. This method inserts entities into a specified collection.

R<InsertResponse> insert(InsertRowsParam requestParam);

InsertRowsParam

Use the InsertRowsParam.Builder to construct an InsertRowsParam object.

import io.milvus.param.highlevel.dml.InsertRowsParam;
InsertRowsParam.Builder builder = InsertRowsParam.newBuilder();

Methods of InsertRowsParam.Builder:

Method

Description

Parameters

withCollectionName(String collectionName)

Sets the target collection name. Collection name cannot be empty or null.

collectionName: The name of the collection to insert data into.

withRows(List<gson.JsonObject> rows)

Sets the row data to insert. The rows list cannot be empty.
Note that no input is required for the primary key field if auto-ID is enabled.

rows: A list of gson.JsonObject objects, each representing a row data.
For each field:
- If dataType is Bool/Int8/Int16/Int32/Int64/Float/Double/Varchar, use JsonObject.addProperty(key, value) to input;
- If dataType is FloatVector, use JsonObject.add(key, gson.toJsonTree(List[Float]) to input;
- If dataType is BinaryVector/Float16Vector/BFloat16Vector, use JsonObject.add(key, gson.toJsonTree(byte[])) to input;
- If dataType is SparseFloatVector, use JsonObject.add(key, gson.toJsonTree(SortedMap[Long, Float])) to input;
- If dataType is Array, use JsonObject.add(key, gson.toJsonTree(List of Boolean/Integer/Short/Long/Float/Double/String)) to input;
- If dataType is JSON, use JsonObject.add(key, JsonElement) to input;
Note:
1. For scalar numeric values, value will be cut according to the type of the field.
For example:
An Int8 field named "XX", you set the value to be 128 by JsonObject.addProperty("XX", 128), the value 128 is cut to -128.
An Int64 field named "XX", you set the value to be 3.9 by JsonObject.addProperty("XX", 3.9), the value 3.9 is cut to 3.
2. String value can be parsed to numeric/boolean type if the value is valid.
For example:
A Bool field named "XX", you set the value to be "TRUE" by JsonObject.addProperty("XX", "TRUE"), the string "TRUE" is parsed as true.
A Float field named "XX", you set the value to be "3.5" by JsonObject.addProperty("XX", "3.5", the string "3.5" is parsed as 3.5.

build()

Constructs an InsertRowsParam object.

N/A

notes

In Java SDK versions v2.4.1 or earlier versions, the input is a fastjson.JSONObject. But fastjson is not recommended to use now because of its unsafe deserialization vulnerability. Therefore, replace fastjson with gson if you use the Java SDK of v2.4.2 or later releases.

The InsertRowsParam.Builder.build() can throw the following exceptions:

  • ParamException: error if the parameter is invalid.

Returns

This method catches all the exceptions and returns an R<InsertResponse> object.

  • If the API fails on the server side, it returns the error code and message from the server.

  • If the API fails by RPC exception, it returns R.Status.Unknown and the error message of the exception.

  • If the API succeeds, it returns a valid InsertResponse held by the R template.

Example

import io.milvus.param.*;
import io.milvus.response.MutationResultWrapper;
import io.milvus.grpc.MutationResult;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

List<JsonObject> rows = new ArrayList<>();
Random ran = new Random();
for (long i = 0L; i < rowCount; ++i) {
    JsonObject row = new JsonObject();
    row.addProperty(AGE_FIELD, ran.nextInt(99));
    List<Float> vector = generateFloatVector();
    row.add(VECTOR_FIELD, gson.toJsonTree(vector));

    // $meta if collection EnableDynamicField, you can input this field not exist in schema, else deny
    row.addProperty(INT32_FIELD_NAME, ran.nextInt());
    row.addProperty(INT64_FIELD_NAME, ran.nextLong());
    row.addProperty(VARCHAR_FIELD_NAME, "varchar");
    row.addProperty(FLOAT_FIELD_NAME, ran.nextFloat());
    row.addProperty(DOUBLE_FIELD_NAME, ran.nextDouble());
    row.addProperty(BOOL_FIELD_NAME, ran.nextBoolean());

    // $json
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty(INT32_FIELD_NAME, ran.nextInt());
    jsonObject.addProperty(INT64_FIELD_NAME, ran.nextLong());
    jsonObject.addProperty(VARCHAR_FIELD_NAME, "varchar");
    jsonObject.addProperty(FLOAT_FIELD_NAME, ran.nextFloat());
    jsonObject.addProperty(DOUBLE_FIELD_NAME, ran.nextDouble());
    jsonObject.addProperty(BOOL_FIELD_NAME, ran.nextBoolean());
    row.add(USER_JSON_FIELD, jsonObject);

    rows.add(row);
}

InsertRowsParam param = InsertRowsParam.newBuilder()
        .withCollectionName(COLLECTION_NAME)
        .withRows(rows)
        .build();
R<InsertResponse> response = client.insert(param);
if (response.getStatus() != R.Status.Success.getCode()) {
    System.out.println(response.getMessage());
}

System.out.println("insertCount: " + response.getData().getInsertCount());
System.out.println("insertIds: " + response.getData().getInsertIds());

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
Feedback

Was this page helpful?