java 操作 milvus 2.1.4

news2024/11/16 2:35:08

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;
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1868588.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ubuntu篇---添加环境变量并且在pycharm中使用

ubuntu篇—添加环境变量并且在pycharm中使用 一. 添加环境变量 vim ~/.bashrc 在文件末尾加上 保存退出 source ~/.bashrc二. 在pycharm中添加环境变量 1.打开pycharm&#xff0c;并打开你的项目 2.点击菜单栏中的“Run”&#xff0c; 选择“Edit Configurations” 3.在弹…

一天跌20%,多只可转债“腰斩”,近百只跌破面值,“退可守”的香饽饽为何破防?

专业人士指出&#xff0c;近期部分可转债大跌原因主要有两点&#xff1a;一方面&#xff0c;转债市场与权益市场联动性强。另一方面&#xff0c;近期公布的宏观经济数据稳中趋缓&#xff0c;“供强需弱”特征依然明显&#xff0c;证监会主席吴清发言及“科创板八条”新规延续了…

Spring Cloud - nacos +ubuntu环境搭建

1、安装ubuntu虚拟环境 VMware虚拟机安装Ubuntu与配置Ubuntu&#xff08;超详细教程&#xff09; 2、docker环境安装 1、apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common 2、curl -fsSL https://mirrors.ustc.edu.cn/docke…

百问网全志D1h开发板HDMI、IR、WIFI功能适配

HDMI、IR、WIFI 适配 DTS 文件 ./device/config/chips/d1-h/configs/nezha/uboot-board.dts./device/config/chips/d1-h/configs/nezha/linux-5.4/board.dts 分区文件 ./target/allwinner/d1-h-nezha/swupdate/sys_partition_ab.fex 默认 HDMI 输出 这里我希望开机就是默认…

ESXI 7.0 之间互传文件

前言、思路分析 ESXI 7.0 之间互传文件 1.vcenter 使用 vmotion 热迁移 2.不用vcerter, 直接使用Esxi cli 使用linux SCP 命令进行ESXI 主机之间数据传输。 未加vcenter ,不使用vcentre ------使用ssh , 使用scp---开启ssh 服务---- 源开启ssh 客-户端----组织scp 命令--…

昇思25天学习打卡营第9天 | 静态图加速

内容介绍&#xff1a; AI编译框架分为两种运行模式&#xff0c;分别是动态图模式以及静态图模式。MindSpore默认情况下是以动态图模式运行&#xff0c;但也支持手工切换为静态图模式。两种运行模式的详细介绍如下&#xff1a; 动态图模式&#xff1a; 动态图的特点是计算图的…

移远通信发布两款Wi-Fi 6模组新品:率先采用亚马逊ACK SDK for Matter方案实现互联互通

6月26日 &#xff0c;在MWC上海展上&#xff0c;全球领先的物联网整体解决方案供应商移远通信联合亚马逊及上海博通现场宣布&#xff0c;推出支持亚马逊Alexa Connect Kit &#xff08;ACK&#xff09;SDK for Matter方案的MCU Wi-Fi 6模组FLM163D和FLM263D。 后续&#xff0c;…

论文降重困难重重?AI降重来帮忙

论文查重和降重是确保学术成果原创性及学术诚信的关键步骤&#xff0c;直接影响我们的学业成果和毕业资格。传统的论文查重方法主要包括使用查重软件和个人自查&#xff0c;而论文降重通常涉及改写、使用同义词替换、内容的扩展和深化&#xff0c;以及正确的引用和注释等方式来…

硬件实用技巧:电容精度和常用容值表

若该文为原创文章&#xff0c;转载请注明原文出处 本文章博客地址&#xff1a;https://hpzwl.blog.csdn.net/article/details/140009042 长沙红胖子Qt&#xff08;长沙创微智科&#xff09;博文大全&#xff1a;开发技术集合&#xff08;包含Qt实用技术、树莓派、三维、OpenCV…

Sentinel链路流控模式失效的解决方法

解决方法 1、在pom.xml中增加sentinel-web-servlet的依赖&#xff0c;我使用的版本是1.7.1 <dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-web-servlet</artifactId> </dependency>2、在项目中添加一个FilterCon…

汇川H5u小型PLC作modbusRTU从站设置及测试

目录 新建工程COM通讯参数配置协议选择协议配置 查看手册Modbus地址对应关系仿真测试 新建工程 新建一个H5U工程&#xff0c;不使用临时工程 系列选择H5U即可 COM通讯参数配置 协议选择 选择ModbusRTU从站 协议配置 端口号默认不可选择 波特率这里使用9600 数据长度&…

Studying-代码随想录训练营day21| 669.修建二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树、二叉树总结

第21天&#xff0c;二叉树最后一篇&#xff0c;冲&#x1f4aa; 目录 669.修建二叉搜索树 108.将有序数组转换为二叉搜索树 538.把二叉搜索树转换为累加树 二叉树总结 669.修建二叉搜索树 文档讲解&#xff1a;代码随想录修建二叉搜索树 视频讲解&#xff1a;手撕修建二叉…

力扣爆刷第155天之TOP100五连刷41-45(下一个排列、正序数组中位数、归并排序链表)

力扣爆刷第155天之TOP100五连刷41-45&#xff08;下一个排列、正序数组中位数、归并排序链表&#xff09; 文章目录 力扣爆刷第155天之TOP100五连刷41-45&#xff08;下一个排列、正序数组中位数、归并排序链表&#xff09;一、31. 下一个排列二、4. 寻找两个正序数组的中位数三…

Python基于逻辑回归分类模型、决策树分类模型、LightGBM分类模型和XGBoost分类模型实现车辆贷款违约预测项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 随着经济的发展和人民生活水平的提高&#xff0c;汽车消费在居民消费中所占比例逐渐增加&#xff0c;汽…

【Linux】CentOS7重置登录密码

一、具体步骤 1、启动虚拟机&#xff0c;进入下方页面时选择第一个选项&#xff0c;并按e 2、在下方界面 添加 init/bin/sh,刚开始进来可能看不到&#xff0c;需要按下键盘的↓键。 3、填写完成后ctrlx保存 4、输入mount -o remount, rw / 5、输入passwd 用户名;输入后出现…

澳汰尔(Altair® )FlowSimulator™ 集成热流体系统设计

FlowSimulator 是一款 3D 设计工具&#xff0c;可为 CAD 集成环境中的流体和热系统提供跨学科建模和优化。 系统级设计变得越来越重要&#xff0c;因为企业正在面临仿真复杂工作周期的挑战。需要采用 FlowSimulator 等快速软件解决方案对整个系统进行建模&#xff0c;包括对快…

VAE-pytorch代码

import osimport torch import torch.nn as nn import torch.nn.functional as F from torch.utils.data import DataLoaderfrom torchvision import transforms, datasets from torchvision.utils import save_imagefrom tqdm import tqdmclass VAE(nn.Module): # 定义VAE模型…

【AI大模型】Transformers大模型库(十四):Datasets Viewer

目录 一、引言 二、Datasets Viewer数据查看器 2.1 概述 2.2 示例 三、总结 一、引言 这里的Transformers指的是huggingface开发的大模型库&#xff0c;为huggingface上数以万计的预训练大模型提供预测、训练等服务。 &#x1f917; Transformers 提供了数以千计的预训练…

python-立方和不等式

[题目描述] 试求满足下述立方和不等式的 m 的整数解。 1^32^3...m^3≤n。本题算法如下&#xff1a; 对指定的 n&#xff0c;设置求和循环&#xff0c;从 i1 开始&#xff0c;i 递增1取值&#xff0c;把 i^3 (或 i∗i∗i)累加到 s&#xff0c;直至 s>n&#xff0c;脱离循环作…

VMware Workstation环境下,邮件(E-Mail)服务的安装配置,并用Windows7来验证测试

需求说明: 某企业信息中心计划使用IP地址17216.11.0用于虚拟网络测试,注册域名为xyz.net.cn.并将172.16.11.2作为主域名的服务器(DNS服务器)的IP地址,将172.16.11.3分配给虚拟网络测试的DHCP服务器,将172.16.11.4分配给虚拟网络测试的web服务器,将172.16.11.5分配给FTP服务器…