Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve][Connector-v2] Redis support select db #5570

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/en/connector-v2/sink/Redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Used to write data to Redis.
| data_type | string | yes | - |
| user | string | no | - |
| auth | string | no | - |
| db_num | int | no | - |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| db_num | int | no | - |
| db_num | int | no | 0 |

| mode | string | no | single |
| nodes | list | yes when mode=cluster | - |
| format | string | no | json |
Expand Down Expand Up @@ -91,6 +92,10 @@ redis authentication user, you need it when you connect to an encrypted cluster

Redis authentication password, you need it when you connect to an encrypted cluster

### db_num [int]

Redis database index ID. It is connected to db 0 by default

### mode [string]

redis mode, `single` or `cluster`, default is `single`
Expand Down
5 changes: 5 additions & 0 deletions docs/en/connector-v2/source/Redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Used to read data from Redis.
| data_type | string | yes | - |
| user | string | no | - |
| auth | string | no | - |
| db_num | int | no | - |
| mode | string | no | single |
| hash_key_parse_mode | string | no | all |
| nodes | list | yes when mode=cluster | - |
Expand Down Expand Up @@ -151,6 +152,10 @@ redis authentication user, you need it when you connect to an encrypted cluster

redis authentication password, you need it when you connect to an encrypted cluster

### db_num [int]

Redis database index ID. It is connected to db 0 by default

### mode [string]

redis mode, `single` or `cluster`, default is `single`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public enum HashKeyParseMode {
.withDescription(
"redis authentication password, you need it when you connect to an encrypted cluster");

public static final Option<Integer> DB_NUM =
Options.key("db_num")
.intType()
.defaultValue(0)
.withDescription(
"redis database index id, It is connected to db 0 by default");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"redis database index id, It is connected to db 0 by default");
"Redis database index id, it is connected to db 0 by default");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


public static final Option<String> USER =
Options.key("user")
.stringType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class RedisParameters implements Serializable {
private String host;
private int port;
private String auth = "";
private Integer dbNum;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private Integer dbNum;
private int dbNum;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

private String user = "";
private String keysPattern;
private String keyField;
Expand All @@ -58,6 +59,10 @@ public void buildWithConfig(Config config) {
if (config.hasPath(RedisConfig.AUTH.key())) {
this.auth = config.getString(RedisConfig.AUTH.key());
}
// set db_num
if (config.hasPath(RedisConfig.DB_NUM.key())) {
this.dbNum = config.getInt(RedisConfig.DB_NUM.key());
}
// set user
if (config.hasPath(RedisConfig.USER.key())) {
this.user = config.getString(RedisConfig.USER.key());
Expand Down Expand Up @@ -115,6 +120,9 @@ public Jedis buildJedis() {
if (StringUtils.isNotBlank(user)) {
jedis.aclSetUser(user);
}
if (dbNum != null && dbNum > 0) {
jedis.select(dbNum);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (dbNum != null && dbNum > 0) {
jedis.select(dbNum);
}
jedis.select(dbNum);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return jedis;
case CLUSTER:
HashSet<HostAndPort> nodes = new HashSet<>();
Expand Down Expand Up @@ -148,7 +156,11 @@ public Jedis buildJedis() {
} else {
jedisCluster = new JedisCluster(nodes);
}
return new JedisWrapper(jedisCluster);
JedisWrapper jedisWrapper = new JedisWrapper(jedisCluster);
if (dbNum != null && dbNum > 0) {
jedisWrapper.select(dbNum);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above.

}
return jedisWrapper;
default:
// do nothing
throw new RedisConnectorException(
Expand Down