Skip to content

Commit c98e19b

Browse files
committed
新增 getClient, closeClient 等方法
1 parent 106a470 commit c98e19b

File tree

1 file changed

+81
-23
lines changed

1 file changed

+81
-23
lines changed

src/main/java/apijson/milvus/MilvusUtil.java

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* \@Override
4343
* public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
4444
* if (config.isMilvus()) {
45-
* return MilvusUtil.execute(config, unknownType);
45+
* return MilvusUtil.execute(config, null, unknownType);
4646
* }
4747
*
4848
* return super.execute(config, unknownType);
@@ -51,19 +51,58 @@
5151
public class MilvusUtil {
5252
public static final String TAG = "MilvusUtil";
5353

54-
public static <T> JSONObject execute(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
55-
if (RequestMethod.isQueryMethod(config.getMethod())) {
56-
List<JSONObject> list = executeQuery(config, sql, unknownType);
57-
JSONObject result = list == null || list.isEmpty() ? null : list.get(0);
58-
if (result == null) {
59-
result = new JSONObject(true);
60-
}
54+
public static final Map<String, MilvusServiceClient> CLIENT_MAP = new LinkedHashMap<>();
55+
public static <T> MilvusServiceClient getClient(@NotNull SQLConfig<T> config) {
56+
String uri = config.getDBUri();
57+
String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
58+
59+
MilvusServiceClient conn = CLIENT_MAP.get(key);
60+
if (conn == null) {
61+
conn = new MilvusServiceClient(
62+
ConnectParam.newBuilder()
63+
.withUri(config.getDBUri())
64+
.withAuthorization(config.getDBAccount(), config.getDBPassword())
65+
.build()
66+
);
67+
CLIENT_MAP.put(key, conn);
68+
}
69+
return conn;
70+
}
71+
72+
public static <T> void closeClient(@NotNull SQLConfig<T> config) {
73+
MilvusServiceClient conn = getClient(config);
74+
if (conn != null) {
75+
String uri = config.getDBUri();
76+
String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
77+
CLIENT_MAP.remove(key);
78+
79+
// try {
80+
conn.close();
81+
// }
82+
// catch (Throwable e) {
83+
// e.printStackTrace();
84+
// }
85+
}
86+
}
6187

62-
if (list != null && list.size() > 1) {
63-
result.put(KEY_RAW_LIST, list);
88+
public static <T> void closeAllClient() {
89+
Collection<MilvusServiceClient> cs = CLIENT_MAP.values();
90+
for (MilvusServiceClient c : cs) {
91+
try {
92+
c.close();
93+
}
94+
catch (Throwable e) {
95+
e.printStackTrace();
6496
}
97+
}
98+
99+
CLIENT_MAP.clear();
100+
}
65101

66-
return result;
102+
103+
public static <T> JSONObject execute(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
104+
if (RequestMethod.isQueryMethod(config.getMethod())) {
105+
return execQuery(config, sql, unknownType);
67106
}
68107

69108
return executeUpdate(config, sql);
@@ -75,9 +114,12 @@ public static <T> int execUpdate(SQLConfig<T> config, String sql) throws Excepti
75114
}
76115

77116
public static <T> JSONObject executeUpdate(SQLConfig<T> config, String sql) throws Exception {
78-
MilvusServiceClient milvusClient = new MilvusServiceClient(
79-
ConnectParam.newBuilder().withUri(config.getDBUri()).build()
80-
);
117+
return executeUpdate(null, config, sql);
118+
}
119+
public static <T> JSONObject executeUpdate(MilvusServiceClient client, SQLConfig<T> config, String sql) throws Exception {
120+
if (client == null) {
121+
client = getClient(config);
122+
}
81123

82124
R<MutationResult> mr;
83125
JSONObject result = AbstractParser.newSuccessResult();
@@ -135,7 +177,7 @@ else if (v instanceof Collection) {
135177
.withFields(fl)
136178
.build();
137179

138-
mr = milvusClient.insert(param);
180+
mr = client.insert(param);
139181
}
140182
else if (method == RequestMethod.PUT) {
141183
// UpdateCredentialParam param = UpdateCredentialParam.newBuilder()
@@ -148,7 +190,7 @@ else if (method == RequestMethod.DELETE) {
148190
.withCollectionName(config.getSQLTable())
149191
.withExpr(config.setPrepared(false).getWhereString(false))
150192
.build();
151-
mr = milvusClient.delete(param);
193+
mr = client.delete(param);
152194
}
153195
else {
154196
throw new UnsupportedOperationException("Milvus Java SDK 暂不支持 APIJSON " + method + " 这个操作!");
@@ -184,21 +226,37 @@ else if (method == RequestMethod.DELETE) {
184226
return result;
185227
}
186228

229+
230+
public static <T> JSONObject execQuery(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
231+
List<JSONObject> list = executeQuery(config, sql, unknownType);
232+
JSONObject result = list == null || list.isEmpty() ? null : list.get(0);
233+
if (result == null) {
234+
result = new JSONObject(true);
235+
}
236+
237+
if (list != null && list.size() > 1) {
238+
result.put(KEY_RAW_LIST, list);
239+
}
240+
241+
return result;
242+
}
243+
187244
public static <T> List<JSONObject> executeQuery(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
188-
// 构建Milvus客户端
189-
MilvusServiceClient milvusClient = new MilvusServiceClient(
190-
ConnectParam.newBuilder().withUri(config.getDBUri()).build()
191-
);
245+
return executeQuery(null, config, sql, unknownType);
246+
}
247+
public static <T> List<JSONObject> executeQuery(MilvusServiceClient client, @NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
248+
if (client == null) {
249+
client = getClient(config);
250+
}
192251

193252
/*
194253
查询语句含义:从book集合中筛选数据,并返回col1,col2两个列。筛选条件为,当数据的col3列值为4,col4列值为'a','b','c'中的任意一
195254
个,且vec向量字段采用'L2'类型匹配,值为'[[1.0, 2.0, 3.0],[1.1,2.1,3.1]]'。另外,采用强一致性级别在10个单元内进行检索,取第11到第15,5条命中记录。
196255
*/
197256
// String sql = "select id,userId,momentId,content,date from Comment where vMatch(vec, 'L2', '[[1]]') and consistencyLevel('STRONG') limit 1,1";
198257

199-
200258
// 使用Milvus客户端创建Milvus查询器
201-
MilvusQuerier milvusQuerier = new MilvusQuerier(milvusClient);
259+
MilvusQuerier milvusQuerier = new MilvusQuerier(client);
202260
// 使用查询器执行sql语句,并返回查询结果
203261
RecordSet recordSet = milvusQuerier.query(StringUtil.isEmpty(sql) ? config.getSQL(false) : sql);
204262

@@ -217,7 +275,7 @@ public static <T> List<JSONObject> executeQuery(@NotNull SQLConfig<T> config, St
217275
for (int i = 0; i < list.size(); i++) {
218276
Map<String, Object> map = list.get(i);
219277

220-
JSONObject obj = new JSONObject(map == null ? new HashMap<>() : map);
278+
JSONObject obj = map == null ? new JSONObject(1) : new JSONObject(new LinkedHashMap<>(map));
221279
// obj.put(col.getValue(), os[j]);
222280
// for (int j = 0; j < os.length; j++) {
223281
// ColumnDefinition col = cols.get(j);

0 commit comments

Comments
 (0)