Apache spark Spark explode/posexplode列值
我是spark的新手,我想以这样的方式分解我的df,它将创建一个新列,其中包含拆分的值,并且它还具有该特定值相对于其行的顺序或索引Apache spark Spark explode/posexplode列值,apache-spark,apache-spark-sql,Apache Spark,Apache Spark Sql,我是spark的新手,我想以这样的方式分解我的df,它将创建一个新列,其中包含拆分的值,并且它还具有该特定值相对于其行的顺序或索引 CODE: import spark.implicits._ val df = Seq("40000.0~0~0~", "0~40000.0~", "0~", "1000.0~0~0~", "1333.3333333333333~0~0~0~0", "66666.66666666667~0~0~") .toDF("VALUES") df.
CODE:
import spark.implicits._
val df = Seq("40000.0~0~0~", "0~40000.0~", "0~", "1000.0~0~0~", "1333.3333333333333~0~0~0~0", "66666.66666666667~0~0~")
.toDF("VALUES")
df.show(false)
Input DF:
+--------------------------+
|VALUES |
+--------------------------+
|40000.0~0~0~ |
|0~40000.0~ |
|0~ |
|1000.0~0~0~ |
|1333.3333333333333~0~0~0~0|
|66666.66666666667~0~0~ |
+--------------------------+
Output DF:
+------+------------------+-----------+
|row_id|col |order |
+------+------------------+-----------+
|1 |40000.0 |1 |
|1 |0 |2 |
|1 |0 |3 |
|1 | |4 |<== don't want this column with empty or null value
|2 |0 |1 |
|2 |40000.0 |2 |
|2 | |3 |<== don't want this column with empty or null value
|3 |0 |1 |
|3 | |2 |<== don't want this column with empty or null value
|4 |1000.0 |1 |
|4 |0 |2 |
|4 |0 |3 |
|4 | |4 |<== don't want this column with empty or null value
|5 |1333.3333333333333|1 |
|5 |0 |2 |
|5 |0 |3 |
|5 |0 |4 |
|5 |0 |5 |
|6 |66666.66666666667 |1 |
|6 |0 |2 |
|6 |0 |3 |
|6 | |4 |<== don't want this column with empty or null value
+------+------------------+-----------+
代码:
导入spark.implicits_
val df=Seq(“40000.0~0~0~”、“0~40000.0~”、“0~”、“1000.0~0~0~”、“1333.3333333333~0~0~0”、“66666.6666667~0~0~”)
.toDF(“价值观”)
df.show(假)
输入DF:
+--------------------------+
|价值观|
+--------------------------+
|40000.0~0~0~ |
|0~40000.0~ |
|0~ |
|1000.0~0~0~ |
|1333.3333333333333~0~0~0~0|
|66666.66666666667~0~0~ |
+--------------------------+
输出DF:
+------+------------------+-----------+
|行id列顺序|
+------+------------------+-----------+
|1 |40000.0 |1 |
|1 |0 |2 |
|1 |0 |3 |
|1 | | 4 |您需要过滤空/空值。这就是你需要做的
from pyspark.sql.types import *
from pyspark.sql.functions import *
value1 = Row(VALUES='40000.0~0~0~')
value2 = Row(VALUES='0~40000.0~')
value3 = Row(VALUES='1000.0~0~0~')
value4 = Row(VALUES='333.3333333333333~0~0~0~0')
value5 = Row(VALUES='66666.66666666667~0~0~')
schema = StructType([StructField('VALUES', StringType())])
rows = [value1,value2,value3,value4,value5]
df = spark.createDataFrame(rows, schema)
df1= df.withColumn("newCol",explode(split("VALUES","~")))
df1=df1.filter(length("newCol")!=0)
df1.show(50,False)
这是输出
+-------------------------+-----------------+
|VALUES |newCol |
+-------------------------+-----------------+
|40000.0~0~0~ |40000.0 |
|40000.0~0~0~ |0 |
|40000.0~0~0~ |0 |
|0~40000.0~ |0 |
|0~40000.0~ |40000.0 |
|1000.0~0~0~ |1000.0 |
|1000.0~0~0~ |0 |
|1000.0~0~0~ |0 |
|333.3333333333333~0~0~0~0|333.3333333333333|
|333.3333333333333~0~0~0~0|0 |
|333.3333333333333~0~0~0~0|0 |
|333.3333333333333~0~0~0~0|0 |
|333.3333333333333~0~0~0~0|0 |
|66666.66666666667~0~0~ |66666.66666666667|
|66666.66666666667~0~0~ |0 |
|66666.66666666667~0~0~ |0 |
+-------------------------+-----------------+
使用窗口功能添加行id
,然后使用posexplode
和过滤器过滤掉空值
示例:
import org.apache.spark.sql.functions._
import org.apache.spark.sql.expressions._
val w=Window.orderBy(col("id"))
df.withColumn("id",monotonically_increasing_id()).
withColumn("row_id",row_number().over(w)).
selectExpr("row_id","posexplode(split(values,'~')) as (pos, val)").
withColumn("order",col("pos") + 1).
drop("pos").
filter(length(col("val")) !== 0).
show()
//or using expr
df.withColumn("id",monotonically_increasing_id()).
withColumn("row_id",row_number().over(w)).
withColumn("arr",expr("filter(split(values,'~'),x -> x != '')")).
selectExpr("row_id","""posexplode(arr) as (pos, val)""").
withColumn("order",col("pos") + 1).
drop("pos").
show()
//+------+------------------+-----+
//|row_id| val|order|
//+------+------------------+-----+
//| 1| 40000.0| 1|
//| 1| 0| 2|
//| 1| 0| 3|
//| 2| 0| 1|
//| 2| 40000.0| 2|
//| 3| 0| 1|
//| 4| 1000.0| 1|
//| 4| 0| 2|
//| 4| 0| 3|
//| 5|1333.3333333333333| 1|
//| 5| 0| 2|
//| 5| 0| 3|
//| 5| 0| 4|
//| 5| 0| 5|
//| 6| 66666.66666666667| 1|
//| 6| 0| 2|
//| 6| 0| 3|
//+------+------------------+-----+
df.withColumn("id",monotonically_increasing_id()).
withColumn("row_id",row_number().over(w)).
selectExpr("row_id","posexplode(array_remove(split(values,'~'),'')) as (pos, val)").
withColumn("order",col("pos") + 1).
drop("pos").
show()
//+------+------------------+-----+
//|row_id| val|order|
//+------+------------------+-----+
//| 1| 40000.0| 1|
//| 1| 0| 2|
//| 1| 0| 3|
//| 2| 0| 1|
//| 2| 40000.0| 2|
//| 3| 0| 1|
//| 4| 1000.0| 1|
//| 4| 0| 2|
//| 4| 0| 3|
//| 5|1333.3333333333333| 1|
//| 5| 0| 2|
//| 5| 0| 3|
//| 5| 0| 4|
//| 5| 0| 5|
//| 6| 66666.66666666667| 1|
//| 6| 0| 2|
//| 6| 0| 3|
//+------+------------------+-----+
从Spark-2.4+中,我们可以使用数组移除功能来过滤“”
import org.apache.spark.sql.functions._
import org.apache.spark.sql.expressions._
val w=Window.orderBy(col("id"))
df.withColumn("id",monotonically_increasing_id()).
withColumn("row_id",row_number().over(w)).
selectExpr("row_id","posexplode(split(values,'~')) as (pos, val)").
withColumn("order",col("pos") + 1).
drop("pos").
filter(length(col("val")) !== 0).
show()
//or using expr
df.withColumn("id",monotonically_increasing_id()).
withColumn("row_id",row_number().over(w)).
withColumn("arr",expr("filter(split(values,'~'),x -> x != '')")).
selectExpr("row_id","""posexplode(arr) as (pos, val)""").
withColumn("order",col("pos") + 1).
drop("pos").
show()
//+------+------------------+-----+
//|row_id| val|order|
//+------+------------------+-----+
//| 1| 40000.0| 1|
//| 1| 0| 2|
//| 1| 0| 3|
//| 2| 0| 1|
//| 2| 40000.0| 2|
//| 3| 0| 1|
//| 4| 1000.0| 1|
//| 4| 0| 2|
//| 4| 0| 3|
//| 5|1333.3333333333333| 1|
//| 5| 0| 2|
//| 5| 0| 3|
//| 5| 0| 4|
//| 5| 0| 5|
//| 6| 66666.66666666667| 1|
//| 6| 0| 2|
//| 6| 0| 3|
//+------+------------------+-----+
df.withColumn("id",monotonically_increasing_id()).
withColumn("row_id",row_number().over(w)).
selectExpr("row_id","posexplode(array_remove(split(values,'~'),'')) as (pos, val)").
withColumn("order",col("pos") + 1).
drop("pos").
show()
//+------+------------------+-----+
//|row_id| val|order|
//+------+------------------+-----+
//| 1| 40000.0| 1|
//| 1| 0| 2|
//| 1| 0| 3|
//| 2| 0| 1|
//| 2| 40000.0| 2|
//| 3| 0| 1|
//| 4| 1000.0| 1|
//| 4| 0| 2|
//| 4| 0| 3|
//| 5|1333.3333333333333| 1|
//| 5| 0| 2|
//| 5| 0| 3|
//| 5| 0| 4|
//| 5| 0| 5|
//| 6| 66666.66666666667| 1|
//| 6| 0| 2|
//| 6| 0| 3|
//+------+------------------+-----+