|
|
|
@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest;
|
|
|
|
|
import cn.hutool.http.HttpResponse; |
|
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.alibaba.fastjson.TypeReference; |
|
|
|
|
import com.alibaba.fastjson.serializer.SerializerFeature; |
|
|
|
@ -11,6 +12,7 @@ import com.google.common.collect.Lists;
|
|
|
|
|
import com.hnac.gglm.bigmodel.BigModelConstants; |
|
|
|
|
import com.hnac.gglm.bigmodel.configuration.BigModelInvokeApi; |
|
|
|
|
import com.hnac.gglm.bigmodel.utils.RequestClientUtil; |
|
|
|
|
import com.hnac.hzims.fdp.constants.ScheduledConstant; |
|
|
|
|
import com.hnac.hzinfo.exception.HzServiceException; |
|
|
|
|
import io.weaviate.client.Config; |
|
|
|
|
import io.weaviate.client.WeaviateAuthClient; |
|
|
|
@ -100,7 +102,7 @@ public class WeaviateService {
|
|
|
|
|
List<List> entitiesList = splitList(entities, 1000); |
|
|
|
|
int total = 0; |
|
|
|
|
for (List entityList : entitiesList) { |
|
|
|
|
Integer insert = this.insert(entityList, className, attrsMap, params); |
|
|
|
|
Integer insert = this.insert(entityList, attrsMap, params); |
|
|
|
|
total += insert; |
|
|
|
|
} |
|
|
|
|
// 查询weaviate 中该表的数据量
|
|
|
|
@ -108,6 +110,36 @@ public class WeaviateService {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 根据条件删除数据 |
|
|
|
|
* @param className 表名 |
|
|
|
|
* @param condition 查询条件 |
|
|
|
|
* @return 删除结果 |
|
|
|
|
*/ |
|
|
|
|
public Boolean deleteCondition(String className,Map<String,String> condition) { |
|
|
|
|
// 查询到相关数据
|
|
|
|
|
Object query = this.query(null, className, condition); |
|
|
|
|
if(Func.isEmpty(query)) { |
|
|
|
|
throw new HzServiceException("暂无数据,删除失败!"); |
|
|
|
|
} |
|
|
|
|
JSONObject queryJson = JSONObject.parseObject(JSON.toJSONString(query)); |
|
|
|
|
JSONArray data = Optional.ofNullable(queryJson).map(json -> json.getJSONObject("Get")) |
|
|
|
|
.map(json -> json.getJSONArray(className)).orElse(null); |
|
|
|
|
if(Func.isNotEmpty(data)) { |
|
|
|
|
List<String> ids = data.stream().map(item -> { |
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(item)); |
|
|
|
|
return Optional.ofNullable(jsonObject) |
|
|
|
|
.map(json -> json.getJSONObject("_additional")) |
|
|
|
|
.map(json -> json.getString("id")) |
|
|
|
|
.orElse(""); |
|
|
|
|
}).filter(Func::isNotEmpty).collect(Collectors.toList()); |
|
|
|
|
if(Func.isNotEmpty(ids)) { |
|
|
|
|
this.delete(ids.stream().collect(Collectors.joining(",")), className); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 将list按size截断为多个list |
|
|
|
|
* @param list 待截断的list |
|
|
|
|
* @param size 截断大小 |
|
|
|
@ -122,14 +154,21 @@ public class WeaviateService {
|
|
|
|
|
return parts; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Integer insert(List entities,String className, Map<String,String> attrsMap, Map<String,Object> params) { |
|
|
|
|
/** |
|
|
|
|
* 批量插入数据 |
|
|
|
|
* @param entities 待插入的数据 |
|
|
|
|
* @param attrsMap 向量計算Map |
|
|
|
|
* @param params 向量計算参数 |
|
|
|
|
* @return 插入数量 |
|
|
|
|
*/ |
|
|
|
|
private Integer insert(List entities, Map<String,String> attrsMap, Map<String,Object> params) { |
|
|
|
|
List<Map<String, Object>> data = new ArrayList<>(); |
|
|
|
|
entities.forEach(entity -> { |
|
|
|
|
// 将entity转换为Map<String,String>
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(entity, SerializerFeature.WriteMapNullValue)); |
|
|
|
|
Map<String,String> map = new HashMap<>(); |
|
|
|
|
Map<String,Object> map = new HashMap<>(); |
|
|
|
|
data.add(this.getVectorData(map,attrsMap)); |
|
|
|
|
jsonObject.forEach((k,v) -> map.put(k,jsonObject.getString(k))); |
|
|
|
|
jsonObject.forEach((k,v) -> map.put(k,jsonObject.get(k))); |
|
|
|
|
}); |
|
|
|
|
params.put("data",data); |
|
|
|
|
String url = gglmUrl + invokeApi.getInsertVectors(); |
|
|
|
@ -138,14 +177,14 @@ public class WeaviateService {
|
|
|
|
|
return (Integer) stringIntegerMap.get("total"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Map<String,Object> getVectorData(Map<String,String> entity,Map<String,String> attrsMap) { |
|
|
|
|
private Map<String,Object> getVectorData(Map<String,Object> entity,Map<String,String> attrsMap) { |
|
|
|
|
Map<String,Object> result = new HashMap<>(2); |
|
|
|
|
result.put("object", entity); |
|
|
|
|
List<Map<String,String>> vectors = new ArrayList<>(); |
|
|
|
|
attrsMap.forEach((k,fields) -> { |
|
|
|
|
Map<String,String> vector = new HashMap<>(); |
|
|
|
|
vector.put("key",k); |
|
|
|
|
String value = Func.toStrList(",", fields).stream().map(field -> entity.get(field)).collect(Collectors.joining(" ")); |
|
|
|
|
String value = Func.toStrList(",", fields).stream().map(field -> entity.get(field)).map(String::valueOf).collect(Collectors.joining(" ")); |
|
|
|
|
vector.put("content", value); |
|
|
|
|
vectors.add(vector); |
|
|
|
|
}); |
|
|
|
|