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-2582][Connector] Add Hashes && Lists in Redis Sink #2587

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion docs/en/connector/sink/Redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Redis timeout

### data_type [string]

Redis data type eg: `KV HASH LIST SET ZSET`
Redis data type eg: `KV HASH LIST SET ZSET HASHES LISTS`

### hash_name [string]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ object RedisDataType extends Enumeration {

val KV: Value = Value("KV")
val HASH: Value = Value("HASH")
val HASHES: Value = Value("HASHES")
val LIST: Value = Value("LIST")
val LISTS: Value = Value("LISTS")
val SET: Value = Value("SET")
val ZSET: Value = Value("ZSET")
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ class Redis extends SparkBatchSink with Logging {
case RedisDataType.SET => dealWithSet(data, config.getString(SET_NAME), config.getInt(TTL))(sc = sc, redisConfig = redisConfigs)
case RedisDataType.ZSET => dealWithZSet(data, config.getString(ZSET_NAME), config.getInt(TTL))(sc = sc, redisConfig = redisConfigs)
case RedisDataType.LIST => dealWithList(data, config.getString(LIST_NAME), config.getInt(TTL))(sc = sc, redisConfig = redisConfigs)
case RedisDataType.HASHES => dealWithHASHes(data, config.getInt(TTL))(sc = sc, redisConfig = redisConfigs)
case RedisDataType.LISTS => dealWithLists(data, config.getInt(TTL))(sc = sc, redisConfig = redisConfigs)
}
}

override def checkConfig(): CheckResult = {
if (config.hasPath(DATA_TYPE)) {
val dataType = config.getString(DATA_TYPE)
val dataTypeList = List("KV", "HASH", "SET", "ZSET", "LIST")
val dataTypeList = List("KV", "HASH", "SET", "ZSET", "LIST", "HASHES", "LISTS")
val bool = dataTypeList.contains(dataType.toUpperCase)
if (!bool) {
CheckResult.error("Unknown redis config. data_type must be in [KV HASH SET ZSET LIST]")
CheckResult.error("Unknown redis config. data_type must be in [KV HASH SET ZSET LIST HASHES LISTS]")
} else {
CheckResult.success()
}
Expand Down Expand Up @@ -106,5 +108,15 @@ class Redis extends SparkBatchSink with Logging {
sc.toRedisHASH(value, hashName, ttl)(redisConfig = redisConfig)
}

def dealWithHASHes(data: Dataset[Row], ttl: Int)(implicit sc: SparkContext, redisConfig: RedisConfig): Unit = {
val value = data.rdd.map(x => (x.getString(0), Map(x.getString(1) -> x.getString(2))))
sc.toRedisHASHes(value, ttl)(redisConfig = redisConfig)
}

def dealWithLists(data: Dataset[Row], ttl: Int)(implicit sc: SparkContext, redisConfig: RedisConfig): Unit = {
val value = data.rdd.map(x => (x.getString(0), scala.Seq(x.getString(1))))
sc.toRedisLISTs(value, ttl)(redisConfig = redisConfig)
}

override def getPluginName: String = "Redis"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.e2e.spark.redis;

import org.apache.seatunnel.e2e.spark.SparkContainer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Container;

import java.io.IOException;

public class FakeSourceToRedisIT extends SparkContainer {

@Test
public void testFakeSourceToRedisSink() throws IOException, InterruptedException {
Container.ExecResult execResult = executeSeaTunnelSparkJob("/jdbc/fakesource_to_redis.conf");
Copy link
Member

Choose a reason for hiding this comment

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

Assertions.assertEquals(0, execResult.getExitCode());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
######
###### This config file is a demonstration of streaming processing in seatunnel config
######

env {
spark.app.name = "SeaTunnel"
spark.executor.instances = 2
spark.executor.cores = 1
spark.executor.memory = "1g"
spark.master = local
}

source {
# This is a example source plugin **only for test and demonstrate the feature source plugin**
Fake {
result_table_name = "my_dataset"
schema = {
fields {
key = "string"
hash_key = "string"
item = "string"
}
}
}

# If you would like to get more information about how to configure seatunnel and see full list of source plugins,
# please go to https://seatunnel.apache.org/docs/flink/configuration/source-plugins/Fake
}

transform {
# If you would like to get more information about how to configure seatunnel and see full list of transform plugins,
# please go to https://seatunnel.apache.org/docs/flink/configuration/transform-plugins/Sql
}

sink {
redis {
host = "localhost"
Copy link
Member

Choose a reason for hiding this comment

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

Connect to redis-server you started in testcase?

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
host = "localhost"
host = "spark_e2e_redis"

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
host = "localhost"
host = "spark_e2e_redis"

port = 6379
data_type = "HASHES"
db_num = 0
}

# If you would like to get more information about how to configure seatunnel and see full list of sink plugins,
# please go to https://seatunnel.apache.org/docs/flink/configuration/sink-plugins/Console
}