|
15 | 15 | package apijson.demo; |
16 | 16 |
|
17 | 17 | import apijson.*; |
18 | | -import apijson.orm.AbstractSQLConfig; |
| 18 | +import apijson.mongodb.MongoUtil; |
19 | 19 | import com.alibaba.druid.pool.DruidDataSource; |
20 | | -import com.alibaba.fastjson.JSONArray; |
21 | 20 | import com.alibaba.fastjson.JSONObject; |
22 | 21 | import com.datastax.oss.driver.api.core.CqlSession; |
23 | | -import com.datastax.oss.driver.api.core.cql.PreparedStatement; |
| 22 | + |
24 | 23 | import java.sql.ResultSet; |
25 | 24 | import com.datastax.oss.driver.api.core.cql.Row; |
26 | 25 | //import com.vesoft.nebula.jdbc.impl.NebulaDriver; |
27 | | -import com.zaxxer.hikari.HikariDataSource; |
| 26 | +//import com.zaxxer.hikari.HikariDataSource; |
28 | 27 |
|
29 | 28 | import java.io.Serializable; |
30 | | -import java.net.URI; |
31 | 29 | import java.net.URL; |
32 | | -import java.nio.file.Paths; |
33 | 30 | import java.sql.Connection; |
34 | 31 | import java.sql.ResultSetMetaData; |
35 | 32 | import java.sql.SQLException; |
|
41 | 38 | import apijson.boot.DemoApplication; |
42 | 39 | import apijson.framework.APIJSONSQLExecutor; |
43 | 40 | import apijson.orm.SQLConfig; |
| 41 | +import io.milvus.client.MilvusServiceClient; |
| 42 | +import io.milvus.param.ConnectParam; |
| 43 | +import org.datayoo.moql.ColumnDefinition; |
| 44 | +import org.datayoo.moql.RecordSet; |
| 45 | +import org.datayoo.moql.RecordSetDefinition; |
| 46 | +import org.datayoo.moql.querier.milvus.MilvusQuerier; |
44 | 47 | import org.influxdb.BatchOptions; |
45 | 48 | import org.influxdb.InfluxDB; |
46 | 49 | import org.influxdb.InfluxDBFactory; |
|
52 | 55 | import org.springframework.data.redis.serializer.GenericToStringSerializer; |
53 | 56 | import org.springframework.data.redis.serializer.StringRedisSerializer; |
54 | 57 |
|
| 58 | +import static apijson.demo.DemoSQLConfig.DATABASE_MILVUS; |
55 | 59 | import static apijson.framework.APIJSONConstant.PRIVACY_; |
56 | 60 | import static apijson.framework.APIJSONConstant.USER_; |
57 | 61 |
|
@@ -162,10 +166,10 @@ public Connection getConnection(SQLConfig<Long> config) throws Exception { |
162 | 166 | try { |
163 | 167 | DataSource ds; |
164 | 168 | switch (datasource) { |
165 | | - case "HIKARICP": |
166 | | - ds = DemoApplication.getApplicationContext().getBean(HikariDataSource.class); |
167 | | - // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_HIKARICP 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_HIKARICP.getConnection(); |
168 | | - break; |
| 169 | +// case "HIKARICP": |
| 170 | +// ds = DemoApplication.getApplicationContext().getBean(HikariDataSource.class); |
| 171 | +// // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_HIKARICP 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_HIKARICP.getConnection(); |
| 172 | +// break; |
169 | 173 | default: |
170 | 174 | Map<String, DruidDataSource> dsMap = DemoApplication.getApplicationContext().getBeansOfType(DruidDataSource.class); |
171 | 175 | // 另一种方式是 DemoDataSourceConfig 初始化获取到 DataSource 后给静态变量 DATA_SOURCE_DRUID 赋值: ds = DemoDataSourceConfig.DATA_SOURCE_DRUID.getConnection(); |
@@ -199,9 +203,68 @@ public Connection getConnection(SQLConfig<Long> config) throws Exception { |
199 | 203 | return super.getConnection(config); |
200 | 204 | } |
201 | 205 |
|
202 | | - |
203 | 206 | @Override |
204 | 207 | 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 | + |
205 | 268 | boolean isCassandra = config.isCassandra(); |
206 | 269 | boolean isInfluxDB = config.isInfluxDB(); |
207 | 270 |
|
@@ -383,4 +446,10 @@ public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) |
383 | 446 | // return StringUtil.firstCase(JSONResponse.formatUnderline(key, true), false); |
384 | 447 | // } |
385 | 448 |
|
| 449 | + |
| 450 | + @Override |
| 451 | + protected Object getValue(SQLConfig<Long> config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table, int columnIndex, String lable, Map<String, JSONObject> childMap) throws Exception { |
| 452 | + Object v = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap); |
| 453 | + return MongoUtil.getValue(v); |
| 454 | + } |
386 | 455 | } |
0 commit comments