简体   繁体   中英

Sort a Dataset[String] in Scala

I have dsString: Dataset[(String,Long)] (No DataFrame or Dataset[Row]) and I'm trying to order by Long .orderBy(_._2)

My problem is that .orderBy() and .sort() only accept columns, and I can only use .sortBy with RDDs.

DataFrame solution

dsString.toDF("a", "b")
  .groupBy("b")

RDD solution:

 dsString.toJavaRDD
    .sortBy(_._2)

How could Dataset[(String,Long)] do the same?

Dataset also can be applied orderBy. For example,

+---+---+
| _1| _2|
+---+---+
|  c|  3|
|  b|  5|
|  a|  4|
+---+---+

this is my Dataset and

df2.orderBy(col("_1").desc).show
df2.orderBy(col("_2").asc).show

give the results as follows:

+---+---+
| _1| _2|
+---+---+
|  c|  3|
|  b|  5|
|  a|  4|
+---+---+


+---+---+
| _1| _2|
+---+---+
|  c|  3|
|  a|  4|
|  b|  5|
+---+---+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM