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. | rows: A list of gson.JsonObject objects, each representing a row data. |
build() | Constructs an InsertRowsParam object. | N/A |
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 theR
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());