https://github.com/leonchen83/redis-rdb-cli-api
https://github.com/leonchen83/redis-rdb-cli-api
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/leonchen83/redis-rdb-cli-api
- Owner: leonchen83
- License: apache-2.0
- Created: 2020-11-02T09:29:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-06-09T07:53:08.000Z (about 1 year ago)
- Last Synced: 2025-06-09T08:42:09.123Z (about 1 year ago)
- Language: Java
- Size: 93.8 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# redis-rdb-cli-api
This project used as API in project [redis-rdb-cli](https://github.com/leonchen83/redis-rdb-cli).
# Usage
## 1. Create a maven project with pom.xml
```java
4.0.0
com.your.company
your-service
1.0.0
UTF-8
1.8
1.8
com.moilioncircle
redis-rdb-cli-api
1.8.0
provided
com.moilioncircle
redis-replicator
[3.6.4, )
provided
org.slf4j
slf4j-api
1.7.25
provided
maven-assembly-plugin
3.1.0
jar-with-dependencies
make-assembly
package
single
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${maven.compiler.source}
${maven.compiler.target}
${project.build.sourceEncoding}
```
## 2. Implement sink service
* create `YourSinkService` implement `SinkService`
```java
public class YourSinkService implements SinkService {
@Override
public String sink() {
return "your-sink-service";
}
@Override
public void init(File config) throws IOException {
// parse your external sink config
}
@Override
public void onEvent(Replicator replicator, Event event) {
// your sink business
}
}
```
* register this service using Java SPI
```java
# create com.moilioncircle.redis.rdb.cli.api.sink.SinkService file in src/main/resources/META-INF/services/
|-src
|____main
| |____resources
| | |____META-INF
| | | |____services
| | | | |____com.moilioncircle.redis.rdb.cli.api.sink.SinkService
# add following content in com.moilioncircle.redis.rdb.cli.api.sink.SinkService
your.package.YourSinkService
```
## 3. Implement formatter service
* create `YourFormatterService` extend `AbstractFormatterService`
```java
public class YourFormatterService extends AbstractFormatterService {
@Override
public String format() {
return "test";
}
@Override
public Event applyString(Replicator replicator, RedisInputStream in, int version, byte[] key, int type, ContextKeyValuePair context) throws IOException {
byte[] val = new DefaultRdbValueVisitor(replicator).applyString(in, version);
getEscaper().encode(key, getOutputStream());
getEscaper().encode(val, getOutputStream());
getOutputStream().write('\n');
return context;
}
}
```
* register this formatter using Java SPI
```java
# create com.moilioncircle.redis.rdb.cli.api.format.FormatterService file in src/main/resources/META-INF/services/
|-src
|____main
| |____resources
| | |____META-INF
| | | |____services
| | | | |____com.moilioncircle.redis.rdb.cli.api.format.FormatterService
# add following content in com.moilioncircle.redis.rdb.cli.api.format.FormatterService
your.package.YourFormatterService
```
## 4. Package and deploy
```java
mvn clean install
cp ./target/your-service-1.0.0-jar-with-dependencies.jar /path/to/redis-rdb-cli/lib
```
## 5. Run
* run your sink service
```java
ret -s redis://127.0.0.1:6379 -c config.conf -n your-sink-service
```
* run your formatter service
```java
rct -f test -s redis://127.0.0.1:6379 -o ./out.csv -t string -d 0 -e json
```
## 6. Debug sink service
```java
public static void main(String[] args) throws Exception {
Replicator replicator = new RedisReplicator("redis://127.0.0.1:6379");
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Replicators.closeQuietly(replicator);
}));
replicator.addExceptionListener((rep, tx, e) -> {
throw new RuntimeException(tx.getMessage(), tx);
});
SinkService sink = new YourSinkService();
sink.init(new File("/path/to/your-sink.conf"));
replicator.addEventListener(new AsyncEventListener(sink, replicator, 4, Executors.defaultThreadFactory()));
replicator.open();
}
```