Skip to content

Commit 0f09871

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

File tree

3 files changed

+21
-93
lines changed

3 files changed

+21
-93
lines changed
Binary file not shown.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
<artifactId>apijson-milvus</artifactId>
7979
<version>1.0.0</version>
8080
</dependency>
81+
<dependency>
82+
<groupId>com.github.APIJSON</groupId>
83+
<artifactId>apijson-influxdb</artifactId>
84+
<version>1.0.0</version>
85+
</dependency>
8186
<dependency>
8287
<groupId>com.github.APIJSON</groupId>
8388
<artifactId>apijson-mongodb</artifactId>

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

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

1717
import apijson.*;
18+
import apijson.influxdb.InfluxDBUtil;
1819
import apijson.milvus.MilvusUtil;
1920
import apijson.mongodb.MongoUtil;
2021
import com.alibaba.druid.pool.DruidDataSource;
@@ -224,26 +225,32 @@ public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType)
224225
return result;
225226
}
226227

227-
RequestMethod method = config.getMethod();
228-
boolean isWrite = ! RequestMethod.isQueryMethod(method);
229-
if (method == null && ! isWrite) {
230-
String trimmedSQL = sql == null ? null : sql.trim();
231-
String sqlPrefix = trimmedSQL == null || trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
232-
isWrite = sqlPrefix.startsWith("INSERT ") || sqlPrefix.startsWith("UPDATE ") || sqlPrefix.startsWith("DELETE ");
228+
if (sql != null && config.getMethod() == null) {
229+
String trimmedSQL = sql.trim();
230+
String sqlPrefix = trimmedSQL.length() < 7 ? "" : trimmedSQL.substring(0, 7).toUpperCase();
231+
if (sqlPrefix.startsWith("INSERT ")) {
232+
config.setMethod(RequestMethod.POST);
233+
}
234+
else if (sqlPrefix.startsWith("UPDATE ")) {
235+
config.setMethod(RequestMethod.PUT);
236+
}
237+
else if (sqlPrefix.startsWith("DELETE ")) {
238+
config.setMethod(RequestMethod.DELETE);
239+
}
233240
}
234241

235242
List<JSONObject> resultList = new ArrayList<>();
236243

237244
if (isMilvus) {
238-
return MilvusUtil.execute(config, unknownType);
245+
return MilvusUtil.execute(config, sql, unknownType);
239246
}
240247

241248
if (isCassandra) {
242249
CqlSession session = CqlSession.builder()
243250
// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
244251
.withCloudSecureConnectBundle(new URL(config.getDBUri()))
245252
.withAuthCredentials(config.getDBAccount(), config.getDBPassword())
246-
.withKeyspace(config.getSQLSchema())
253+
.withKeyspace(config.getSchema())
247254
.build();
248255

249256
// if (config.isPrepared()) {
@@ -270,91 +277,7 @@ public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType)
270277
}
271278
}
272279
else if (isInfluxDB) {
273-
InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword());
274-
influxDB.setDatabase(config.getSchema());
275-
276-
if (isWrite) {
277-
influxDB.enableBatch(
278-
BatchOptions.DEFAULTS
279-
.threadFactory(runnable -> {
280-
Thread thread = new Thread(runnable);
281-
thread.setDaemon(true);
282-
return thread;
283-
})
284-
);
285-
286-
Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close));
287-
288-
influxDB.write(sql);
289-
290-
result = DemoParser.newSuccessResult();
291-
292-
if (method == RequestMethod.POST) {
293-
List<List<Object>> values = config.getValues();
294-
result.put(JSONResponse.KEY_COUNT, values == null ? 0 : values.size());
295-
} else {
296-
String idKey = config.getIdKey();
297-
Object id = config.getId();
298-
Object idIn = config.getIdIn();
299-
if (id != null) {
300-
result.put(idKey, id);
301-
}
302-
if (idIn != null) {
303-
result.put(idKey + "[]", idIn);
304-
}
305-
306-
if (method == RequestMethod.PUT) {
307-
Map<String, Object> content = config.getContent();
308-
result.put(JSONResponse.KEY_COUNT, content == null ? 0 : content.size());
309-
} else {
310-
result.put(JSONResponse.KEY_COUNT, id == null && idIn instanceof Collection ? ((Collection<?>) idIn).size() : 1); // FIXME 直接 SQLAuto 传 Flux/InfluxQL INSERT 如何取数量?
311-
}
312-
}
313-
314-
return result;
315-
}
316-
317-
QueryResult qr = influxDB.query(new Query(sql));
318-
319-
String err = qr == null ? null : qr.getError();
320-
if (StringUtil.isNotEmpty(err, true)) {
321-
throw new SQLException(err);
322-
}
323-
324-
List<QueryResult.Result> list = qr == null ? null : qr.getResults();
325-
if (list == null || list.isEmpty()) {
326-
return new JSONObject(true);
327-
}
328-
329-
for (int i = 0; i < list.size(); i++) {
330-
QueryResult.Result qyrt = list.get(i);
331-
List<QueryResult.Series> seriesList = qyrt.getSeries();
332-
if (seriesList == null || seriesList.isEmpty()) {
333-
continue;
334-
}
335-
336-
for (int j = 0; j < seriesList.size(); j++) {
337-
QueryResult.Series series = seriesList.get(j);
338-
List<List<Object>> valuesList = series.getValues();
339-
if (valuesList == null || valuesList.isEmpty()) {
340-
continue;
341-
}
342-
343-
List<String> columns = series.getColumns();
344-
for (int k = 0; k < valuesList.size(); k++) {
345-
346-
List<Object> values = valuesList.get(k);
347-
JSONObject obj = new JSONObject(true);
348-
if (values != null) {
349-
for (int l = 0; l < values.size(); l++) {
350-
obj.put(columns.get(l), values.get(l));
351-
}
352-
}
353-
resultList.add(obj);
354-
}
355-
}
356-
}
357-
280+
return InfluxDBUtil.execute(config, sql, unknownType);
358281
}
359282

360283
// TODO 把 execute 内与缓存无关只与数据库读写逻辑相关的代码抽取到 executeSQL 函数

0 commit comments

Comments
 (0)