1. 确认 docker 运行的 milvus容器镜像版本情况:
2. pom 依赖:
<dependency>
<groupId>io.milvus</groupId>
<artifactId>milvus-sdk-java</artifactId>
<version>2.1.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
</exclusions>
</dependency>
3. MilvusTest1.java:
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.ByteString;
import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.*;
import io.milvus.param.ConnectParam;
import io.milvus.param.MetricType;
import io.milvus.param.R;
import io.milvus.param.RpcStatus;
import io.milvus.param.collection.*;
import io.milvus.param.dml.DeleteParam;
import io.milvus.param.dml.InsertParam;
import io.milvus.param.dml.QueryParam;
import io.milvus.param.dml.SearchParam;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
public class MilvusTest1 {
private static final String MILVUS_IP = "127.0.0.1";
private static final int MILVUS_PORT = 19530;
/*
使用 docker-compose 方式启动的 milvus 容器默认未开启登录校验,即不填写用户名和密码即可连接成功,开启登录校验,需要修改 milvus 容器内部的一个配置文件,可使用如下方式修改:
1. docker cp xxxxxxxxxx:/milvus/configs/milvus.yaml ./ ( 其中 xxxxxxxxxx 是milvus容器的id )
2. 打开 milvus.yaml,将内容中的 common.security.authorizationEnabled的值改为 true( 默认是 false )
3. docker cp ./milvus.yaml xxxxxxxxxx:/milvus/configs
*/
private static final String USERNAME = "root";
private static final String PASSWORD = "Milvus";
private static final int VECTOR_DIMENSION = 256;
/**
* 创建 MilvusServiceClient
* @return
*/
private static MilvusServiceClient getMilvusServiceClient(){
ConnectParam connectParam = ConnectParam.newBuilder()
.withHost( MILVUS_IP )
.withPort( MILVUS_PORT )
.withAuthorization( USERNAME,PASSWORD )
.build();
connectParam.toString();
MilvusServiceClient milvusServiceClient = new MilvusServiceClient(connectParam);
return milvusServiceClient;
}
private static boolean existCollection(String collectionName){
MilvusServiceClient milvusClient = getMilvusServiceClient();
HasCollectionParam hasCollectionParam = HasCollectionParam.newBuilder()
.withCollectionName(collectionName)
.build();
R<Boolean> response = milvusClient.hasCollection( hasCollectionParam );
Integer status = response.getStatus();
Boolean data = response.getData();
System.out.println( collectionName + ":status = " + status + ",data = " + data );
return false;
}
//创建集合
private static void creatCollection(String collectionName){
MilvusServiceClient milvusClient = getMilvusServiceClient();
FieldType featureId = FieldType.newBuilder()
.withName("id")
.withDescription("主键id")
.withDataType(DataType.Int64)
.withPrimaryKey(true)
.withAutoID(false)
.build();
FieldType materielId = FieldType.newBuilder()
.withName("materiel_id")
.withDescription("物料id")
.withDataType(DataType.Int64)
.build();
FieldType colorPosition = FieldType.newBuilder()
.withName(PushMaterielsConfig.Field.COLOR_POSITION)
.withDescription("特征值")
.withDataType(DataType.FloatVector)
.withDimension(PushMaterielsConfig.FEATURE_DIM)
.build();
FieldType materielTypeId = FieldType.newBuilder()
.withName( "materiel_type_id" )
.withDescription("物料类型id")
.withDataType(DataType.Int64)
.build();
CreateCollectionParam createCollectionReq = CreateCollectionParam.newBuilder()
.withCollectionName(collectionName)
.withDescription("特征集合")
//.withShardsNum(PushMaterielsConfig.SHARDS_NUM)
.addFieldType(featureId)
.addFieldType(materielId)
.addFieldType(colorPosition)
.addFieldType(materielTypeId)
.build();
R<RpcStatus> response = milvusClient.createCollection(createCollectionReq);
System.out.println(PushMaterielsConfig.COLLECTION_NAME+"是否成功创建集合——>>"+response.getStatus());
}
//数据插入
public static void insertDataTo(String collectionName,int insertCount,List<String> imgNameList,List<String> matNameList ) {
List<List<Float>> imageVectors = new ArrayList<>();
for (int i = 0; i < insertCount; i++) {
List<Float> floatList = buildRandomFloatArrayList(VECTOR_DIMENSION);
imageVectors.add( floatList );
}
List<InsertParam.Field> fields = new ArrayList<>();
// fields.add(new InsertParam.Field( "title_vector", DataType.FloatVector, titleVectors ) );
// fields.add(new InsertParam.Field( "title", DataType.String, titles ) );
fields.add(new InsertParam.Field( "image_vector", imageVectors ) );
fields.add(new InsertParam.Field( "img_name", imgNameList ) );
fields.add(new InsertParam.Field( "mat_name", matNameList ) );
insert(collectionName,fields);
}
private static String buildRandomTitle() {
String title = UUID.randomUUID().toString().replaceAll("-", "");
System.out.println( title );
return title;
}
private static List<Float> buildRandomFloatArrayList(int size) {
if( size <= 0 ){
return null;
}
Random random = new Random();
List<Float> floatList = new ArrayList<>();
BigDecimal one_thousand = BigDecimal.valueOf(1000d);
for (int i = 0; i < size; i++) {
float v = one_thousand.multiply(BigDecimal.valueOf(random.nextDouble())).setScale(3, RoundingMode.HALF_UP).floatValue();
floatList.add( v );
}
System.out.println( floatList );
return floatList;
}
/**
* 插入数据
* @param collectionName
* @param fields
* @return
*/
public static void insert(String collectionName, List<InsertParam.Field> fields ){
MilvusServiceClient milvusClient = getMilvusServiceClient();
//插入
InsertParam insertParam = InsertParam.newBuilder()
.withCollectionName(collectionName)
//.withPartitionName(partitionName)
.withFields(fields)
.build();
R<MutationResult> insert = milvusClient.insert(insertParam);
Integer status = insert.getStatus();
Exception exception = insert.getException();
MutationResult data = insert.getData();
if( data == null ){
System.out.println( exception.getMessage() );
return;
}
List<Long> ids = data.getIDs().getIntId().getDataList();
System.out.println();
System.out.println();
System.out.println();
for( Long id:ids ){
System.out.println( id );
}
System.out.println();
System.out.println();
System.out.println();
System.out.println("插入 " + data.getInsertCnt() + " 行");
System.out.println( "status = " + status );
if( exception != null ){
System.out.println( "exception = " + exception.getMessage() );
System.out.println( "message = " + insert.getMessage() );
}
}
/**
* 加载集合到内存( 必须首先加载集合到内存,否则查询报错 "collection:tb_article or partition:[] not loaded into memory when search" )
* @param collectionName
*/
private static void loadCollectionByName( String collectionName ){
if( collectionName == null ){
return;
}
MilvusServiceClient milvusServiceClient = getMilvusServiceClient();
LoadCollectionParam loadCollectionParam = LoadCollectionParam.newBuilder()
.withCollectionName(collectionName)
.build();
R<RpcStatus> response = milvusServiceClient.loadCollection(loadCollectionParam);
handleRpcStatus( response );
}
private static void handleRpcStatus( R<RpcStatus> response ){
if( response == null ){
return;
}
int status = response.getStatus();
if( status == 0 ){
System.out.println( "操作成功" );
return;
}
Exception exception = response.getException();
System.err.println( "操作失败( status = " + status + " )");
if( exception == null ){
return;
}
System.err.println( response.getMessage() );
exception.printStackTrace();
}
/**
* 从内存中释放集合
* */
public static void releaseCollection(String collectionName) {
MilvusServiceClient milvusServiceClient = getMilvusServiceClient();
ReleaseCollectionParam releaseCollectionParam = ReleaseCollectionParam.newBuilder()
.withCollectionName(collectionName)
.build();
R<RpcStatus> response = milvusServiceClient.releaseCollection( releaseCollectionParam );
handleRpcStatus( response );
}
public static List<MaterialMilvusImageVO> search( String collectionName,List<Float> floatList ,int topK){
if( collectionName == null || floatList == null || floatList.size() != VECTOR_DIMENSION ){
return null;
}
MilvusServiceClient milvusServiceClient = getMilvusServiceClient();
List<String> outFields = new ArrayList<>();
outFields.add( "id" );
outFields.add( "img_name" );
outFields.add( "mat_name" );
List<List<Float>> vectors = new ArrayList<>();
vectors.add( floatList );
SearchParam searchParam = SearchParam.newBuilder()
.withCollectionName(collectionName)
.withTopK( topK )
.withMetricType( MetricType.L2 )
.withVectorFieldName("image_vector")
.withVectors( vectors )
.withOutFields( outFields )
.withExpr( "" )
// .withParams( "xxxx" )
.withParams( "{\n" +
"\"params\": \"{\\\"nprobe\\\":1,\\\"round_decimal\\\":-1}\",\n" +
"\"anns_field\": \"image_vector\",\n" +
"\"topk\": "+ topK +",\n" +
"\"metric_type\": \"L2\",\n" +
"\"round_decimal\": -1\n" +
"}" )
.build();
R<SearchResults> response = milvusServiceClient.search(searchParam);
int status = response.getStatus();
if( status != 0 ){
System.err.println( "search 失败( status = " + status + " )" );
Exception exception = response.getException();
if( exception != null ){
String message = response.getMessage();
System.out.println( "message = " + message );
exception.printStackTrace();
}
return null;
}
System.out.println( "search 成功" );
SearchResults data = response.getData();
if( data == null ){
return null;
}
SearchResultData results = data.getResults();
int scoresCount = results.getScoresCount();
List<MaterialMilvusImageVO> imageVOs = new ArrayList<>();
FieldData fieldsData_id = results.getFieldsData(0);
FieldData fieldsData_imgName = results.getFieldsData(1);
FieldData fieldsData_matName = results.getFieldsData(2);
LongArray ids = fieldsData_id.getScalars().getLongData();
StringArray imgNames = fieldsData_imgName.getScalars().getStringData();
StringArray matNames = fieldsData_matName.getScalars().getStringData();
for (int i = 0; i < scoresCount; i++) {
float scores = results.getScores(i);
long id = ids.getData(i);
String imgName = imgNames.getData(i);
String matName = matNames.getData(i);
MaterialMilvusImageVO imageVO = new MaterialMilvusImageVO();
imageVO.setScores( scores );
imageVO.setId( id );
imageVO.setImgName( imgName );
imageVO.setMatName( matName );
imageVOs.add( imageVO );
}
return imageVOs;
}
private static List<Float> jsonArray2FloatList(JSONArray jsonArray) {
if( jsonArray == null || jsonArray.size() == 0 ){
return null;
}
int size = jsonArray.size();
List<Float> floatList = new ArrayList<Float>();
for (int i = 0; i < size; i++) {
Float value = jsonArray.getFloat(i);
floatList.add( value );
}
return floatList;
}
public static void searchTest( String collectionName ){
// JSONArray array = JSONArray.parseArray("[720.665, 758.783, 831.477, 854.1, 597.977, 913.809, 659.829, 18.985, 852.756, 111.971, 596.902, 563.502, 234.051, 567.567, 140.383, 924.133, 829.13, 988.017, 691.144, 262.374, 970.751, 118.709, 774.637, 143.978, 613.916, 501.722, 200.533, 691.481, 991.008, 82.699, 591.095, 360.414, 417.625, 465.253, 537.802, 45.303, 966.211, 808.509, 756.147, 308.451, 350.507, 590.982, 0.121, 142.657, 771.119, 722.743, 170.707, 828.464, 660.933, 777.875, 6.887, 127.168, 238.876, 440.523, 982.933, 262.739, 531.455, 610.351, 552.415, 888.992, 53.779, 997.045, 677.183, 512.721, 655.2, 448.508, 658.687, 649.202, 129.465, 703.703, 649.351, 323.384, 65.253, 738.626, 863.322, 398.229, 70.156, 382.692, 428.757, 611.107, 939.694, 422.846, 27.923, 129.785, 562.639, 984.002, 956.735, 901.485, 354.926, 576.952, 1.945, 61.654, 796.625, 838.696, 582.076, 193.462, 209.974, 655.13, 927.256, 284.777, 261.139, 52.392, 683.495, 816.206, 794.394, 970.637, 218.536, 532.017, 514.615, 792.845, 79.253, 95.126, 722.457, 291.282, 504.682, 8.026, 776.332, 475.541, 634.123, 893.436, 206.254, 678.608, 66.354, 471.613, 436.861, 101.206, 723.824, 345.681]");
// JSONArray array = JSONArray.parseArray("[887.7130126953125,582.594970703125,362.6090087890625,322.5509948730469,400.1929931640625,934.3790283203125,55.89699935913086,595.0859985351562,763.9600219726562,70.4489974975586,388.57501220703125,72.06099700927734,654.4879760742188,914.051025390625,36.4739990234375,700.8900146484375,344.0140075683594,320.7019958496094,840.7139892578125,84.53900146484375,163.5030059814453,918.593017578125,696.1790161132812,951.6420288085938,562.447021484375,152.7689971923828,458.2380065917969,264.114990234375,80.23600006103516,680.0590209960938,631.1799926757812,578.4970092773438,512.5880126953125,583.0670166015625,837.1630249023438,489.1789855957031,307.4800109863281,224.04800415039062,581.0999755859375,724.8469848632812,757.2730102539062,943.2869873046875,624.72802734375,743.85302734375,867.1829833984375,221.64999389648438,362.2430114746094,479.9320068359375,874.9860229492188,373.239990234375,950.2050170898438,226.57200622558594,272.8059997558594,311.6669921875,586.031982421875,516.4990234375,839.0880126953125,442.1809997558594,252.25799560546875,252.18800354003906,627.3159790039062,272.0159912109375,447.82000732421875,273.9389953613281,77.75399780273438,902.5150146484375,912.9970092773438,13.991000175476074,419.49700927734375,169.21800231933594,121.22899627685547,878.3060302734375,312.3059997558594,829.801025390625,358.8900146484375,522.0679931640625,241.21099853515625,864.8870239257812,543.1820068359375,240.61099243164062,521.572021484375,652.947021484375,538.7000122070312,284.39300537109375,898.2739868164062,508.11700439453125,624.77099609375,24.81100082397461,629.948974609375,828.5139770507812,938.5770263671875,547.0609741210938,637.6119995117188,892.6719970703125,173.12100219726562,426.8949890136719,766.8049926757812,163.02699279785156,544.4879760742188,160.07699584960938,463.3340148925781,807.89697265625,59.422000885009766,956.4660034179688,194.8990020751953,37.20600128173828,412.1260070800781,385.0920104980469,764.4030151367188,762.2429809570312,418.2550048828125,662.2429809570312,255.45399475097656,36.01499938964844,754.4569702148438,211.50799560546875,726.5339965820312,708.322021484375,913.39697265625,492.8659973144531,927.5560302734375,738.4450073242188,707.06298828125,997.344970703125,250.9969940185547,648.3900146484375,355.2460021972656,734.427978515625]");
JSONArray array = JSONArray.parseArray("[448.4100036621094,979.3410034179688,66.54499816894531,993.3980102539062,38.8380012512207,599.7849731445312,960.1179809570312,301.9750061035156,50.95899963378906,581.3389892578125,130.75,238.89500427246094,122.23899841308594,750.6589965820312,149.5780029296875,357.802001953125,730.9910278320312,952.2589721679688,203.71099853515625,33.452999114990234,494.2099914550781,470.4779968261719,818.3170166015625,515.3560180664062,286.39599609375,376.0419921875,636.7769775390625,713.02001953125,130.31900024414062,100.96600341796875,729.8670043945312,868.6270141601562,46.03300094604492,757.4639892578125,655.9400024414062,625.4429931640625,561.06298828125,459.656005859375,26.284000396728516,384.10101318359375,382.50799560546875,181.34500122070312,643.302001953125,253.8990020751953,810.947021484375,488.72601318359375,373.9169921875,352.5870056152344,836.343017578125,823.864990234375,796.5440063476562,685.5980224609375,497.7799987792969,882.9019775390625,317.23199462890625,344.52801513671875,11.690999984741211,32.39500045776367,889.030029296875,304.6679992675781,377.3489990234375,637.8259887695312,688.3690185546875,672.6900024414062,653.7520141601562,640.2630004882812,103.22699737548828,838.1380004882812,868.6160278320312,710.3040161132812,170.0749969482422,616.1519775390625,568.051025390625,832.8369750976562,160.06900024414062,133.9010009765625,344.6090087890625,231.84300231933594,359.6809997558594,909.4829711914062,291.7200012207031,131.3560028076172,216.95399475097656,299.5899963378906,224.1490020751953,966.8699951171875,931.7670288085938,251.0330047607422,676.9630126953125,310.89801025390625,256.99798583984375,81.25199890136719,149.5780029296875,957.0490112304688,747.1790161132812,834.2260131835938,796.8380126953125,318.8999938964844,942.2269897460938,610.7440185546875,785.6409912109375,543.1079711914062,6.054999828338623,155.77699279785156,833.5260009765625,510.6300048828125,153.3489990234375,900.7420043945312,24.680999755859375,205.56100463867188,792.510009765625,604.7249755859375,859.239990234375,768.0289916992188,688.7659912109375,716.280029296875,633.2969970703125,915.7589721679688,608.6400146484375,586.2000122070312,669.927978515625,318.3080139160156,845.4879760742188,701.7139892578125,625.6320190429688,938.6530151367188,745.5040283203125,183.82000732421875,81.92500305175781,318.7340087890625,854.593994140625,892.0390014648438,898.4349975585938,278.6889953613281,588.7340087890625,614.6380004882812,351.7170104980469,523.9829711914062,393.9070129394531,987.4310302734375,913.0609741210938,210.2310028076172,206.43800354003906,980.5269775390625,628.9710083007812,602.4910278320312,650.531982421875,19.959999084472656,459.56201171875,583.9320068359375,817.0360107421875,69.92400360107422,602.9769897460938,918.1920166015625,655.9199829101562,291.3420104980469,788.0349731445312,850.4459838867188,984.7119750976562,213.83700561523438,752.676025390625,844.0549926757812,866.0999755859375,710.6680297851562,642.3569946289062,992.8939819335938,640.1060180664062,450.0119934082031,860.4130249023438,923.8280029296875,209.76400756835938,910.9949951171875,981.5,809.4849853515625,273.9289855957031,499.177001953125,699.5859985351562,448.39599609375,76.81999969482422,773.718017578125,423.5539855957031,237.71099853515625,901.1439819335938,307.2650146484375,56.3380012512207,263.1549987792969,970.1110229492188,239.61599731445312,697.6199951171875,661.5020141601562,643.375,157.5189971923828,495.44500732421875,872.8079833984375,792.927978515625,535.4979858398438,638.7429809570312,392.7669982910156,27.187000274658203,606.3930053710938,530.572021484375,159.99600219726562,994.9829711914062,195.33200073242188,511.02099609375,768.7119750976562,345.9179992675781,747.1480102539062,109.03600311279297,284.1619873046875,49.49800109863281,504.7860107421875,694.0650024414062,556.3579711914062,682.6950073242188,555.052001953125,571.2420043945312,577.06201171875,984.0700073242188,248.06399536132812,17.948999404907227,605.6409912109375,442.302001953125,318.0899963378906,494.9429931640625,501.32000732421875,937.9580078125,885.9660034179688,281.8219909667969,942.5269775390625,489.5570068359375,407.093994140625,84.48899841308594,988.4450073242188,968.4609985351562,544.06201171875,741.3280029296875,743.530029296875,241.8509979248047,889.1909790039062,15.206999778747559,722.7039794921875,579.0289916992188,439.8940124511719,599.989013671875,883.0449829101562,313.5,738.4819946289062,893.0850219726562,187.26699829101562,691.7789916992188,628.1099853515625,789.7230224609375,994.9010009765625,559.5830078125,647.8560180664062]");
List<Float> floatList = jsonArray2FloatList( array );
List<MaterialMilvusImageVO> imageVOS = search(collectionName, floatList, 10);
if( imageVOS == null ){
return;
}
for ( MaterialMilvusImageVO imageVO:imageVOS ){
Long id = imageVO.getId();
Float scores = imageVO.getScores();
String imgName = imageVO.getImgName();
String matName = imageVO.getMatName();
System.out.println( id + " \t "+ scores +" \t "+ imgName +" \t " + matName );
}
}
public static void insertTest( String collectionName ){
int insertCount = 3;
List<String> imgNameList = new ArrayList<>();
imgNameList.add( "西瓜01" );
imgNameList.add( "西瓜02" );
imgNameList.add( "西瓜03" );
List<String> matNameList = new ArrayList<>();
matNameList.add( "西瓜" );
matNameList.add( "西瓜" );
matNameList.add( "西瓜" );
insertDataTo( collectionName,insertCount,imgNameList,matNameList );
}
private static void deleteTest( String collectionName,List<Long> ids ){
if( ids == null || ids.size() == 0 ){
return;
}
/*String expr = "{" +
"\"expr\": \"id in " + JSONObject.toJSONString( ids ) + "\"" +
"}";*/
String expr = "id in " + JSONObject.toJSONString( ids );
System.out.println();
System.out.println( expr );
System.out.println();
DeleteParam deleteParam = DeleteParam.newBuilder()
.withCollectionName(collectionName)
.withExpr( expr )
.build();
R<MutationResult> response = getMilvusServiceClient().delete(deleteParam);
if( response == null ){
System.err.println( "从 milvus 中删除记录失败( ids = " + JSONObject.toJSONString( ids ) + " ) cause response is empty" );
return;
}
Integer status = response.getStatus();
if( status == 0 ){
System.out.println( "删除成功" );
return;
}
Exception e = response.getException();
if( e != null ){
System.err.println( "从 milbus 中删除记录失败( ids = " + JSONObject.toJSONString( ids ) + " ) cause " + e.getMessage() );
}
}
public static void main(String[] args) throws InterruptedException {
// existCollection( "tb_article" );
// existCollection( "tb_material_image" );
// existCollection( "tb_material_image" );
// existCollection( "xxx" );
// creatCollection();
// existCollection( "tb_article" );
// creatCollection( "tb_materiel_xxx2" );
// loadCollectionByName( collectionName );
// releaseCollection( collectionName );
// searchTest( "tb_material_image" );
// id = 450704941225082936
// 450704941225082937
// 450704941225082938
// insertTest( "tb_material_image" );
queryTest( "tb_material_image" );
}
private static void queryTest(String collectionName) {
QueryParam queryParam = QueryParam.newBuilder().withCollectionName(collectionName)
.addOutField("id")
.addOutField("image_vector")
.addOutField("mat_name")
.addOutField("img_name")
.withExpr("id >= 111")
.build();
R<QueryResults> response = getMilvusServiceClient().query(queryParam);
Integer status = response.getStatus();
if( status != 0 ){
System.err.println( "查询失败,cause " + response.getException().getMessage() );
return;
}
System.out.println( "查询成功!" );
QueryResults queryResults = response.getData();
List<Long> ids = queryResults.getFieldsData(0).getScalars().getLongData().getDataList();
List<Float> imageVectors = queryResults.getFieldsData(1).getVectors().getFloatVector().getDataList();
List<String> matNames = queryResults.getFieldsData(2).getScalars().getStringData().getDataList();
List<String> imgNames = queryResults.getFieldsData(3).getScalars().getStringData().getDataList();
int size = ids.size();
int dimension = 256;
int index_start = 0;
for (int i = 0; i < size; i++) {
Long id = ids.get(i);
int index_end = index_start + dimension;
List<Float> imageVector = imageVectors.subList(index_start, index_end);
String matName = matNames.get(i);
String imgName = imgNames.get(i);
System.out.println( i + ":" +id + " \t "+ matName +" \t " + imgName + " \t " + imageVector.size() + ":" + imageVector );
index_start += dimension;
}
}
}