wuchunfu commented on a change in pull request #1059: URL: https://github.com/apache/incubator-seatunnel/pull/1059#discussion_r791240613
########## File path: seatunnel-connectors/seatunnel-connector-spark-cassandra/src/main/scala/org/apache/seatunnel/spark/sink/Cassandra.scala ########## @@ -0,0 +1,69 @@ +/* + * 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 org.apache.seatunnel.common.config.CheckConfigUtil.check +import org.apache.seatunnel.common.config.{CheckResult, TypesafeConfigUtils} +import org.apache.seatunnel.shade.com.typesafe.config.ConfigFactory +import org.apache.seatunnel.spark.SparkEnvironment +import org.apache.seatunnel.spark.batch.SparkBatchSink +import org.apache.spark.sql.{Dataset, Row} + +import scala.collection.JavaConversions._ +import scala.util.{Failure, Success, Try} + +class Cassandra extends SparkBatchSink { + + override def output(data: Dataset[Row], env: SparkEnvironment): Unit = { + val writer = data.write + .format("org.apache.spark.sql.cassandra") + .mode(config.getString("save.mode")) + .options(Map( + "table" -> config.getString("table"), + "keyspace" -> config.getString("keyspace"), + "cluster" -> config.getString("cluster"), + "confirm.truncate" -> config.getString("confirm.truncate"))) + + Try(TypesafeConfigUtils.extractSubConfigThrowable(config, "options.", false)) match { + + case Success(options) => + val optionMap = options + .entrySet() + .foldRight(Map[String, String]())((entry, m) => { + m + (entry.getKey -> entry.getValue.unwrapped().toString) + }) + + writer.options(optionMap) + case Failure(exception) => // do nothing + } + + writer.save() + } + + override def checkConfig(): CheckResult = { + check(config, "table", "keyspace") + } Review comment: We have now changed `check` to `checkAllExists` , please check, thanks ########## File path: docs/en/spark/configuration/sink-plugins/Cassandra.md ########## @@ -0,0 +1,59 @@ +# Sink plugin: Cassandra + +### Description + +Write data into Cassandra. + +### Env +| name | type | required | default value | +| -------------- | ------ | -------- | ------------- | +| [spark.cassandra.connection.host](#spark.cassandra.connection.host-string) | string | yes | - | + +##### spark.cassandra.connection.host [string] + +Cassandra connection host + +##### other + +Refer to [spark-cassandra-connector-options](https://github.com/datastax/spark-cassandra-connector/blob/b2.4/doc/reference.md) for configurations. + +### Options + +| name | type | required | default value | +| -------------- | ------ | -------- | ------------- | +| [table](#table-string) | string | yes | - | +| [keyspace](#keyspace-string) | string | yes | - | +| [cluster](#cluster-string) | string | no | default | +| [confirm.truncate](#confirm.truncate-string) | string | no | false | +| [save.mode](#save.mode-string) | string | no | append | + +##### table [string] + +The Cassandra table to connect to + +##### keyspace [string] + +The keyspace where table is looked for + +##### cluster [string] + +The group of the Cluster Level Settings to inherit + +##### confirm.truncate [string] + +Confirm to truncate table when use Save.overwrite mode + +##### save.mode [string] + +Save mode + +### Example + +```bash +cassandra { + table = "t2" + keyspace = "excelsior" + result_table_name = "test" +} Review comment: `sink` does not need `result_table_name` , please check, thanks -- 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]
