Skip to content

Commit 30774ff

Browse files
authored
AddField example (#1540)
Signed-off-by: yhmo <yihua.mo@zilliz.com>
1 parent b80dcfd commit 30774ff

9 files changed

Lines changed: 305 additions & 0 deletions

examples/src/main/java/io/milvus/v1/ArrayFieldExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package io.milvus.v1;
221

322
import com.google.gson.Gson;

examples/src/main/java/io/milvus/v1/NullAndDefaultExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package io.milvus.v1;
221

322
import com.google.gson.Gson;
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package io.milvus.v2;
21+
22+
import com.google.gson.Gson;
23+
import com.google.gson.JsonObject;
24+
import io.milvus.v1.CommonUtils;
25+
import io.milvus.v2.client.ConnectConfig;
26+
import io.milvus.v2.client.MilvusClientV2;
27+
import io.milvus.v2.common.ConsistencyLevel;
28+
import io.milvus.v2.common.DataType;
29+
import io.milvus.v2.common.IndexParam;
30+
import io.milvus.v2.service.collection.request.AddCollectionFieldReq;
31+
import io.milvus.v2.service.collection.request.AddFieldReq;
32+
import io.milvus.v2.service.collection.request.CreateCollectionReq;
33+
import io.milvus.v2.service.collection.request.DropCollectionReq;
34+
import io.milvus.v2.service.vector.request.InsertReq;
35+
import io.milvus.v2.service.vector.request.QueryReq;
36+
import io.milvus.v2.service.vector.response.QueryResp;
37+
38+
import java.util.*;
39+
40+
public class AddFieldExample {
41+
private static final MilvusClientV2 client;
42+
43+
static {
44+
ConnectConfig config = ConnectConfig.builder()
45+
.uri("http://localhost:19530")
46+
.build();
47+
client = new MilvusClientV2(config);
48+
}
49+
private static final String COLLECTION_NAME = "java_sdk_example_add_field_v2";
50+
private static final String ID_FIELD = "id";
51+
private static final String VECTOR_FIELD = "vector";
52+
private static final Integer VECTOR_DIM = 8;
53+
54+
private static void createCollection() {
55+
// Drop collection if exists
56+
client.dropCollection(DropCollectionReq.builder()
57+
.collectionName(COLLECTION_NAME)
58+
.build());
59+
60+
// Create collection
61+
CreateCollectionReq.CollectionSchema collectionSchema = CreateCollectionReq.CollectionSchema.builder()
62+
.build();
63+
collectionSchema.addField(AddFieldReq.builder()
64+
.fieldName(ID_FIELD)
65+
.dataType(DataType.Int64)
66+
.isPrimaryKey(true)
67+
.autoID(false)
68+
.build());
69+
collectionSchema.addField(AddFieldReq.builder()
70+
.fieldName(VECTOR_FIELD)
71+
.dataType(DataType.FloatVector)
72+
.dimension(VECTOR_DIM)
73+
.build());
74+
75+
List<IndexParam> indexes = new ArrayList<>();
76+
indexes.add(IndexParam.builder()
77+
.fieldName(VECTOR_FIELD)
78+
.indexType(IndexParam.IndexType.FLAT)
79+
.metricType(IndexParam.MetricType.COSINE)
80+
.build());
81+
82+
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
83+
.collectionName(COLLECTION_NAME)
84+
.collectionSchema(collectionSchema)
85+
.indexParams(indexes)
86+
.consistencyLevel(ConsistencyLevel.BOUNDED)
87+
.build();
88+
client.createCollection(requestCreate);
89+
System.out.println("Collection created");
90+
}
91+
92+
private static void query(long id) {
93+
QueryResp queryRet = client.query(QueryReq.builder()
94+
.collectionName(COLLECTION_NAME)
95+
.ids(Collections.singletonList(id))
96+
.outputFields(Collections.singletonList("*"))
97+
.consistencyLevel(ConsistencyLevel.STRONG)
98+
.build());
99+
System.out.println("\nQuery with id: " + id);
100+
List<QueryResp.QueryResult> records = queryRet.getQueryResults();
101+
for (QueryResp.QueryResult record : records) {
102+
System.out.println(record.getEntity());
103+
}
104+
System.out.println("=============================================================");
105+
}
106+
107+
public static void main(String[] args) {
108+
createCollection();
109+
110+
Gson gson = new Gson();
111+
{
112+
// Insert one row
113+
JsonObject row = new JsonObject();
114+
row.addProperty(ID_FIELD, 100L);
115+
row.add(VECTOR_FIELD, gson.toJsonTree(CommonUtils.generateFloatVector(VECTOR_DIM)));
116+
client.insert(InsertReq.builder()
117+
.collectionName(COLLECTION_NAME)
118+
.data(Collections.singletonList(row))
119+
.build());
120+
}
121+
122+
{
123+
// Add a new field, the new field must be nullable, for the previous row, the "text" value is null
124+
client.addCollectionField(AddCollectionFieldReq.builder()
125+
.collectionName(COLLECTION_NAME)
126+
.fieldName("text")
127+
.dataType(DataType.VarChar)
128+
.maxLength(100)
129+
.isNullable(true)
130+
.build());
131+
132+
// Query the previous row
133+
query(100L);
134+
}
135+
136+
{
137+
// Add a new row
138+
JsonObject row = new JsonObject();
139+
row.addProperty(ID_FIELD, 500L);
140+
row.addProperty("text", "this is a new row");
141+
row.add(VECTOR_FIELD, gson.toJsonTree(CommonUtils.generateFloatVector(VECTOR_DIM)));
142+
client.insert(InsertReq.builder()
143+
.collectionName(COLLECTION_NAME)
144+
.data(Collections.singletonList(row))
145+
.build());
146+
147+
// Query the new row
148+
query(500L);
149+
}
150+
151+
client.close();
152+
}
153+
}

examples/src/main/java/io/milvus/v2/ArrayFieldExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package io.milvus.v2;
221

322
import com.google.gson.Gson;

examples/src/main/java/io/milvus/v2/FullTextSearchExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package io.milvus.v2;
221

322
import com.google.gson.Gson;

examples/src/main/java/io/milvus/v2/Int8VectorExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package io.milvus.v2;
221

322
import com.google.gson.Gson;

examples/src/main/java/io/milvus/v2/NullAndDefaultExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package io.milvus.v2;
221

322
import com.google.gson.Gson;

examples/src/main/java/io/milvus/v2/TextMatchExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package io.milvus.v2;
221

322
import com.google.gson.Gson;

examples/src/main/java/io/milvus/v2/bulkwriter/CsvDataObject.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package io.milvus.v2.bulkwriter;
221

322
import com.fasterxml.jackson.annotation.JsonProperty;

0 commit comments

Comments
 (0)