kumar :
Goutam Pradhan :
UDF:
def myudf2=(input:String)=>{
val regex = "('\\d+':'\\d+')".r
val out = new ListBuffer[String]()
input.replaceAll("'", "").split(",").map(x=>{
if(x.matches("(\\d+:\\d+)")){
val colon = x.split(":")
out += (colon(0).toInt to colon(1).toInt).mkString(", ")
} else {
out += x
}
})
out.mkString(",").replaceAll("\\[|\\]", "")
}
val df = Seq((1,"'1':'5','6','7':'10'"),(2,"'1':'6','7','8':'12'")).toDF("id","number")
scala> df.show
+---+--------------------+
| id| number|
+---+--------------------+
| 1|'1':'5','6','7':'10'|
| 2|'1':'6','7','8':'12'|
+---+--------------------+
val myCostumeudf = udf(myudf2)
scala> val outDF = df.withColumn("output", myCostumeudf(df("number")))
scala> outDF.show(5,false)
+---+--------------------+---------------------------------------+
|id |number |output |
+---+--------------------+---------------------------------------+
|1 |'1':'5','6','7':'10'|1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
|2 |'1':'6','7','8':'12'|1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 |
+---+--------------------+---------------------------------------+
Try something like above.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=366783&siteId=1