在spark中向Dataframe添加带有字符串索引的运行编号?

hi3rlvi2  于 2021-07-12  发布在  Spark
关注(0)|答案(1)|浏览(274)

新的Spark。是否可以向现有数据集添加一个索引列,该数据集是字符串和运行编号的组合
现在我正在用单调递增的id函数创建一个动态索引

List<Employee> columns = Arrays.asList(new Employee("john" ,"Lead"), new Employee("Doe" ,"Master"));
dataset = dataset.withColumn("index",monotonically_increasing_id());
dataset = dataset.select(col("index"),col("name"),col("desc"));

 index|name|  desc|
+-----+----+------+
|    0|john|  Lead|
|    1| Doe|Master|

希望索引列具有字符串和索引号。像下面这样

index|name|  desc|
+-----+----+------+
|   E0|john|  Lead|
|   E1| Doe|Master|
zsohkypk

zsohkypk1#

你可以用 concat 添加 E 开始时:

dataset = dataset.select(concat(lit("E"), col("index")).alias("index"),col("name"),col("desc"));

相关问题