Skip to content

Commit 99dadd9

Browse files
committed
Java: MultiDataSource 依赖 apijson-milvus 来简化代码
1 parent a0e703d commit 99dadd9

File tree

4 files changed

+85
-107
lines changed

4 files changed

+85
-107
lines changed
Binary file not shown.

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/pom.xml

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
<artifactId>apijson-router</artifactId>
7474
<version>1.8.0</version>
7575
</dependency>
76+
<dependency>
77+
<groupId>com.github.APIJSON</groupId>
78+
<artifactId>apijson-milvus</artifactId>
79+
<version>1.0.0</version>
80+
</dependency>
7681
<dependency>
7782
<groupId>com.github.APIJSON</groupId>
7883
<artifactId>apijson-mongodb</artifactId>
@@ -148,10 +153,13 @@
148153
<groupId>org.datayoo.moql</groupId>
149154
<artifactId>moql-querier-milvus</artifactId>
150155
<version>1.0.0</version>
156+
<groupId>io.milvus</groupId>
157+
<artifactId>milvus-sdk-java</artifactId>
158+
<version>2.2.2</version>
151159
<exclusions>
152160
<exclusion>
153-
<groupId>org.apache.logging.log4j</groupId>
154-
<artifactId>log4j-slf4j-impl</artifactId>
161+
<groupId>org.apache.logging.log4j</groupId>
162+
<artifactId>log4j-slf4j-impl</artifactId>
155163
</exclusion>
156164
</exclusions>
157165
</dependency>
@@ -194,6 +202,24 @@
194202
</exclusion>
195203
</exclusions>
196204
</dependency>
205+
<dependency>
206+
<groupId>org.datayoo.moql</groupId>
207+
<artifactId>moql-meta</artifactId>
208+
<version>1.1.8</version>
209+
<scope>compile</scope>
210+
</dependency>
211+
<dependency>
212+
<groupId>org.datayoo.moql</groupId>
213+
<artifactId>moql-engine</artifactId>
214+
<version>1.4.6</version>
215+
<scope>compile</scope>
216+
</dependency>
217+
<dependency>
218+
<groupId>org.datayoo.moql</groupId>
219+
<artifactId>moql-querier</artifactId>
220+
<version>1.2.3</version>
221+
<scope>compile</scope>
222+
</dependency>
197223

198224
</dependencies>
199225

@@ -237,14 +263,14 @@
237263

238264
<repository>
239265
<id>spring-snapshots</id>
240-
<url>http://repo.spring.io/snapshot</url>
266+
<url>https://repo.spring.io/snapshot</url>
241267
<snapshots>
242268
<enabled>true</enabled>
243269
</snapshots>
244270
</repository>
245271
<repository>
246272
<id>spring-milestones</id>
247-
<url>http://repo.spring.io/milestone</url>
273+
<url>https://repo.spring.io/milestone</url>
248274
<snapshots>
249275
<enabled>true</enabled>
250276
</snapshots>
@@ -253,11 +279,11 @@
253279
<pluginRepositories>
254280
<pluginRepository>
255281
<id>spring-snapshots</id>
256-
<url>http://repo.spring.io/snapshot</url>
282+
<url>https://repo.spring.io/snapshot</url>
257283
</pluginRepository>
258284
<pluginRepository>
259285
<id>spring-milestones</id>
260-
<url>http://repo.spring.io/milestone</url>
286+
<url>https://repo.spring.io/milestone</url>
261287
</pluginRepository>
262288
</pluginRepositories>
263289

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLConfig.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import java.util.*;
2424

2525
import apijson.*;
26+
import apijson.orm.AbstractParser;
2627
import apijson.orm.AbstractSQLConfig;
28+
import apijson.orm.Parser;
2729
import com.alibaba.fastjson.annotation.JSONField;
2830

2931
import apijson.column.ColumnUtil;
@@ -72,11 +74,17 @@ public String getQuote() {
7274

7375
@Override
7476
public String getLimitString() {
75-
if (DATABASE_MILVUS.equals(getDatabase()) && RequestMethod.isGetMethod(getMethod(), true)) {
77+
if (isMilvus()) {
7678
int count = getCount();
79+
if (count == 0) {
80+
Parser<Long> parser = getParser();
81+
count = parser == null ? AbstractParser.MAX_QUERY_COUNT : parser.getMaxQueryCount();
82+
}
83+
7784
int offset = getOffset(getPage(), count);
78-
return " LIMIT " + offset + ", " + count;
85+
return " LIMIT " + offset + ", " + count; // 目前 moql-transx 的限制
7986
}
87+
8088
return super.getLimitString();
8189
}
8290

@@ -210,7 +218,8 @@ public String getDBUri() {
210218
}
211219

212220
if (isMySQL()) {
213-
// 这个是 MySQL 8.0 及以上,要加 userSSL=false return "jdbc:mysql://localhost:3306?userSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
221+
// 这个是 MySQL 8.0 及以上,要加 userSSL=false
222+
// return "jdbc:mysql://47.122.25.116:3306?userSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
214223
// 以下是 MySQL 5.7 及以下
215224
return "jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"; //TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
216225
}
@@ -246,27 +255,9 @@ public String getDBUri() {
246255
return "jdbc:mongodb://atlas-sql-6593c65c296c5865121e6ebe-xxskv.a.query.mongodb.net/myVirtualDatabase?ssl=true&authSource=admin";
247256
}
248257

249-
return "";
250-
}
251-
252-
253-
// TODO 迁移到 APIJSON 主项目 <<<<<<<<<<<<<<<<<<<<
254-
@Override
255-
public String getSchema() {
256-
String sch = super.getSchema();
257-
if (StringUtil.isEmpty(sch) && isInfluxDB()) {
258-
sch = DEFAULT_SCHEMA;
259-
}
260-
return sch;
261-
}
262-
263-
@Override
264-
public String getSQLSchema() {
265-
return isInfluxDB() ? null : super.getSQLSchema();
258+
return null;
266259
}
267260

268-
// TODO 迁移到 APIJSON 主项目 >>>>>>>>>>>>>>>>>>>>>>
269-
270261
private String dbAccount;
271262
public DemoSQLConfig setDBAccount(String dbAccount) {
272263
this.dbAccount = dbAccount;
@@ -457,6 +448,24 @@ protected void onJoinComplexRelation(String sql, String quote, Join join, String
457448
}
458449

459450

451+
452+
// TODO 迁移到 apijson-influxdb 主项目 <<<<<<<<<<<<<<<<<<<<
453+
@Override
454+
public String getSchema() {
455+
String sch = super.getSchema();
456+
if (StringUtil.isEmpty(sch) && isInfluxDB()) {
457+
sch = DEFAULT_SCHEMA;
458+
}
459+
return sch;
460+
}
461+
462+
@Override
463+
public String getSQLSchema() {
464+
return isInfluxDB() ? null : super.getSQLSchema();
465+
}
466+
467+
// TODO 迁移到 apijson-influxdb 主项目 >>>>>>>>>>>>>>>>>>>>>>
468+
460469
@Override
461470
public String getSQLTable() {
462471
String t = super.getSQLTable();

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLExecutor.java

Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package apijson.demo;
1616

1717
import apijson.*;
18+
import apijson.milvus.MilvusUtil;
1819
import apijson.mongodb.MongoUtil;
1920
import com.alibaba.druid.pool.DruidDataSource;
2021
import com.alibaba.fastjson.JSONObject;
@@ -43,6 +44,7 @@
4344
import org.datayoo.moql.ColumnDefinition;
4445
import org.datayoo.moql.RecordSet;
4546
import org.datayoo.moql.RecordSetDefinition;
47+
import org.datayoo.moql.querier.DataQuerier;
4648
import org.datayoo.moql.querier.milvus.MilvusQuerier;
4749
import org.influxdb.BatchOptions;
4850
import org.influxdb.InfluxDB;
@@ -205,70 +207,12 @@ public Connection getConnection(SQLConfig<Long> config) throws Exception {
205207

206208
@Override
207209
public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
208-
if (DATABASE_MILVUS.equals(config.getDatabase())) { // 3.0.0 及以下要这样连接
209-
String uri = config.getDBUri();
210-
//
211-
// int start = uri.indexOf("://");
212-
// String prefix = uri.substring(0, start);
213-
//
214-
// uri = uri.substring(start + "://".length());
215-
// int end = uri.indexOf(":");
216-
// int port = Integer.parseInt(uri.substring(end + 1));
217-
// String host = uri.substring(0, end);
218-
219-
// 构建Milvus客户端
220-
MilvusServiceClient milvusClient = new MilvusServiceClient(
221-
ConnectParam.newBuilder().withUri(uri).build()
222-
);
223-
224-
// 使用Milvus客户端创建Milvus查询器
225-
MilvusQuerier milvusQuerier = new MilvusQuerier(milvusClient);
226-
227-
/*
228-
查询语句含义:从book集合中筛选数据,并返回col1,col2两个列。筛选条件为,当数据的col3列值为4,col4列值为'a','b','c'中的任意一
229-
个,且vec向量字段采用'L2'类型匹配,值为'[[1.0, 2.0, 3.0],[1.1,2.1,3.1]]'。另外,采用强一致性级别在10个单元内进行检索,取第11到第15,5条命中记录。
230-
*/
231-
String sql = config.getSQL(false); //
232-
// String sql = "select id,userId,momentId,content,date from Comment where vMatch(vec, 'L2', '[[1]]') and consistencyLevel('STRONG') limit 1,1";
233-
// 使用查询器执行sql语句,并返回查询结果
234-
RecordSet recordSet = milvusQuerier.query(sql);
235-
236-
// int count = recordSet == null ? 0 : recordSet.getRecordsCount();
237-
List<Map<String, Object>> list = recordSet == null ? null : recordSet.getRecordsAsMaps();
238-
// RecordSetDefinition def = recordSet.getRecordSetDefinition();
239-
// List<ColumnDefinition> cols = def.getColumns();
240-
241-
// List<Object[]> list = count <= 0 ? null : recordSet.getRecords();
242-
243-
if (list == null || list.isEmpty()) {
244-
return new JSONObject(true);
245-
}
246-
247-
List<JSONObject> nl = new ArrayList<>(list.size());
248-
for (int i = 0; i < list.size(); i++) {
249-
Map<String, Object> map = list.get(i);
250-
251-
JSONObject obj = new JSONObject(map == null ? new HashMap<>() : map);
252-
// obj.put(col.getValue(), os[j]);
253-
// for (int j = 0; j < os.length; j++) {
254-
// ColumnDefinition col = cols.get(j);
255-
// obj.put(col.getValue(), os[j]);
256-
// }
257-
nl.add(obj);
258-
}
259-
260-
JSONObject result = nl.get(0); // JSON.parseObject(list.get(0));
261-
if (nl.size() > 1) {
262-
result.put(KEY_RAW_LIST, nl);
263-
}
264-
265-
return result;
266-
}
267-
210+
boolean isMilvus = DATABASE_MILVUS.equals(config.getDatabase()); // APIJSON 6.4.0+ 可用 config.isMilvus();
268211
boolean isCassandra = config.isCassandra();
269212
boolean isInfluxDB = config.isInfluxDB();
270213

271-
if (isCassandra || isInfluxDB) {
214+
if (isMilvus || isCassandra || isInfluxDB) {
215+
// TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数
272216
String sql = config.getSQL(false); // config.isPrepared());
273217
List<JSONObject> cache = getCache(sql, config);
274218
int position = config.getPosition();
@@ -288,13 +232,18 @@ public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType)
288232
isWrite = sqlPrefix.startsWith("INSERT ") || sqlPrefix.startsWith("UPDATE ") || sqlPrefix.startsWith("DELETE ");
289233
}
290234

235+
List<JSONObject> resultList = new ArrayList<>();
236+
237+
if (isMilvus) {
238+
return MilvusUtil.execute(config, unknownType);
239+
}
291240

292241
if (isCassandra) {
293242
CqlSession session = CqlSession.builder()
294243
// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
295244
.withCloudSecureConnectBundle(new URL(config.getDBUri()))
296245
.withAuthCredentials(config.getDBAccount(), config.getDBPassword())
297-
.withKeyspace(config.getSchema())
246+
.withKeyspace(config.getSQLSchema())
298247
.build();
299248

300249
// if (config.isPrepared()) {
@@ -316,16 +265,11 @@ public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType)
316265
return new JSONObject(true);
317266
}
318267

319-
result = JSON.parseObject(list.get(0));
320-
if (list.size() > 1) {
321-
result.put(KEY_RAW_LIST, list);
268+
for (int i = 0; i < list.size(); i++) {
269+
resultList.add(JSON.parseObject(list.get(i)));
322270
}
323-
324-
return result;
325271
}
326-
327-
328-
if (isInfluxDB) {
272+
else if (isInfluxDB) {
329273
InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword());
330274
influxDB.setDatabase(config.getSchema());
331275

@@ -382,8 +326,6 @@ public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType)
382326
return new JSONObject(true);
383327
}
384328

385-
List<JSONObject> resultList = new ArrayList<>();
386-
387329
for (int i = 0; i < list.size(); i++) {
388330
QueryResult.Result qyrt = list.get(i);
389331
List<QueryResult.Series> seriesList = qyrt.getSeries();
@@ -413,16 +355,17 @@ public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType)
413355
}
414356
}
415357

416-
result = resultList.isEmpty() ? new JSONObject() : resultList.get(0);
417-
if (resultList.size() > 1) {
418-
result.put(KEY_RAW_LIST, resultList);
419-
}
420-
421-
putCache(sql, resultList, config);
358+
}
422359

423-
return result;
360+
// TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数
361+
result = resultList.isEmpty() ? new JSONObject() : resultList.get(0);
362+
if (resultList.size() > 1) {
363+
result.put(KEY_RAW_LIST, resultList);
424364
}
425365

366+
putCache(sql, resultList, config);
367+
368+
return result;
426369
}
427370

428371
return super.execute(config, unknownType);

0 commit comments

Comments
 (0)