用于从Dataframe中查找所有间隔重叠的spark

fquxozlt  于 2021-07-14  发布在  Spark
关注(0)|答案(1)|浏览(314)

我有一个由两列组成的间隔Dataframe:“from”,“to”。
例如:
至1016189151114
我想使用spark有效地获得一个Dataframe,该Dataframe由基于输入的重叠级别的间隔组成。
对于提供的输入:
至标高1919102101111114214.15116181

gkn4icbw

gkn4icbw1#

将列值放入列表中,对其进行排序,然后转换为对,再转换为Dataframe。
我不知道是什么 level 是。

scala> def toPairs[A](xs: Seq[A]): Seq[(A,A)] = xs.zip(xs.tail)
toPairs: [A](xs: Seq[A])Seq[(A, A)]

scala> val df = Seq((1, 10),(16, 18), (9, 15), (11, 14)).toDF("from", "to")
df: org.apache.spark.sql.DataFrame = [from: int, to: int]

scala> val col1 = df.select($"from").rdd.map(r => r(0).asInstanceOf[Integer]).collect()
col1: Array[Integer] = Array(1, 16, 9, 11)

scala> val col2 = df.select($"to").rdd.map(r => r(0).asInstanceOf[Integer]).collect()
col2: Array[Integer] = Array(10, 18, 15, 14)

scala> toPairs((col1 ++ col2).sorted).toDF("from", "to").show
+----+---+
|from| to|
+----+---+
|   1|  9|
|   9| 10|
|  10| 11|
|  11| 14|
|  14| 15|
|  15| 16|
|  16| 18|
+----+---+

相关问题