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

[Feature][Connector-V2] Support TableSourceFactory/TableSinkFactory on redis #5901

Merged
merged 9 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
redis source and sink
  • Loading branch information
q3356564 committed Nov 22, 2023
commit b643e36d768c1cca437baadf1a92b5c9f1249599
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public HttpSink(Config pluginConfig, SeaTunnelRowType rowType) {

@Override
public String getPluginName() {
return "Http";
return HttpConfig.CONNECTOR_IDENTITY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

public class RedisConfig {

public static final String CONNECTOR_IDENTITY = "Redis";

public enum RedisMode {
SINGLE,
CLUSTER;
Expand Down Expand Up @@ -81,16 +83,16 @@ public enum HashKeyParseMode {
.noDefaultValue()
.withDescription("redis data types, support key hash list set zset.");

public static final Option<RedisConfig.Format> FORMAT =
public static final Option<Format> FORMAT =
Options.key("format")
.enumType(RedisConfig.Format.class)
.defaultValue(RedisConfig.Format.JSON)
.enumType(Format.class)
.defaultValue(Format.JSON)
.withDescription(
"the format of upstream data, now only support json and text, default json.");

public static final Option<RedisConfig.RedisMode> MODE =
public static final Option<RedisMode> MODE =
Options.key("mode")
.enumType(RedisConfig.RedisMode.class)
.enumType(RedisMode.class)
.defaultValue(RedisMode.SINGLE)
.withDescription(
"redis mode, support single or cluster, default value is single");
Expand All @@ -102,9 +104,9 @@ public enum HashKeyParseMode {
.withDescription(
"redis nodes information, used in cluster mode, must like as the following format: [host1:port1, host2:port2]");

public static final Option<RedisConfig.HashKeyParseMode> HASH_KEY_PARSE_MODE =
public static final Option<HashKeyParseMode> HASH_KEY_PARSE_MODE =
Options.key("hash_key_parse_mode")
.enumType(RedisConfig.HashKeyParseMode.class)
.enumType(HashKeyParseMode.class)
.defaultValue(HashKeyParseMode.ALL)
.withDescription(
"hash key parse mode, support all or kv, default value is all");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

import org.apache.seatunnel.shade.com.typesafe.config.Config;

import org.apache.seatunnel.api.common.PrepareFailException;
import org.apache.seatunnel.api.common.SeaTunnelAPIErrorCode;
import org.apache.seatunnel.api.sink.SeaTunnelSink;
import org.apache.seatunnel.api.sink.SinkWriter;
import org.apache.seatunnel.api.table.catalog.CatalogTable;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
import org.apache.seatunnel.common.config.CheckConfigUtil;
Expand All @@ -34,24 +33,17 @@
import org.apache.seatunnel.connectors.seatunnel.redis.config.RedisParameters;
import org.apache.seatunnel.connectors.seatunnel.redis.exception.RedisConnectorException;

import com.google.auto.service.AutoService;

import java.io.IOException;

@AutoService(SeaTunnelSink.class)
public class RedisSink extends AbstractSimpleSink<SeaTunnelRow, Void> {
private final RedisParameters redisParameters = new RedisParameters();
private SeaTunnelRowType seaTunnelRowType;
private Config pluginConfig;
private CatalogTable catalogTable;

@Override
public String getPluginName() {
return "Redis";
}

@Override
public void prepare(Config pluginConfig) throws PrepareFailException {
this.pluginConfig = pluginConfig;
public RedisSink(Config config, CatalogTable table) {
this.pluginConfig = config;
this.catalogTable = table;
CheckResult result =
CheckConfigUtil.checkAllExists(
pluginConfig,
Expand All @@ -67,11 +59,12 @@ public void prepare(Config pluginConfig) throws PrepareFailException {
getPluginName(), PluginType.SINK, result.getMsg()));
}
this.redisParameters.buildWithConfig(pluginConfig);
this.seaTunnelRowType = catalogTable.getSeaTunnelRowType();
}

@Override
public void setTypeInfo(SeaTunnelRowType seaTunnelRowType) {
this.seaTunnelRowType = seaTunnelRowType;
public String getPluginName() {
return RedisConfig.CONNECTOR_IDENTITY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
package org.apache.seatunnel.connectors.seatunnel.redis.sink;

import org.apache.seatunnel.api.configuration.util.OptionRule;
import org.apache.seatunnel.api.table.catalog.CatalogTable;
import org.apache.seatunnel.api.table.connector.TableSink;
import org.apache.seatunnel.api.table.factory.Factory;
import org.apache.seatunnel.api.table.factory.TableSinkFactory;
import org.apache.seatunnel.api.table.factory.TableSinkFactoryContext;
import org.apache.seatunnel.connectors.seatunnel.redis.config.RedisConfig;

import com.google.auto.service.AutoService;
Expand All @@ -31,6 +34,12 @@ public String factoryIdentifier() {
return "Redis";
}

@Override
public TableSink createSink(TableSinkFactoryContext context) {
CatalogTable catalogTable = context.getCatalogTable();
return () -> new RedisSink(context.getOptions().toConfig(), catalogTable);
}

@Override
public OptionRule optionRule() {
return OptionRule.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@

import org.apache.seatunnel.shade.com.typesafe.config.Config;

import org.apache.seatunnel.api.common.PrepareFailException;
import org.apache.seatunnel.api.common.SeaTunnelAPIErrorCode;
import org.apache.seatunnel.api.serialization.DeserializationSchema;
import org.apache.seatunnel.api.source.Boundedness;
import org.apache.seatunnel.api.source.SeaTunnelSource;
import org.apache.seatunnel.api.table.catalog.CatalogTable;
import org.apache.seatunnel.api.table.catalog.CatalogTableUtil;
import org.apache.seatunnel.api.table.catalog.PhysicalColumn;
import org.apache.seatunnel.api.table.catalog.TableIdentifier;
import org.apache.seatunnel.api.table.catalog.TableSchema;
import org.apache.seatunnel.api.table.catalog.schema.TableSchemaOptions;
import org.apache.seatunnel.api.table.type.BasicType;
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
Expand All @@ -40,21 +43,24 @@
import org.apache.seatunnel.connectors.seatunnel.redis.exception.RedisConnectorException;
import org.apache.seatunnel.format.json.JsonDeserializationSchema;

import com.google.auto.service.AutoService;
import com.google.common.collect.Lists;

import java.util.Collections;
import java.util.List;

@AutoService(SeaTunnelSource.class)
public class RedisSource extends AbstractSingleSplitSource<SeaTunnelRow> {
private final RedisParameters redisParameters = new RedisParameters();
private SeaTunnelRowType seaTunnelRowType;
private DeserializationSchema<SeaTunnelRow> deserializationSchema;

private CatalogTable catalogTable;

@Override
public String getPluginName() {
return "Redis";
return RedisConfig.CONNECTOR_IDENTITY;
}

@Override
public void prepare(Config pluginConfig) throws PrepareFailException {
public RedisSource(Config pluginConfig) {
Copy link
Member

Choose a reason for hiding this comment

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

Please use ReadOnlyConfig, do not convert ReadOnlyConfig to Config. This is a bad example, we want connectors to be able to use ReadOnlyConfig instead of Config

CheckResult result =
CheckConfigUtil.checkAllExists(
pluginConfig,
Expand Down Expand Up @@ -87,13 +93,38 @@ public void prepare(Config pluginConfig) throws PrepareFailException {
RedisConfig.Format.valueOf(
pluginConfig.getString(RedisConfig.FORMAT.key()).toUpperCase());
if (RedisConfig.Format.JSON.equals(format)) {
this.seaTunnelRowType =
CatalogTableUtil.buildWithConfig(pluginConfig).getSeaTunnelRowType();
this.catalogTable = CatalogTableUtil.buildWithConfig(pluginConfig);
this.seaTunnelRowType = catalogTable.getSeaTunnelRowType();
this.deserializationSchema =
new JsonDeserializationSchema(false, false, seaTunnelRowType);
}
} else {
this.seaTunnelRowType = CatalogTableUtil.buildSimpleTextSchema();
TableIdentifier tableIdentifier =
TableIdentifier.of(RedisConfig.CONNECTOR_IDENTITY, null, null);
TableSchema tableSchema =
TableSchema.builder()
.column(
PhysicalColumn.of(
"content",
new SeaTunnelRowType(
new String[] {"content"},
new SeaTunnelDataType<?>[] {
BasicType.STRING_TYPE
}),
0,
false,
null,
null))
.build();

this.catalogTable =
CatalogTable.of(
tableIdentifier,
tableSchema,
Collections.emptyMap(),
Collections.emptyList(),
null);
this.seaTunnelRowType = catalogTable.getSeaTunnelRowType();
this.deserializationSchema = null;
}
}
Expand All @@ -104,8 +135,8 @@ public Boundedness getBoundedness() {
}

@Override
public SeaTunnelDataType<SeaTunnelRow> getProducedType() {
return seaTunnelRowType;
public List<CatalogTable> getProducedCatalogTables() {
return Lists.newArrayList(catalogTable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,33 @@

import org.apache.seatunnel.api.configuration.util.OptionRule;
import org.apache.seatunnel.api.source.SeaTunnelSource;
import org.apache.seatunnel.api.source.SourceSplit;
import org.apache.seatunnel.api.table.catalog.schema.TableSchemaOptions;
import org.apache.seatunnel.api.table.connector.TableSource;
import org.apache.seatunnel.api.table.factory.Factory;
import org.apache.seatunnel.api.table.factory.TableSourceFactory;
import org.apache.seatunnel.api.table.factory.TableSourceFactoryContext;
import org.apache.seatunnel.connectors.seatunnel.redis.config.RedisConfig;

import com.google.auto.service.AutoService;

import java.io.Serializable;

@AutoService(Factory.class)
public class RedisSourceFactory implements TableSourceFactory {
@Override
public String factoryIdentifier() {
return "Redis";
}

@Override
public <T, SplitT extends SourceSplit, StateT extends Serializable>
TableSource<T, SplitT, StateT> createSource(TableSourceFactoryContext context) {
return () ->
(SeaTunnelSource<T, SplitT, StateT>)
new RedisSource(context.getOptions().toConfig());
}

@Override
public OptionRule optionRule() {
return OptionRule.builder()
Expand Down