簡體   English   中英

如何通過 Scala 中列的哈希對數據集進行排序?

[英]How to sort a dataset by the hash of a column in Scala?

我有一個org.apache.spark.sql.Dataset並嘗試按列的散列對其進行排序。 試過了

ds.sort($"source".hashCode)

但這顯然是錯誤的。

您可以使用函數package的內置函數hash

import org.apache.spark.sql.functions.hash

ds.sort(hash($"source"))

快速示例

輸入

+--------+-----+
| source |other|
+--------+-----+
|       a|    3|
|       c|    2|
|       b|    2|
+--------+-----+

輸出:

+------+-----+
|source|other|
+------+-----+
|     c|    2|
|     a|    3|
|     b|    2|
+------+-----+

列中的散列結果僅用於演示目的:

+------+-----+-----------+
|source|other|       hash|
+------+-----+-----------+
|     c|    2|-2124386278|
|     a|    3| 1485273170|
|     b|    2| 1905031361|
+------+-----+-----------+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM