Skip to content

Commit 1065854

Browse files
committed
初始化提交
1 parent 9827b65 commit 1065854

5 files changed

Lines changed: 341 additions & 25 deletions

File tree

.gitignore

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
1-
# Compiled class file
2-
*.class
3-
4-
# Log file
5-
*.log
6-
7-
# BlueJ files
8-
*.ctxt
9-
10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
12-
13-
# Package Files #
14-
*.jar
15-
*.war
16-
*.nar
17-
*.ear
18-
*.zip
19-
*.tar.gz
20-
*.rar
21-
22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
24-
replay_pid*
1+
/target/
2+
.classpath
3+
.project
4+
.settings
5+
.idea
6+
*.iml

LICENSE

Lines changed: 1 addition & 1 deletion
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

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

pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.influxdb</groupId>
7+
<artifactId>apijson-influxdb</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>apijson-influxdb</name>
12+
<description>A InfluxDB 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.influxdb</groupId>
34+
<artifactId>influxdb-java</artifactId>
35+
<version>2.23</version>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.8.1</version>
46+
<configuration>
47+
<source>1.8</source>
48+
<target>1.8</target>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
54+
<repositories>
55+
<!-- APIJSON 必须用到的托管平台 -->
56+
<repository>
57+
<id>jitpack.io</id>
58+
<url>https://jitpack.io</url>
59+
<snapshots>
60+
<enabled>true</enabled>
61+
</snapshots>
62+
</repository>
63+
</repositories>
64+
65+
</project>
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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.influxdb;
16+
17+
import apijson.JSONResponse;
18+
import apijson.NotNull;
19+
import apijson.RequestMethod;
20+
import apijson.StringUtil;
21+
import apijson.orm.AbstractParser;
22+
import apijson.orm.SQLConfig;
23+
import com.alibaba.fastjson.JSONObject;
24+
import org.influxdb.BatchOptions;
25+
import org.influxdb.InfluxDB;
26+
import org.influxdb.InfluxDBFactory;
27+
import org.influxdb.dto.Query;
28+
import org.influxdb.dto.QueryResult;
29+
30+
import java.math.BigDecimal;
31+
import java.sql.SQLException;
32+
import java.util.*;
33+
34+
import static apijson.orm.AbstractSQLExecutor.KEY_RAW_LIST;
35+
36+
37+
/**
38+
* @author Lemon
39+
* @see DemoSQLExecutor 重写 execute 方法:
40+
* \@Override
41+
* public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
42+
* if (config.isMilvus()) {
43+
* return MilvusUtil.execute(config, null, unknownType);
44+
* }
45+
*
46+
* return super.execute(config, unknownType);
47+
* }
48+
*/
49+
public class InfluxDBUtil {
50+
public static final String TAG = "MilvusUtil";
51+
52+
public static <T> JSONObject execute(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
53+
if (RequestMethod.isQueryMethod(config.getMethod())) {
54+
List<JSONObject> list = executeQuery(config, sql, unknownType);
55+
JSONObject result = list == null || list.isEmpty() ? null : list.get(0);
56+
if (result == null) {
57+
result = new JSONObject(true);
58+
}
59+
60+
if (list != null && list.size() > 1) {
61+
result.put(KEY_RAW_LIST, list);
62+
}
63+
64+
return result;
65+
}
66+
67+
return executeUpdate(config, sql);
68+
}
69+
70+
public static <T> int execUpdate(SQLConfig<T> config, String sql) throws Exception {
71+
JSONObject result = executeUpdate(config, sql);
72+
return result.getIntValue(JSONResponse.KEY_COUNT);
73+
}
74+
75+
public static <T> JSONObject executeUpdate(SQLConfig<T> config, String sql) throws Exception {
76+
InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword());
77+
influxDB.setDatabase(config.getSchema());
78+
79+
influxDB.enableBatch(
80+
BatchOptions.DEFAULTS
81+
.threadFactory(runnable -> {
82+
Thread thread = new Thread(runnable);
83+
thread.setDaemon(true);
84+
return thread;
85+
})
86+
);
87+
88+
Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close));
89+
90+
influxDB.write(StringUtil.isEmpty(sql) ? config.getSQL(false) : sql);
91+
92+
JSONObject result = AbstractParser.newSuccessResult();
93+
94+
RequestMethod method = config.getMethod();
95+
if (method == RequestMethod.POST) {
96+
List<List<Object>> values = config.getValues();
97+
result.put(JSONResponse.KEY_COUNT, values == null ? 0 : values.size());
98+
} else {
99+
String idKey = config.getIdKey();
100+
Object id = config.getId();
101+
Object idIn = config.getIdIn();
102+
if (id != null) {
103+
result.put(idKey, id);
104+
}
105+
if (idIn != null) {
106+
result.put(idKey + "[]", idIn);
107+
}
108+
109+
if (method == RequestMethod.PUT) {
110+
Map<String, Object> content = config.getContent();
111+
result.put(JSONResponse.KEY_COUNT, content == null ? 0 : content.size());
112+
} else {
113+
result.put(JSONResponse.KEY_COUNT, id == null && idIn instanceof Collection ? ((Collection<?>) idIn).size() : 1); // FIXME 直接 SQLAuto 传 Flux/InfluxQL INSERT 如何取数量?
114+
}
115+
}
116+
117+
return result;
118+
}
119+
120+
public static <T> List<JSONObject> executeQuery(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
121+
InfluxDB influxDB = InfluxDBFactory.connect(config.getDBUri(), config.getDBAccount(), config.getDBPassword());
122+
influxDB.setDatabase(config.getSchema());
123+
QueryResult qr = influxDB.query(new Query(StringUtil.isEmpty(sql) ? config.getSQL(false) : sql));
124+
125+
String err = qr == null ? null : qr.getError();
126+
if (StringUtil.isNotEmpty(err, true)) {
127+
throw new SQLException(err);
128+
}
129+
130+
List<QueryResult.Result> list = qr == null ? null : qr.getResults();
131+
if (list == null) {
132+
return null;
133+
}
134+
135+
List<JSONObject> resultList = new ArrayList<>();
136+
137+
for (int i = 0; i < list.size(); i++) {
138+
QueryResult.Result qyrt = list.get(i);
139+
List<QueryResult.Series> seriesList = qyrt.getSeries();
140+
if (seriesList == null || seriesList.isEmpty()) {
141+
continue;
142+
}
143+
144+
for (int j = 0; j < seriesList.size(); j++) {
145+
QueryResult.Series series = seriesList.get(j);
146+
List<List<Object>> valuesList = series.getValues();
147+
if (valuesList == null || valuesList.isEmpty()) {
148+
continue;
149+
}
150+
151+
List<String> columns = series.getColumns();
152+
for (int k = 0; k < valuesList.size(); k++) {
153+
154+
List<Object> values = valuesList.get(k);
155+
JSONObject obj = new JSONObject(true);
156+
if (values != null) {
157+
for (int l = 0; l < values.size(); l++) {
158+
obj.put(columns.get(l), values.get(l));
159+
}
160+
}
161+
162+
resultList.add(obj);
163+
}
164+
}
165+
}
166+
167+
return resultList;
168+
}
169+
170+
}

0 commit comments

Comments
 (0)