From 4d0f0911e9fb90bb32e5e676c29cd94ba43d4197 Mon Sep 17 00:00:00 2001 From: xgc Date: Sun, 12 May 2024 02:40:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=AF=E7=94=A8=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=95=B0=E6=8D=AE=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../milvus/demo/ApplicationRunnerTest.java | 58 +++++++++---------- .../javpower/milvus/demo/model/Face.java | 7 +-- .../src/main/resources/application.yml | 8 ++- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/milvus-demo/src/main/java/io/github/javpower/milvus/demo/ApplicationRunnerTest.java b/milvus-demo/src/main/java/io/github/javpower/milvus/demo/ApplicationRunnerTest.java index 357679f..25dfa9a 100644 --- a/milvus-demo/src/main/java/io/github/javpower/milvus/demo/ApplicationRunnerTest.java +++ b/milvus-demo/src/main/java/io/github/javpower/milvus/demo/ApplicationRunnerTest.java @@ -1,56 +1,56 @@ package io.github.javpower.milvus.demo; -import com.google.common.collect.Lists; +import com.alibaba.fastjson.JSONObject; import io.github.javpower.milvus.demo.model.Face; import io.github.javpower.milvus.demo.test.FaceMilvusMapper; import io.github.javpower.milvus.plus.model.vo.MilvusResp; -import io.github.javpower.milvus.plus.model.vo.MilvusResultVo; import io.milvus.v2.service.vector.response.DeleteResp; import io.milvus.v2.service.vector.response.InsertResp; import io.milvus.v2.service.vector.response.UpsertResp; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; +import java.util.ArrayList; import java.util.List; @Component +@Slf4j public class ApplicationRunnerTest implements ApplicationRunner { @Autowired private FaceMilvusMapper mapper; @Override public void run(ApplicationArguments args) throws Exception { - List vector = Lists.newArrayList(0.1f,0.2f,0.3f); + Face face=new Face(); + List vector = new ArrayList<>(); + // 假设我们有一些浮点数值来填充向量,实际应用中这里应该是人脸特征提取算法得到的数值 + for (int i = 0; i < 128; i++) { + vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数 + } + face.setFaceVector(vector); + face.setPersonId(1l); + //新增 + MilvusResp insert = mapper.insert(face); + log.info("insert--{}", JSONObject.toJSONString(insert)); + //查询 + MilvusResp> query = mapper.getById(1l); + log.info("query--{}", JSONObject.toJSONString(query)); + //更新 + vector.clear(); + for (int i = 0; i < 128; i++) { + vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数 + } + MilvusResp update = mapper.updateById(face); + log.info("update--{}", JSONObject.toJSONString(update)); + //删除 + MilvusResp remove = mapper.removeById(1l); + log.info("remove--{}", JSONObject.toJSONString(remove)); //查询 - MilvusResp> query = mapper.searchWrapper() - .eq(Face::getPersonId, 1l) - .vector(Face::getFaceVector,vector) - .limit(100l) - .query(); MilvusResp> query2 = mapper.getById(1l); + log.info("query--{}", JSONObject.toJSONString(query2)); - //删除 - MilvusResp remove= mapper.deleteWrapper() - .eq(Face::getPersonId, 1l) - .id(111) - .remove(); - MilvusResp remove2 = mapper.removeById(1l); - - //更新 - Face face=new Face(); - face.setFaceVector(vector); - MilvusResp update = mapper.updateWrapper() - .eq(Face::getPersonId, 1l) - .update(face); - face.setPersonId(1l); - MilvusResp update2 = mapper.updateById(face); - - //新增 - MilvusResp insert = mapper.insertWrapper() - .put(Face::getFaceVector, vector) - .insert(); - MilvusResp insert2 = mapper.insert(face); } } \ No newline at end of file diff --git a/milvus-demo/src/main/java/io/github/javpower/milvus/demo/model/Face.java b/milvus-demo/src/main/java/io/github/javpower/milvus/demo/model/Face.java index 43382c2..6e3ec22 100644 --- a/milvus-demo/src/main/java/io/github/javpower/milvus/demo/model/Face.java +++ b/milvus-demo/src/main/java/io/github/javpower/milvus/demo/model/Face.java @@ -11,21 +11,20 @@ import lombok.Data; import java.util.List; @Data -@MilvusCollection(name = "face_collection") // 指定Milvus集合的名称 +@MilvusCollection(name = "face_collection_3") // 指定Milvus集合的名称 public class Face { @MilvusField( name = "person_id", // 字段名称 dataType = DataType.Int64, // 数据类型为64位整数 isPrimaryKey = true, // 标记为主键 - autoID = true // 假设这个ID是自动生成的 + autoID = false // 假设这个ID是自动生成的 ) private Long personId; // 人员的唯一标识符 @MilvusField( name = "face_vector", // 字段名称 dataType = DataType.FloatVector, // 数据类型为浮点型向量 - dimension = 128, // 向量维度,假设人脸特征向量的维度是128 - isPartitionKey = false // 假设这个字段不是分区键 + dimension = 128 // 向量维度,假设人脸特征向量的维度是128 ) @MilvusIndex( indexType = IndexParam.IndexType.IVF_FLAT, // 使用IVF_FLAT索引类型 diff --git a/milvus-demo/src/main/resources/application.yml b/milvus-demo/src/main/resources/application.yml index 7d6ef9d..1be2f8e 100644 --- a/milvus-demo/src/main/resources/application.yml +++ b/milvus-demo/src/main/resources/application.yml @@ -2,6 +2,8 @@ server: port: 8131 milvus: - uri: localhost:8999 - token: sss - enable: false \ No newline at end of file + uri: http://xxxxx:19530 + token: root:xxxx + enable: true + packages: + - io.github.javpower.milvus.demo.model \ No newline at end of file