如果你只想在scala中生成数据,试试这种方式。
val r = new scala.util.Random //create scala random object
val new_val = r.nextFloat() // for generating next random float between 0 to 1 for every call
并将这个 new_val 添加到数据中纬度的最大值。无论如何,独特的纬度使配对独一无二。
您可以使用带有 Scala 的 Spark 尝试此选项。
val latLongDF = ss.read.option("header", true).option("delimiter", ",").format("csv").load(mypath) // loaded your sample data in your question as Dataframe
+---------+----------+----+-----+
| latitude| longitude|step|count|
+---------+----------+----+-----+
|25.696395|-80.297496| 1| 1|
|25.699544|-80.297055| 1| 1|
|25.698612|-80.292015| 1| 1|
val max_lat = latLongDF.select(max("latitude")).first.get(0).toString().toDouble // got max latitude value
val r = new scala.util.Random // scala random object to get random numbers
val nextLat = udf(() => (28.355484 + 0.000001 + r.nextFloat()).toFloat) // udf to give random latitude more than the existing maximum latitude
在上面的行toFloat
中,浮动可能会导致重复值。如果您可以在纬度中使用更多十进制值(超过 6 个),请删除它以获得完整的随机值。或者在经度上使用相同的方法也可以获得更好的唯一性。
val new_df = latLongDF.withColumn("new_lat", nextLat()).select(col("new_lat").alias("latitude"),$"longitude",$"step",$"count").union(latLongDF) // creating new dataframe and Union with existing dataframe
新生成的数据样本。
+----------+----------+----+-----+
|latitude| longitude|step|count|
+----------+----------+----+-----+
| 28.446129|-80.297496| 1| 1|
| 28.494934|-80.297055| 1| 1|
| 28.605234|-80.292015| 1| 1|
| 28.866316|-80.341607| 1| 1|