RickyHuo commented on a change in pull request #907: URL: https://github.com/apache/incubator-seatunnel/pull/907#discussion_r778604560
########## File path: seatunnel-connectors/seatunnel-connector-spark-redis/src/main/scala/org/apache/seatunnel/spark/sink/Redis.scala ########## @@ -0,0 +1,130 @@ +/* + * 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.spark.sink + +import com.redislabs.provider.redis.toRedisContext +import org.apache.seatunnel.common.config.CheckResult +import org.apache.seatunnel.spark.SparkEnvironment +import org.apache.seatunnel.spark.batch.SparkBatchSink +import org.apache.spark.SparkContext +import org.apache.spark.sql.{Dataset, Row} +import org.apache.spark.internal.Logging + +import scala.collection.JavaConverters.asScalaSetConverter +import scala.collection.mutable + +class Redis extends SparkBatchSink with Logging { + + val redisConfig: mutable.Map[String, String] = mutable.Map() + val redisPrefix = "redis" + var redisSaveType: RedisSaveType.Value = _ + val HASH_NAME = "redis_hash_name" + val SET_NAME = "redis_set_name" + val ZSET_NAME = "redis_zset_name" + val LIST_NAME = "redis_list_name" + val REDIS_SAVE_TYPE = "redis_save_type" + + override def output(data: Dataset[Row], env: SparkEnvironment): Unit = { + implicit val sc: SparkContext = env.getSparkSession.sparkContext + redisSaveType match { + case RedisSaveType.KV => dealWithKV(data) + case RedisSaveType.HASH => dealWithHASH(data, redisConfig(HASH_NAME)) + case RedisSaveType.SET => dealWithSet(data, redisConfig(SET_NAME)) + case RedisSaveType.ZSET => dealWithZSet(data, redisConfig(ZSET_NAME)) + case RedisSaveType.LIST => dealWithList(data, redisConfig(LIST_NAME)) + } + } + + override def checkConfig(): CheckResult = { + config.entrySet().asScala.filter(x => x.getKey.startsWith("redis")).foreach(entry => { + if (entry.getKey.equals("redis_save_type")) { + redisSaveType = RedisSaveType.withName(config.getString("redis_save_type")) + } + redisConfig.put(entry.getKey, config.getString(entry.getKey)) + }) + + def checkParam(checkArr: Array[String]): CheckResult = { + val notExistConfig: Array[String] = checkArr.filter(checkItem => !config.hasPath(checkItem)) + if (notExistConfig.isEmpty) { + new CheckResult(true, "redis config is enough") + } else { + new CheckResult(false, s"redis config is not enough please check config [${notExistConfig.mkString(",")}]") + } + } + + val result = checkParam(Array("redis_save_type", "redis_host", "redis_port")) + if (!result.isSuccess) { + result + } else { + redisSaveType match { + case RedisSaveType.KV => checkParam(Array()) + case RedisSaveType.HASH => checkParam(Array(HASH_NAME)) + case RedisSaveType.SET => checkParam(Array(SET_NAME)) + case RedisSaveType.ZSET => checkParam(Array(ZSET_NAME)) + case RedisSaveType.LIST => checkParam(Array(LIST_NAME)) + case _ => new CheckResult(false, "unknown redis config RedisSaveType must be in [KV HASH SET ZSET LIST]") Review comment: Unknown redis config. redis_save_type must be in [KV HASH SET ZSET LIST] `RedisSaveType` would be misunderstood. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
