Skip to content

Commit

Permalink
Update java api parameters with python to keep in sync
Browse files Browse the repository at this point in the history
Signed-off-by: Nian Liu <[email protected]>
  • Loading branch information
nianliuu committed Jan 30, 2024
1 parent b9e4c05 commit f8e1e7a
Show file tree
Hide file tree
Showing 39 changed files with 726 additions and 541 deletions.
4 changes: 1 addition & 3 deletions src/main/java/io/milvus/v2/client/ConnectConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import lombok.experimental.SuperBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

@Data
Expand All @@ -16,7 +14,7 @@ public class ConnectConfig {
private String token;
private String username;
private String password;
private String databaseName;
private String dbName;
@Builder.Default
private long connectTimeoutMs = 10000;
@Builder.Default
Expand Down
198 changes: 92 additions & 106 deletions src/main/java/io/milvus/v2/client/MilvusClientV2.java

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions src/main/java/io/milvus/v2/examples/Simple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.milvus.v2.examples;

import com.alibaba.fastjson.JSONObject;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.exception.MilvusClientException;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.DescribeCollectionReq;
import io.milvus.v2.service.collection.request.DropCollectionReq;
import io.milvus.v2.service.collection.request.HasCollectionReq;
import io.milvus.v2.service.vector.request.InsertReq;
import io.milvus.v2.service.vector.request.QueryReq;
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.response.QueryResp;
import io.milvus.v2.service.vector.response.SearchResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Simple {
Integer dim = 2;
String collectionName = "book";
static Logger logger = LoggerFactory.getLogger(Simple.class);
public static void main(String[] args) {
try {
new Simple().run();
} catch (MilvusClientException | InterruptedException e) {
logger.info(e.toString());
}
}

public void run() throws InterruptedException {
ConnectConfig connectConfig = ConnectConfig.builder()
.uri("https://in01-******.aws-us-west-2.vectordb.zillizcloud.com:19531")
.token("*****")
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// check collection exists
if (client.hasCollection(HasCollectionReq.builder().collectionName(collectionName).build())) {
logger.info("collection exists");
client.dropCollection(DropCollectionReq.builder().collectionName(collectionName).build());
TimeUnit.SECONDS.sleep(1);
}
// create collection
CreateCollectionReq createCollectionReq = CreateCollectionReq.builder()
.collectionName(collectionName)
.dimension(dim)
.build();
client.createCollection(createCollectionReq);

logger.info(String.valueOf(client.listCollections()));
logger.info(String.valueOf(client.describeCollection(DescribeCollectionReq.builder().collectionName(collectionName).build())));
//insert data
List<JSONObject> insertData = new ArrayList<>();
for(int i = 0; i < 6; i++){
JSONObject jsonObject = new JSONObject();
List<Float> vectorList = new ArrayList<>();
for(int j = 0; j < dim; j++){
// generate random float vector
vectorList.add(new Random().nextFloat());
}
jsonObject.put("id", (long) i);
jsonObject.put("vector", vectorList);
insertData.add(jsonObject);
}
InsertReq insertReq = InsertReq.builder()
.collectionName(collectionName)
.data(insertData)
.build();
client.insert(insertReq);
//query data
QueryReq queryReq = QueryReq.builder()
.collectionName(collectionName)
.filter("id in [0]")
.build();
QueryResp queryResp = client.query(queryReq);
System.out.println(queryResp);
//search data
SearchReq searchReq = SearchReq.builder()
.collectionName(collectionName)
.data(Collections.singletonList(insertData.get(0).get("vector")))
.topK(10)
.build();
SearchResp searchResp = client.search(searchReq);
System.out.println(searchResp);
}
}
108 changes: 108 additions & 0 deletions src/main/java/io/milvus/v2/examples/Simple_Schema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package io.milvus.v2.examples;

import com.alibaba.fastjson.JSONObject;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.DataType;
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.collection.request.CreateCollectionWithSchemaReq;
import io.milvus.v2.service.collection.request.DropCollectionReq;
import io.milvus.v2.service.collection.request.HasCollectionReq;
import io.milvus.v2.service.collection.request.LoadCollectionReq;
import io.milvus.v2.service.index.request.CreateIndexReq;
import io.milvus.v2.service.vector.request.InsertReq;
import io.milvus.v2.service.vector.request.QueryReq;
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.response.QueryResp;
import io.milvus.v2.service.vector.response.SearchResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Simple_Schema {
Integer dim = 2;
String collectionName = "book";
static Logger logger = LoggerFactory.getLogger(Simple_Schema.class);
public void run() throws InterruptedException {
ConnectConfig connectConfig = ConnectConfig.builder()
.uri("https://in01-****.aws-us-west-2.vectordb.zillizcloud.com:19531")
.token("******")
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// check collection exists
if (client.hasCollection(HasCollectionReq.builder().collectionName(collectionName).build())) {
logger.info("collection exists");
client.dropCollection(DropCollectionReq.builder().collectionName(collectionName).build());
TimeUnit.SECONDS.sleep(1);
}
// create collection
CreateCollectionWithSchemaReq.CollectionSchema collectionSchema = client.createSchema(Boolean.TRUE, "");
collectionSchema.addPrimaryField("id", DataType.Int64, Boolean.TRUE, Boolean.FALSE);
collectionSchema.addVectorField("vector", DataType.FloatVector, dim);
collectionSchema.addScalarField("num", DataType.Int32);

CreateCollectionWithSchemaReq createCollectionReq = CreateCollectionWithSchemaReq.builder()
.collectionName(collectionName)
.collectionSchema(collectionSchema)
.build();
client.createCollectionWithSchema(createCollectionReq);
//create index
IndexParam indexParam = IndexParam.builder()
.fieldName("vector")
.metricType(IndexParam.MetricType.COSINE)
.build();
CreateIndexReq createIndexReq = CreateIndexReq.builder()
.collectionName(collectionName)
.indexParams(Collections.singletonList(indexParam))
.build();
client.createIndex(createIndexReq);
client.loadCollection(LoadCollectionReq.builder().collectionName(collectionName).build());
//insert data
List<JSONObject> insertData = new ArrayList<>();
for(int i = 0; i < 6; i++){
JSONObject jsonObject = new JSONObject();
List<Float> vectorList = new ArrayList<>();
for(int j = 0; j < dim; j++){
// generate random float vector
vectorList.add(new Random().nextFloat());
}
jsonObject.put("id", (long) i);
jsonObject.put("vector", vectorList);
jsonObject.put("num", i);
insertData.add(jsonObject);
}

InsertReq insertReq = InsertReq.builder()
.collectionName(collectionName)
.data(insertData)
.build();
client.insert(insertReq);
//query data
QueryReq queryReq = QueryReq.builder()
.collectionName(collectionName)
.filter("id in [0]")
.build();
QueryResp queryResp = client.query(queryReq);
System.out.println(queryResp);
//search data
SearchReq searchReq = SearchReq.builder()
.collectionName(collectionName)
.data(Collections.singletonList(insertData.get(0).get("vector")))
.topK(10)
.build();
SearchResp searchResp = client.search(searchReq);
System.out.println(searchResp);
}
public static void main(String[] args) {
try {
new Simple_Schema().run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/milvus/v2/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.milvus.v2.exception;

import lombok.Getter;

@Getter
public enum ErrorCode {
SUCCESS(0),
COLLECTION_NOT_FOUND(1),
SERVER_ERROR(2);

private final int code;

ErrorCode(int i) {
this.code = i;
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/milvus/v2/exception/MilvusClientException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.milvus.v2.exception;

import lombok.Getter;

@Getter
public class MilvusClientException extends RuntimeException {

private final ErrorCode errorCode;

public MilvusClientException(ErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}


}
12 changes: 8 additions & 4 deletions src/main/java/io/milvus/v2/service/BaseService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package io.milvus.v2.service;

import io.milvus.exception.MilvusException;
import io.milvus.grpc.BoolResponse;
import io.milvus.grpc.HasCollectionRequest;
import io.milvus.grpc.MilvusServiceGrpc;
import io.milvus.param.R;
import io.milvus.v2.exception.ErrorCode;
import io.milvus.v2.exception.MilvusClientException;
import io.milvus.v2.utils.ConvertUtils;
import io.milvus.v2.utils.DataUtils;
import io.milvus.v2.utils.VectorUtils;
import io.milvus.v2.utils.RpcUtils;
import io.milvus.v2.utils.VectorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseService {
protected static final Logger logger = LoggerFactory.getLogger(BaseService.class);
public RpcUtils rpcUtils = new RpcUtils();
public DataUtils dataUtils = new DataUtils();
public VectorUtils vectorUtils = new VectorUtils();
Expand All @@ -21,7 +24,8 @@ protected void checkCollectionExist(MilvusServiceGrpc.MilvusServiceBlockingStub
BoolResponse result = blockingStub.hasCollection(request);
rpcUtils.handleResponse("", result.getStatus());
if (!result.getValue() == Boolean.TRUE) {
throw new MilvusException("Collection " + collectionName + " not exist", R.Status.CollectionNotExists.getCode());
logger.error("Collection not found");
throw new MilvusClientException(ErrorCode.COLLECTION_NOT_FOUND, "Collection not found");
}
}
}
Loading

0 comments on commit f8e1e7a

Please sign in to comment.