ruanwenjun commented on code in PR #1770:
URL: 
https://github.com/apache/incubator-seatunnel/pull/1770#discussion_r860706820


##########
seatunnel-transforms/seatunnel-transforms-spark/seatunnel-transform-spark-uuid/src/main/scala/org/apache/seatunnel/spark/transform/Uuid.scala:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.transform
+
+import java.security.SecureRandom
+import java.util.UUID
+
+import scala.collection.JavaConversions._
+
+import com.google.common.annotations.VisibleForTesting
+import org.apache.commons.math3.random.{RandomGenerator, Well19937c}
+import org.apache.seatunnel.common.Constants
+import org.apache.seatunnel.common.config.CheckConfigUtil.checkAllExists
+import org.apache.seatunnel.common.config.CheckResult
+import org.apache.seatunnel.shade.com.typesafe.config.ConfigFactory
+import org.apache.seatunnel.spark.{BaseSparkTransform, SparkEnvironment}
+import org.apache.spark.sql.{Dataset, Row}
+import org.apache.spark.sql.expressions.UserDefinedFunction
+import org.apache.spark.sql.functions.{col, udf}
+
+class Uuid extends BaseSparkTransform {
+  private var prng: RandomGenerator = _
+
+  override def process(df: Dataset[Row], env: SparkEnvironment): Dataset[Row] 
= {
+    val key = config.getString("fields")

Review Comment:
   Please split this constant string, we don't want to receive another PR to 
solve this. And split the constant string in `Replace`.



##########
seatunnel-transforms/seatunnel-transforms-spark/seatunnel-transform-spark-uuid/src/main/scala/org/apache/seatunnel/spark/transform/Uuid.scala:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.transform
+
+import java.security.SecureRandom
+import java.util.UUID
+
+import scala.collection.JavaConversions._
+
+import com.google.common.annotations.VisibleForTesting
+import org.apache.commons.math3.random.{RandomGenerator, Well19937c}
+import org.apache.seatunnel.common.Constants
+import org.apache.seatunnel.common.config.CheckConfigUtil.checkAllExists
+import org.apache.seatunnel.common.config.CheckResult
+import org.apache.seatunnel.shade.com.typesafe.config.ConfigFactory
+import org.apache.seatunnel.spark.{BaseSparkTransform, SparkEnvironment}
+import org.apache.spark.sql.{Dataset, Row}
+import org.apache.spark.sql.expressions.UserDefinedFunction
+import org.apache.spark.sql.functions.{col, udf}
+
+class Uuid extends BaseSparkTransform {
+  private var prng: RandomGenerator = _
+
+  override def process(df: Dataset[Row], env: SparkEnvironment): Dataset[Row] 
= {
+    val key = config.getString("fields")
+
+    val func: UserDefinedFunction = udf(() => {
+      generate(config.getString("prefix"))
+    })
+    var filterDf = df.withColumn(Constants.ROW_TMP, func())
+    filterDf = filterDf.withColumn(key, col(Constants.ROW_TMP))
+    val ds = filterDf.drop(Constants.ROW_TMP)
+    if (func != null) {
+      env.getSparkSession.udf.register("Uuid", func)
+    }
+    ds
+  }
+
+  override def checkConfig(): CheckResult = {
+    checkAllExists(config, "fields")
+  }
+
+  override def prepare(env: SparkEnvironment): Unit = {
+    val defaultConfig = ConfigFactory.parseMap(Map("prefix" -> "", "secure" -> 
false))
+    config = config.withFallback(defaultConfig)
+
+    /**
+     * The secure algorithm can be comparatively slow.
+     * The new nonSecure algorithm never blocks and is much faster.
+     * The nonSecure algorithm uses a secure random seed but is otherwise 
deterministic,
+     * though it is one of the strongest uniform pseudo-random number 
generators known so far.
+     * thanks for [email protected]
+     */
+    if (config.getBoolean("secure")) {
+      val rand = new SecureRandom
+      val seed = for (_ <- 0 until 728) yield rand.nextInt
+      prng = new Well19937c(seed.toArray)
+    }
+  }
+
+  @VisibleForTesting
+  def generate(prefix: String): String = {
+    val uuid = if (prng == null) UUID.randomUUID else new UUID(prng.nextLong, 
prng.nextLong)
+    prefix + uuid
+  }
+
+  @VisibleForTesting
+  def setPrng(prng: RandomGenerator): Unit = {
+    this.prng = prng
+  }
+}

Review Comment:
   Please implement the `getPluginName()` method. And please solve this in 
`Replace` together.



##########
docs/en/transform/uuid.md:
##########
@@ -0,0 +1,62 @@
+# Uuid
+
+## Description
+
+Generate a universally unique identifier on a specified field.
+
+:::tip
+
+This transform **ONLY** supported by Spark.
+
+:::
+
+## Options
+
+| name           | type   | required | default value |
+| -------------- | ------ | -------- | ------------- |
+| fields         | string | yes      | -             |
+| prefix         | string | no       | -             |
+| secure         | boolean| no       | false         |
+
+### field [string]
+
+The name of the field to generate.
+
+### prefix [string]
+
+The prefix string constant to prepend to each generated UUID.
+
+### secure [boolean]
+
+the cryptographically secure algorithm can be comparatively slow
+The nonSecure algorithm uses a secure random seed but is otherwise 
deterministic
+
+### common options [string]
+
+Transform plugin common parameters, please refer to [Transform 
Plugin](common-options.mdx) for details
+
+## Examples
+
+```bash
+  uuid {

Review Comment:
   Maybe use `UUID` is better? Anyway there is a typo.
   ```suggestion
     UUID {
   ```



-- 
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]

Reply via email to