-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update java api parameters with python to keep in sync
Signed-off-by: Nian Liu <[email protected]>
- Loading branch information
Showing
39 changed files
with
726 additions
and
541 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/main/java/io/milvus/v2/exception/MilvusClientException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.