两种路线:
第一种是把byte[]转换为List< float >,然后存储到Milvus的floatVector中 第二种是把byte[]转换为ByteBuffer,然后存储到Milvus的BinaryVector中
步骤:
我先用的是第一种,但是在转换float过程中,报错NaN,某些数据无法转换,于是换第二种,伪代码如下
. . .
FieldType fieldType = FieldType . newBuilder ( )
. withName ( "feature" )
. withDataType ( DataType. BinaryVector )
. withDimension ( 8256 )
. build ( ) ;
. . .
byte [ ] featureData= . . . ;
ByteBuffer byteBuffer = ByteBuffer . wrap ( featureData) ;
byteBuffer. position ( byteBuffer. capacity ( ) ) ;
List < ByteBuffer > vector = new ArrayList < > ( ) ;
vector. add ( byteBuffer) ;
List < InsertParam. Field > fields = new ArrayList < > ( ) ;
fields. add ( new InsertParam. Field ( "feature" , vector) ) ;
InsertParam insertParam = InsertParam . newBuilder ( )
. withCollectionName ( collection_name)
. withFields ( fields)
. build ( ) ;
R < MutationResult > mutationResultR = milvusClient. insert ( insertParam) ;
报错:Incorrect dimension for field ‘feature’: the no.0 vector’s dimension: 0 is not equal to field’s dimension: 1032
解决思路:先百度谷歌,然后没找到解决办法,于是看报错位置,查源码,定位报错原因,如下 然后发现byte[]在经过ByteBuffer byteBuffer = ByteBuffer.wrap(featureData);
这段代码转换时,position被默认设置为0,因此加入byteBuffer.position(byteBuffer.capacity());
修改position值 还有需要注意的是设置该字段的向量维度时,需要设置为byte[]的长度乘以8