Skip to content

Commit 47a65aa

Browse files
committed
初始化提交
1 parent ed5eb00 commit 47a65aa

File tree

5 files changed

+268
-1
lines changed

5 files changed

+268
-1
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/target/
2+
.classpath
3+
.project
4+
.settings
5+
.idea
6+
*.iml

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright ©2021 APIJSON(https://github.com/APIJSON)
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# apijson-mongodb [![](https://jitpack.io/v/APIJSON/apijson-mongodb.svg)](https://jitpack.io/#APIJSON/apijson-mongodb)
2+
腾讯 [APIJSON](https://github.com/Tencent/APIJSON) 的 MongoDB 数据库插件,可通过 Maven, Gradle 等远程依赖。<br />
3+
A MongoDB plugin for Tencent [APIJSON](https://github.com/Tencent/APIJSON)
4+
5+
![image](https://github-production-user-asset-6210df.s3.amazonaws.com/5738175/293696252-36406e20-b157-4121-bca7-280491462481.png)
6+
7+
## 添加依赖
8+
## Add Dependency
9+
10+
### Maven
11+
#### 1. 在 pom.xml 中添加 JitPack 仓库
12+
#### 1. Add the JitPack repository to pom.xml
13+
```xml
14+
<repositories>
15+
<repository>
16+
<id>jitpack.io</id>
17+
<url>https://jitpack.io</url>
18+
</repository>
19+
</repositories>
20+
```
21+
22+
![image](https://user-images.githubusercontent.com/5738175/167261814-d75d8fff-0e64-4534-a840-60ef628a8873.png)
23+
24+
<br />
25+
26+
#### 2. 在 pom.xml 中添加 apijson-mongodb 依赖
27+
#### 2. Add the apijson-mongodb dependency to pom.xml
28+
```xml
29+
<dependency>
30+
<groupId>com.github.APIJSON</groupId>
31+
<artifactId>apijson-mongodb</artifactId>
32+
<version>LATEST</version>
33+
</dependency>
34+
```
35+
36+
<br />
37+
38+
https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/pom.xml
39+
40+
<br />
41+
<br />
42+
43+
### Gradle
44+
#### 1. 在项目根目录 build.gradle 中最后添加 JitPack 仓库
45+
#### 1. Add the JitPack repository in your root build.gradle at the end of repositories
46+
```gradle
47+
allprojects {
48+
repositories {
49+
maven { url 'https://jitpack.io' }
50+
}
51+
}
52+
```
53+
<br />
54+
55+
#### 2. 在项目某个 module 目录(例如 `app`) build.gradle 中添加 apijson-mongodb 依赖
56+
#### 2. Add the apijson-mongodb dependency in one of your modules(such as `app`)
57+
```gradle
58+
dependencies {
59+
implementation 'com.github.APIJSON:apijson-mongodb:latest'
60+
}
61+
```
62+
63+
<br />
64+
<br />
65+
<br />
66+
67+
## 使用
68+
## Usage
69+
70+
在你项目继承 AbstractSQLExecutor 的子类重写方法 getValue <br/>
71+
Override getValue in your SQLExecutor extends AbstractSQLExecutor
72+
```java
73+
@Override
74+
protected Object getValue(SQLConfig<Long> config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table, int columnIndex, String lable, Map<String, JSONObject> childMap) throws Exception {
75+
Object v = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap);
76+
return MongoUtil.getValue(v);
77+
}
78+
```
79+
80+
#### [MongoUtil](/apijson/milvus/MongoUtil.java) 的注释及 [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot)[DemoSQLExecutor](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLExecutor.java) <br />
81+
82+
#### See document in [MongoUtil](/apijson/milvus/MongoUtil.java) and [DemoSQLExecutor](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLExecutor.java) in [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot)
83+
84+
<br />
85+
<br />
86+
<br />
87+
88+
有问题可以去 Tencent/APIJSON 提 issue <br />
89+
https://github.com/Tencent/APIJSON/issues/36
90+
91+
<br /><br />
92+
93+
#### 点右上角 ⭐Star 支持一下,谢谢 ^_^
94+
#### Please ⭐Star this project ^_^
95+
https://github.com/APIJSON/apijson-mongodb

pom.xml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>apijson.milvus</groupId>
7+
<artifactId>apijson-milvus</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>apijson-milvus</name>
12+
<description>A Milvus plugin for Tencent APIJSON</description>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- APIJSON 需要用的依赖库,1.2.0 以上 -->
22+
<dependency>
23+
<groupId>com.alibaba</groupId>
24+
<artifactId>fastjson</artifactId>
25+
<version>1.2.83</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.github.Tencent</groupId>
29+
<artifactId>APIJSON</artifactId>
30+
<version>6.3.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.datayoo.moql</groupId>
34+
<artifactId>moql-querier-milvus</artifactId>
35+
<version>1.0.3</version>
36+
<exclusions>
37+
<exclusion>
38+
<groupId>org.apache.logging.log4j</groupId>
39+
<artifactId>log4j-slf4j-impl</artifactId>
40+
</exclusion>
41+
</exclusions>
42+
</dependency>
43+
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>3.8.1</version>
52+
<configuration>
53+
<source>1.8</source>
54+
<target>1.8</target>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
<repositories>
61+
<!-- APIJSON 必须用到的托管平台 -->
62+
<repository>
63+
<id>jitpack.io</id>
64+
<url>https://jitpack.io</url>
65+
<snapshots>
66+
<enabled>true</enabled>
67+
</snapshots>
68+
</repository>
69+
</repositories>
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*Copyright ©2024 APIJSON(https://github.com/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package apijson.milvus;
16+
17+
import apijson.NotNull;
18+
import apijson.orm.SQLConfig;
19+
import com.alibaba.fastjson.JSONObject;
20+
import io.milvus.client.MilvusServiceClient;
21+
import io.milvus.param.ConnectParam;
22+
import org.datayoo.moql.RecordSet;
23+
import org.datayoo.moql.querier.milvus.MilvusQuerier;
24+
25+
import java.util.ArrayList;
26+
import java.util.HashMap;
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
import static apijson.orm.AbstractSQLExecutor.KEY_RAW_LIST;
31+
32+
33+
/**
34+
* @author Lemon
35+
* @see DemoSQLExecutor 重写 execute 方法:
36+
* \@Override
37+
* public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
38+
*
39+
* return MilvusUtil.execute(config, unknownType);
40+
* }
41+
*/
42+
public class MilvusUtil {
43+
public static final String TAG = "MilvusUtil";
44+
45+
public static JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
46+
// 构建Milvus客户端
47+
MilvusServiceClient milvusClient = new MilvusServiceClient(
48+
ConnectParam.newBuilder().withUri(config.getDBUri()).build()
49+
);
50+
51+
// 使用Milvus客户端创建Milvus查询器
52+
MilvusQuerier milvusQuerier = new MilvusQuerier(milvusClient);
53+
54+
/*
55+
查询语句含义:从book集合中筛选数据,并返回col1,col2两个列。筛选条件为,当数据的col3列值为4,col4列值为'a','b','c'中的任意一
56+
个,且vec向量字段采用'L2'类型匹配,值为'[[1.0, 2.0, 3.0],[1.1,2.1,3.1]]'。另外,采用强一致性级别在10个单元内进行检索,取第11到第15,5条命中记录。
57+
*/
58+
String sql = config.getSQL(false); //
59+
// String sql = "select id,userId,momentId,content,date from Comment where vMatch(vec, 'L2', '[[1]]') and consistencyLevel('STRONG') limit 1,1";
60+
// 使用查询器执行sql语句,并返回查询结果
61+
RecordSet recordSet = milvusQuerier.query(sql);
62+
63+
// int count = recordSet == null ? 0 : recordSet.getRecordsCount();
64+
List<Map<String, Object>> list = recordSet == null ? null : recordSet.getRecordsAsMaps();
65+
// RecordSetDefinition def = recordSet.getRecordSetDefinition();
66+
// List<ColumnDefinition> cols = def.getColumns();
67+
68+
// List<Object[]> list = count <= 0 ? null : recordSet.getRecords();
69+
70+
if (list == null || list.isEmpty()) {
71+
return new JSONObject(true);
72+
}
73+
74+
List<JSONObject> nl = new ArrayList<>(list.size());
75+
for (int i = 0; i < list.size(); i++) {
76+
Map<String, Object> map = list.get(i);
77+
78+
JSONObject obj = new JSONObject(map == null ? new HashMap<>() : map);
79+
// obj.put(col.getValue(), os[j]);
80+
// for (int j = 0; j < os.length; j++) {
81+
// ColumnDefinition col = cols.get(j);
82+
// obj.put(col.getValue(), os[j]);
83+
// }
84+
nl.add(obj);
85+
}
86+
87+
JSONObject result = nl.get(0); // JSON.parseObject(list.get(0));
88+
if (nl.size() > 1) {
89+
result.put(KEY_RAW_LIST, nl);
90+
}
91+
92+
return result;
93+
}
94+
95+
}

0 commit comments

Comments
 (0)