This is an automated email from the ASF dual-hosted git repository.

peacewong pushed a commit to branch dev-1.4.0
in repository https://gitbox.apache.org/repos/asf/linkis.git


The following commit(s) were added to refs/heads/dev-1.4.0 by this push:
     new 786751f4e spark etl support RocketMQ (#4564)
786751f4e is described below

commit 786751f4e61346e47a88399f9c3747c2509ae9f3
Author: ChengJie1053 <[email protected]>
AuthorDate: Mon May 22 12:05:53 2023 +0800

    spark etl support RocketMQ (#4564)
---
 docs/configuration/spark.md                        |  5 +-
 .../spark/datacalc/sink/RocketmqSinkConfig.java    | 66 +++++++++++++++++++
 .../datacalc/source/RocketmqSourceConfig.java      | 55 ++++++++++++++++
 .../spark/datacalc/util/PluginUtil.java            |  2 +
 .../spark/datacalc/sink/RocketmqSink.scala         | 60 +++++++++++++++++
 .../spark/datacalc/source/RocketmqSource.scala     | 44 +++++++++++++
 .../spark/datacalc/TestRocketmqCala.scala          | 75 ++++++++++++++++++++++
 7 files changed, 306 insertions(+), 1 deletion(-)

diff --git a/docs/configuration/spark.md b/docs/configuration/spark.md
index ed2024b98..f0a4723c7 100644
--- a/docs/configuration/spark.md
+++ b/docs/configuration/spark.md
@@ -30,4 +30,7 @@
 
 The spark-excel package may cause class conflicts,need to download 
separately,put it in spark lib
 wget 
https://repo1.maven.org/maven2/com/crealytics/spark-excel-2.12.17-3.2.2_2.12/3.2.2_0.18.1/spark-excel-2.12.17-3.2.2_2.12-3.2.2_0.18.1.jar
-cp spark-excel-2.12.17-3.2.2_2.12-3.2.2_0.18.1.jar 
{LINKIS_HOME}/lib/linkis-engineconn-plugins/spark/dist/3.2.1/lib
\ No newline at end of file
+cp spark-excel-2.12.17-3.2.2_2.12-3.2.2_0.18.1.jar 
{LINKIS_HOME}/lib/linkis-engineconn-plugins/spark/dist/3.2.1/lib
+
+spark3 is not supported by native rocketmq-spark, and the source code needs to 
be modified, which can be downloaded directly from the link below
+https://github.com/ChengJie1053/spark3-rocketmq-connector-jar
\ No newline at end of file
diff --git 
a/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/sink/RocketmqSinkConfig.java
 
b/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/sink/RocketmqSinkConfig.java
new file mode 100644
index 000000000..344af7ab9
--- /dev/null
+++ 
b/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/sink/RocketmqSinkConfig.java
@@ -0,0 +1,66 @@
+/*
+ * 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.linkis.engineplugin.spark.datacalc.sink;
+
+import org.apache.linkis.engineplugin.spark.datacalc.model.SinkConfig;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.Pattern;
+
+public class RocketmqSinkConfig extends SinkConfig {
+
+  @NotBlank private String nameServer;
+
+  @NotBlank private String topic;
+
+  private String checkpointLocation = "./ck";
+
+  @NotBlank
+  @Pattern(
+      regexp = "^(batch|stream)$",
+      message = "Unknown mode: {saveMode}. Accepted modes are 'batch', 
'stream'.")
+  private String mode = "stream";
+
+  public String getNameServer() {
+    return nameServer;
+  }
+
+  public String getCheckpointLocation() {
+    return checkpointLocation;
+  }
+
+  public void setCheckpointLocation(String checkpointLocation) {
+    this.checkpointLocation = checkpointLocation;
+  }
+
+  public String getMode() {
+    return mode;
+  }
+
+  public void setMode(String mode) {
+    this.mode = mode;
+  }
+
+  public String getTopic() {
+    return topic;
+  }
+
+  public void setTopic(String topic) {
+    this.topic = topic;
+  }
+}
diff --git 
a/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/source/RocketmqSourceConfig.java
 
b/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/source/RocketmqSourceConfig.java
new file mode 100644
index 000000000..83d79ec1a
--- /dev/null
+++ 
b/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/source/RocketmqSourceConfig.java
@@ -0,0 +1,55 @@
+/*
+ * 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.linkis.engineplugin.spark.datacalc.source;
+
+import org.apache.linkis.engineplugin.spark.datacalc.model.SourceConfig;
+
+import javax.validation.constraints.NotBlank;
+
+public class RocketmqSourceConfig extends SourceConfig {
+
+  @NotBlank private String nameServer;
+
+  @NotBlank private String topic;
+
+  private String consumeMode = "earliest";
+
+  public String getNameServer() {
+    return nameServer;
+  }
+
+  public void setNameServer(String nameServer) {
+    this.nameServer = nameServer;
+  }
+
+  public String getTopic() {
+    return topic;
+  }
+
+  public void setTopic(String topic) {
+    this.topic = topic;
+  }
+
+  public String getConsumeMode() {
+    return consumeMode;
+  }
+
+  public void setConsumeMode(String consumeMode) {
+    this.consumeMode = consumeMode;
+  }
+}
diff --git 
a/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/util/PluginUtil.java
 
b/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/util/PluginUtil.java
index 471d44ab5..d4628bb82 100644
--- 
a/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/util/PluginUtil.java
+++ 
b/linkis-engineconn-plugins/spark/src/main/java/org/apache/linkis/engineplugin/spark/datacalc/util/PluginUtil.java
@@ -46,6 +46,7 @@ public class PluginUtil {
     classMap.put("file", FileSource.class);
     classMap.put("redis", RedisSource.class);
     classMap.put("datalake", DataLakeSource.class);
+    classMap.put("rocketmq", RocketmqSource.class);
     return classMap;
   }
 
@@ -63,6 +64,7 @@ public class PluginUtil {
     classMap.put("file", FileSink.class);
     classMap.put("redis", RedisSink.class);
     classMap.put("datalake", DataLakeSink.class);
+    classMap.put("rocketmq", RocketmqSink.class);
     return classMap;
   }
 
diff --git 
a/linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/datacalc/sink/RocketmqSink.scala
 
b/linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/datacalc/sink/RocketmqSink.scala
new file mode 100644
index 000000000..b2a69f249
--- /dev/null
+++ 
b/linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/datacalc/sink/RocketmqSink.scala
@@ -0,0 +1,60 @@
+/*
+ * 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.linkis.engineplugin.spark.datacalc.sink
+
+import org.apache.linkis.common.utils.Logging
+import org.apache.linkis.engineplugin.spark.datacalc.api.DataCalcSink
+
+import org.apache.spark.sql.{Dataset, Row, SparkSession}
+import org.apache.spark.sql.functions.lit
+
+import scala.collection.JavaConverters._
+
+class RocketmqSink extends DataCalcSink[RocketmqSinkConfig] with Logging {
+
+  def output(spark: SparkSession, ds: Dataset[Row]): Unit = {
+    var options = Map("nameServer" -> config.getNameServer, "topic" -> 
config.getTopic)
+
+    if (config.getOptions != null && !config.getOptions.isEmpty) {
+      options = config.getOptions.asScala.toMap ++ options
+    }
+
+    logger.info(
+      s"Load data to rocketmq nameServer: ${config.getNameServer}, topic: 
${config.getTopic}"
+    )
+
+    ds.show(false)
+    config.getMode match {
+      case "batch" =>
+        ds.selectExpr("to_json(struct(*)) AS body")
+          .write
+          .format("org.apache.spark.sql.rocketmq.RocketMQSourceProvider")
+          .options(options)
+          .save()
+      case "stream" =>
+        options = Map("checkpointLocation" -> config.getCheckpointLocation) ++ 
options
+        ds.writeStream
+          .format("org.apache.spark.sql.rocketmq.RocketMQSourceProvider")
+          .options(options)
+          .start()
+          .awaitTermination()
+      case _ =>
+    }
+  }
+
+}
diff --git 
a/linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/datacalc/source/RocketmqSource.scala
 
b/linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/datacalc/source/RocketmqSource.scala
new file mode 100644
index 000000000..b24ba2ac9
--- /dev/null
+++ 
b/linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/datacalc/source/RocketmqSource.scala
@@ -0,0 +1,44 @@
+/*
+ * 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.linkis.engineplugin.spark.datacalc.source
+
+import org.apache.linkis.common.utils.Logging
+import org.apache.linkis.engineplugin.spark.datacalc.api.DataCalcSource
+
+import org.apache.spark.sql.{Dataset, Row, SparkSession}
+
+class RocketmqSource extends DataCalcSource[RocketmqSourceConfig] with Logging 
{
+
+  override def getData(spark: SparkSession): Dataset[Row] = {
+    val reader = 
spark.readStream.format("org.apache.spark.sql.rocketmq.RocketMQSourceProvider")
+    if (config.getOptions != null && !config.getOptions.isEmpty) {
+      reader.options(config.getOptions)
+    }
+
+    logger.info(
+      s"Load data from rocketmq nameServer: ${config.getNameServer}, topic: 
${config.getTopic}"
+    )
+
+    reader
+      .option("nameServer", config.getNameServer)
+      .option("topic", config.getTopic)
+      .option("startingOffsets", config.getConsumeMode)
+      .load()
+  }
+
+}
diff --git 
a/linkis-engineconn-plugins/spark/src/test/scala/org/apache/linkis/engineplugin/spark/datacalc/TestRocketmqCala.scala
 
b/linkis-engineconn-plugins/spark/src/test/scala/org/apache/linkis/engineplugin/spark/datacalc/TestRocketmqCala.scala
new file mode 100644
index 000000000..37b3ae92e
--- /dev/null
+++ 
b/linkis-engineconn-plugins/spark/src/test/scala/org/apache/linkis/engineplugin/spark/datacalc/TestRocketmqCala.scala
@@ -0,0 +1,75 @@
+/*
+ * 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.linkis.engineplugin.spark.datacalc
+
+import org.apache.linkis.common.io.FsPath
+import org.apache.linkis.engineplugin.spark.datacalc.model.DataCalcGroupData
+
+import org.apache.spark.sql.SparkSession
+
+import org.junit.jupiter.api.Test;
+
+class TestRocketmqCala {
+
+  val filePath = this.getClass.getResource("/").getFile
+
+  @Test
+  def testRocketmqReader: Unit = {
+    // skip os: windows
+    if (!FsPath.WINDOWS) {
+      val data = DataCalcGroupData.getData(rocketmqReaderConfigJson)
+      val (sources, transforms, sinks) = DataCalcExecution.getPlugins(data)
+
+      val spark = SparkSession
+        .builder()
+        .master("local")
+        .getOrCreate()
+
+      DataCalcExecution.execute(spark, sources, transforms, sinks)
+    }
+  }
+
+  val rocketmqReaderConfigJson =
+    """
+      |{
+      |    "sources": [
+      |        {
+      |            "name": "rocketmq",
+      |            "type": "source",
+      |            "config": {
+      |                "resultTable": "T1654611700631",
+      |                "nameServer": "localhost:9876",
+      |                "topic": "test11111"
+      |            }
+      |        }
+      |    ],
+      |    "sinks": [
+      |        {
+      |            "name": "rocketmq",
+      |            "config": {
+      |                "sourceTable": "T1654611700631",
+      |                "nameServer": "localhost:9876",
+      |                "mode": "stream",
+      |                "topic": "test55555"
+      |            }
+      |        }
+      |    ]
+      |}
+      |""".stripMargin
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to