Skip to content

Commit d22a255

Browse files
authored
Add support for redisgraph commands. (#51)
1 parent af2277d commit d22a255

27 files changed

Lines changed: 1357 additions & 1 deletion

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Java Client libraries for [redis-modules](https://redis.io/modules), based on [R
1010
* [RedisAI](redisai)
1111
* [RedisGears](redisgears)
1212
* [RedisJSON](redisjson)
13+
* [RedisGraph](redisgraph)
1314

1415
## TODO
15-
* [RedisGraph](https://oss.redislabs.com/redisgraph/)
1616
* [RediSQL](https://redisql.com/)
1717
* [...](https://redis.io/modules)
1818

all/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@
4343
<groupId>${project.groupId}</groupId>
4444
<artifactId>redisjson</artifactId>
4545
</dependency>
46+
47+
<dependency>
48+
<groupId>${project.groupId}</groupId>
49+
<artifactId>redisgraph</artifactId>
50+
</dependency>
4651
</dependencies>
4752
</project>

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>redistimeseries</module>
1717
<module>redisgears</module>
1818
<module>redisjson</module>
19+
<module>redisgraph</module>
1920
<module>commons</module>
2021
</modules>
2122

@@ -111,6 +112,12 @@
111112
<artifactId>redisjson</artifactId>
112113
<version>${project.version}</version>
113114
</dependency>
115+
116+
<dependency>
117+
<groupId>io.github.dengliming.redismodule</groupId>
118+
<artifactId>redisgraph</artifactId>
119+
<version>${project.version}</version>
120+
</dependency>
114121
</dependencies>
115122
</dependencyManagement>
116123

redisgraph/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Java Client for RedisGraph
2+
See https://oss.redislabs.com/redisgraph/ for more details.
3+
4+
## Redis commands mapping
5+
Redis command|Sync / Async Api|
6+
| --- | --- |
7+
GRAPH.CONFIG | RedisGraph.<br/>getConfig()<br/>getConfigAsync()<br/>setConfig()<br/>setConfigAsync() |
8+
GRAPH.DELETE | RedisGraph.<br/>delete()<br/>deleteAsync()<br/> |
9+
GRAPH.EXPLAIN | RedisGraph.<br/>explain()<br/>explainAsync()<br/> |
10+
GRAPH.LIST | RedisGraph.<br/>list()<br/>listAsync()<br/> |
11+
GRAPH.PROFILE | RedisGraph.<br/>profile()<br/>profileAsync()<br/> |
12+
GRAPH.QUERY | RedisGraph.<br/>query()<br/>queryAsync()<br/> |
13+
GRAPH.RO_QUERY | RedisGraph.<br/>readOnlyQuery()<br/>readOnlyQueryAsync()<br/> |
14+
GRAPH.SLOWLOG | RedisGraph.<br/>slowLog()<br/>slowLogAsync()<br/> |
15+
16+

redisgraph/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>redis-modules-java</artifactId>
7+
<groupId>io.github.dengliming.redismodule</groupId>
8+
<version>2.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>redisgraph</artifactId>
13+
<name>RedisGraph</name>
14+
15+
<properties></properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.github.dengliming.redismodule</groupId>
20+
<artifactId>commons</artifactId>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/*
2+
* Copyright 2022 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.redisgraph;
18+
19+
import io.github.dengliming.redismodule.common.util.RAssert;
20+
import io.github.dengliming.redismodule.redisgraph.model.ResultSet;
21+
import io.github.dengliming.redismodule.redisgraph.model.SlowLogItem;
22+
import org.redisson.api.RFuture;
23+
import org.redisson.client.codec.Codec;
24+
import org.redisson.client.codec.StringCodec;
25+
import org.redisson.command.CommandAsyncExecutor;
26+
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
import static io.github.dengliming.redismodule.redisgraph.protocol.Keywords.__COMPACT;
31+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_CONFIG_GET;
32+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_CONFIG_SET;
33+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_DELETE;
34+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_EXPLAIN;
35+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_LIST;
36+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_PROFILE;
37+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_QUERY;
38+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_READ_ONLY_QUERY;
39+
import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_SLOWLOG;
40+
41+
public class RedisGraph {
42+
43+
private final CommandAsyncExecutor commandExecutor;
44+
private final Codec codec;
45+
46+
public RedisGraph(CommandAsyncExecutor commandExecutor) {
47+
this(commandExecutor, commandExecutor.getConnectionManager().getCodec());
48+
}
49+
50+
public RedisGraph(CommandAsyncExecutor commandExecutor, Codec codec) {
51+
this.commandExecutor = commandExecutor;
52+
this.codec = codec;
53+
}
54+
55+
/**
56+
* Retrieves the current value of a RedisGraph configuration parameter.
57+
* <p>
58+
* GRAPH.CONFIG GET name
59+
*
60+
* @param parameter
61+
* @return
62+
*/
63+
public Map<String, Object> getConfig(String parameter) {
64+
return commandExecutor.get(getConfigAsync(parameter));
65+
}
66+
67+
public RFuture<Map<String, Object>> getConfigAsync(String parameter) {
68+
RAssert.notEmpty(parameter, "parameter must not be empty");
69+
70+
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, GRAPH_CONFIG_GET, parameter);
71+
}
72+
73+
/**
74+
* Set the value of a RedisGraph configuration parameter.
75+
* <p>
76+
* GRAPH.CONFIG SET name value
77+
*
78+
* @param name
79+
* @param value
80+
* @return
81+
*/
82+
public Boolean setConfig(String name, Object value) {
83+
return commandExecutor.get(setConfigAsync(name, value));
84+
}
85+
86+
public RFuture<Boolean> setConfigAsync(String name, Object value) {
87+
RAssert.notEmpty(name, "name must not be empty");
88+
RAssert.notNull(value, "value must not be null");
89+
90+
return commandExecutor.readAsync(getName(), codec, GRAPH_CONFIG_SET, name, value);
91+
}
92+
93+
/**
94+
* Completely removes the graph and all of its entities.
95+
* <p>
96+
* GRAPH.DELETE graph
97+
*
98+
* @param name
99+
* @return
100+
*/
101+
public String delete(String name) {
102+
return commandExecutor.get(deleteAsync(name));
103+
}
104+
105+
public RFuture<String> deleteAsync(String name) {
106+
RAssert.notEmpty(name, "name must not be empty");
107+
108+
return commandExecutor.writeAsync(name, StringCodec.INSTANCE, GRAPH_DELETE, name);
109+
}
110+
111+
/**
112+
* Lists all graph keys in the keyspace.
113+
* <p>
114+
* GRAPH.LIST
115+
*
116+
* @return
117+
*/
118+
public List<String> list() {
119+
return commandExecutor.get(listAsync());
120+
}
121+
122+
public RFuture<List<String>> listAsync() {
123+
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, GRAPH_LIST);
124+
}
125+
126+
/**
127+
* Executes a query and produces an execution plan augmented with metrics for each operation's execution.
128+
* <p>
129+
* GRAPH.PROFILE graph query [TIMEOUT timeout]
130+
*
131+
* @return
132+
*/
133+
public List<String> profile(String graphName, String query, long timeout) {
134+
return commandExecutor.get(profileAsync(graphName, query, timeout));
135+
}
136+
137+
public RFuture<List<String>> profileAsync(String graphName, String query, long timeout) {
138+
if (timeout > 0) {
139+
return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_PROFILE, graphName, query, timeout);
140+
}
141+
return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_PROFILE, graphName, query);
142+
}
143+
144+
145+
/**
146+
* Constructs a query execution plan but does not run it. Inspect this execution plan to better understand how
147+
* your query will get executed.
148+
* <p>
149+
* GRAPH.EXPLAIN graph query
150+
*
151+
* @return
152+
*/
153+
public List<String> explain(String graphName, String query) {
154+
return commandExecutor.get(explainAsync(graphName, query));
155+
}
156+
157+
public RFuture<List<String>> explainAsync(String graphName, String query) {
158+
return commandExecutor.readAsync(graphName, codec, GRAPH_EXPLAIN, graphName, query);
159+
}
160+
161+
/**
162+
* Returns a list containing up to 10 of the slowest queries issued against the given graph ID.
163+
* <p>
164+
* GRAPH.SLOWLOG graph
165+
*
166+
* @return
167+
*/
168+
public List<SlowLogItem> slowLog(String graphName) {
169+
return commandExecutor.get(slowLogAsync(graphName));
170+
}
171+
172+
public RFuture<List<SlowLogItem>> slowLogAsync(String graphName) {
173+
return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_SLOWLOG, graphName);
174+
}
175+
176+
/**
177+
* Executes the given query against a specified graph.
178+
* <p>
179+
* GRAPH.QUERY graph query [TIMEOUT timeout]
180+
*
181+
* @return
182+
*/
183+
public ResultSet query(String graphName, String query, long timeout) {
184+
return commandExecutor.get(queryAsync(graphName, query, timeout));
185+
}
186+
187+
public RFuture<ResultSet> queryAsync(String graphName, String query, long timeout) {
188+
if (timeout > 0) {
189+
return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_QUERY, graphName, query, timeout, __COMPACT.getAlias());
190+
}
191+
return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_QUERY, graphName, query, __COMPACT.getAlias());
192+
}
193+
194+
/**
195+
* Executes a given read only query against a specified graph.
196+
* <p>
197+
* GRAPH.RO_QUERY graph query [TIMEOUT timeout]
198+
*
199+
* @return
200+
*/
201+
public ResultSet readOnlyQuery(String graphName, String query, long timeout) {
202+
return commandExecutor.get(readOnlyQueryAsync(graphName, query, timeout));
203+
}
204+
205+
public RFuture<ResultSet> readOnlyQueryAsync(String graphName, String query, long timeout) {
206+
if (timeout > 0) {
207+
return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_READ_ONLY_QUERY, graphName, query, timeout, __COMPACT.getAlias());
208+
}
209+
return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_READ_ONLY_QUERY, graphName, query, __COMPACT.getAlias());
210+
}
211+
212+
public String getName() {
213+
return "";
214+
}
215+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2022 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.redisgraph;
18+
19+
import io.github.dengliming.redismodule.common.api.RCommonBatch;
20+
import org.redisson.api.BatchOptions;
21+
import org.redisson.command.CommandAsyncExecutor;
22+
23+
public class RedisGraphBatch extends RCommonBatch {
24+
25+
public RedisGraphBatch(CommandAsyncExecutor executor, BatchOptions options) {
26+
super(executor, options);
27+
}
28+
29+
public RedisGraph getRedisGraph() {
30+
return new RedisGraph(getExecutorService());
31+
}
32+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2022 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.redisgraph.client;
18+
19+
import io.github.dengliming.redismodule.redisgraph.RedisGraph;
20+
import io.github.dengliming.redismodule.redisgraph.RedisGraphBatch;
21+
import org.redisson.Redisson;
22+
import org.redisson.api.BatchOptions;
23+
import org.redisson.client.protocol.RedisCommands;
24+
import org.redisson.command.CommandAsyncExecutor;
25+
import org.redisson.config.Config;
26+
27+
public class RedisGraphClient extends Redisson {
28+
29+
public RedisGraphClient(Config config) {
30+
super(config);
31+
}
32+
33+
public RedisGraph getRedisGraph() {
34+
return new RedisGraph(getCommandExecutor());
35+
}
36+
37+
public RedisGraphBatch createRedisGraphBatch() {
38+
return this.createRedisGraphBatch(BatchOptions.defaults());
39+
}
40+
41+
public RedisGraphBatch createRedisGraphBatch(BatchOptions options) {
42+
return new RedisGraphBatch(getCommandExecutor(), options);
43+
}
44+
45+
public Void flushall() {
46+
CommandAsyncExecutor commandExecutor = getCommandExecutor();
47+
return commandExecutor.get(commandExecutor.writeAllAsync(RedisCommands.FLUSHALL));
48+
}
49+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.dengliming.redismodule.redisgraph.enums;
2+
3+
public enum ColumnType {
4+
5+
UNKNOWN,
6+
SCALAR,
7+
NODE,
8+
RELATION;
9+
10+
public static final ColumnType[] COLUMN_TYPES = ColumnType.values();
11+
}

0 commit comments

Comments
 (0)