我的输入将是
Id txtyp
1 1
2. A1
3. A2
4. A3
输出应为
Id tx_typ
2. A1
3. A2
4. A3
1. 1
我尝试过在spark java中使用order by()和sort,但它不起作用。。sparkjava中还有其他方式或自定义排序吗?
您可以创建一个新的布尔列is_numeric
,当tx_typ
是数值false
时,该列的值为true
。然后,按两列is_numeric
和tx_typ
进行排序。最后,删除is_numeric
列。
翻译成代码,其中input
是你的输入Dataset<Row>
,它给你:
import static org.apache.spark.sql.functions.col;
...
input.withColumn("is_numeric", col("tx_typ").cast("int").isNotNull())
.orderBy("is_numeric", "tx_typ")
.drop("is_numeric");
具有以下行的input
数据集:
+---+------+
|Id |tx_typ|
+---+------+
|1 |1 |
|2 |A1 |
|3 |A2 |
|4 |A3 |
+---+------+
您得到以下行的输出数据集:
+---+------+
|Id |tx_typ|
+---+------+
|2 |A1 |
|3 |A2 |
|4 |A3 |
|1 |1 |
+---+------+