Skip to content

Commit 35d89ee

Browse files
committed
初始化提交
1 parent d15daa3 commit 35d89ee

File tree

5 files changed

+358
-1
lines changed

5 files changed

+358
-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

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

pom.xml

+65
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.cassandra</groupId>
7+
<artifactId>apijson-cassandra</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>apijson-cassandra</name>
12+
<description>A Cassandra 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>com.datastax.oss</groupId>
34+
<artifactId>java-driver-core</artifactId>
35+
<version>4.15.0</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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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.cassandra;
16+
17+
import apijson.JSON;
18+
import apijson.JSONResponse;
19+
import apijson.NotNull;
20+
import apijson.RequestMethod;
21+
import apijson.orm.SQLConfig;
22+
import com.alibaba.fastjson.JSONObject;
23+
import com.datastax.oss.driver.api.core.CqlSession;
24+
import com.datastax.oss.driver.api.core.cql.ResultSet;
25+
import com.datastax.oss.driver.api.core.cql.Row;
26+
27+
import java.net.MalformedURLException;
28+
import java.net.URL;
29+
import java.util.*;
30+
31+
import static apijson.orm.AbstractSQLExecutor.KEY_RAW_LIST;
32+
33+
34+
/**
35+
* @author Lemon
36+
* @see DemoSQLExecutor 重写 execute 方法:
37+
* \@Override
38+
* public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
39+
* if (config.isMilvus()) {
40+
* return CassandraUtil.execute(config, null, unknownType);
41+
* }
42+
*
43+
* return super.execute(config, unknownType);
44+
* }
45+
*/
46+
public class CassandraUtil {
47+
public static final String TAG = "CassandraUtil";
48+
49+
public static final Map<String, CqlSession> CLIENT_MAP = new LinkedHashMap<>();
50+
public static <T> CqlSession getSession(@NotNull SQLConfig<T> config) throws MalformedURLException {
51+
String uri = config.getDBUri();
52+
String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
53+
54+
CqlSession session = CLIENT_MAP.get(key);
55+
if (session == null) {
56+
session = CqlSession.builder()
57+
// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
58+
.withCloudSecureConnectBundle(new URL(config.getDBUri()))
59+
// .withAuthCredentials(config.getDBAccount(), config.getDBPassword())
60+
.withKeyspace(config.getSchema())
61+
.build();
62+
63+
CLIENT_MAP.put(key, session);
64+
}
65+
return session;
66+
}
67+
68+
public static <T> void closeSession(@NotNull SQLConfig<T> config) throws MalformedURLException {
69+
CqlSession conn = getSession(config);
70+
if (conn != null) {
71+
String uri = config.getDBUri();
72+
String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
73+
CLIENT_MAP.remove(key);
74+
75+
// try {
76+
conn.close();
77+
// }
78+
// catch (Throwable e) {
79+
// e.printStackTrace();
80+
// }
81+
}
82+
}
83+
84+
public static <T> void closeAllSession() {
85+
Collection<CqlSession> cs = CLIENT_MAP.values();
86+
for (CqlSession c : cs) {
87+
try {
88+
c.close();
89+
}
90+
catch (Throwable e) {
91+
e.printStackTrace();
92+
}
93+
}
94+
95+
CLIENT_MAP.clear();
96+
}
97+
98+
99+
public static <T> JSONObject execute(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
100+
if (RequestMethod.isQueryMethod(config.getMethod())) {
101+
return execQuery(config, sql, unknownType);
102+
}
103+
104+
return executeUpdate(config, sql);
105+
}
106+
107+
public static <T> int execUpdate(SQLConfig<T> config, String sql) throws Exception {
108+
JSONObject result = executeUpdate(config, sql);
109+
return result.getIntValue(JSONResponse.KEY_COUNT);
110+
}
111+
112+
public static <T> JSONObject executeUpdate(SQLConfig<T> config, String sql) throws Exception {
113+
return executeUpdate(null, config, sql);
114+
}
115+
public static <T> JSONObject executeUpdate(CqlSession session, SQLConfig<T> config, String sql) throws Exception {
116+
if (session == null) {
117+
session = getSession(config);
118+
}
119+
120+
if (session == null) {
121+
session = getSession(config);
122+
}
123+
124+
ResultSet rs = session.execute(sql);
125+
126+
Row row = rs.one();
127+
if (row == null) {
128+
return null;
129+
}
130+
131+
JSONObject result = new JSONObject(true);
132+
result.put(JSONResponse.KEY_COUNT, row.get(JSONResponse.KEY_COUNT, Integer.class));
133+
if (config.getId() != null) {
134+
result.put(JSONResponse.KEY_ID, config.getId());
135+
}
136+
137+
return result;
138+
}
139+
140+
141+
public static <T> JSONObject execQuery(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
142+
List<JSONObject> list = executeQuery(config, sql, unknownType);
143+
JSONObject result = list == null || list.isEmpty() ? null : list.get(0);
144+
if (result == null) {
145+
result = new JSONObject(true);
146+
}
147+
148+
if (list != null && list.size() > 1) {
149+
result.put(KEY_RAW_LIST, list);
150+
}
151+
152+
return result;
153+
}
154+
155+
public static <T> List<JSONObject> executeQuery(@NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
156+
return executeQuery(null, config, sql, unknownType);
157+
}
158+
public static <T> List<JSONObject> executeQuery(CqlSession session, @NotNull SQLConfig<T> config, String sql, boolean unknownType) throws Exception {
159+
if (session == null) {
160+
session = getSession(config);
161+
}
162+
163+
// if (config.isPrepared()) {
164+
// PreparedStatement stt = session.prepare(sql);
165+
//
166+
// List<Object> pl = config.getPreparedValueList();
167+
// if (pl != null) {
168+
// for (Object o : pl) {
169+
// stt.bind(pl.toArray());
170+
// }
171+
// }
172+
// sql = stt.getQuery();
173+
// }
174+
175+
ResultSet rs = session.execute(sql);
176+
177+
List<Row> list = rs.all();
178+
if (list == null) {
179+
return null;
180+
}
181+
182+
List<JSONObject> resultList = new ArrayList<>(list.size());
183+
for (int i = 0; i < list.size(); i++) {
184+
resultList.add(JSON.parseObject(list.get(i)));
185+
}
186+
187+
return resultList;
188+
}
189+
190+
}

0 commit comments

Comments
 (0)