Kun*_*and 13 apache-spark rdd pyspark spark-dataframe pyspark-sql
我正在处理将SQL代码转换为PySpark代码并遇到一些SQL语句.我不知道如何处理pyspark中的案例陈述?我打算创建一个RDD然后使用rdd.map然后做一些逻辑检查.这是正确的方法吗?请帮忙!
基本上我需要遍历RDD或DF中的每一行,并根据我需要编辑其中一个列值的逻辑.
case
when (e."a" Like 'a%' Or e."b" Like 'b%')
And e."aa"='BW' And cast(e."abc" as decimal(10,4))=75.0 Then 'callitA'
when (e."a" Like 'b%' Or e."b" Like 'a%')
And e."aa"='AW' And cast(e."abc" as decimal(10,4))=75.0 Then 'callitB'
else
'CallitC'
Run Code Online (Sandbox Code Playgroud)
Ram*_*ram 27
我不擅长python.但是会尝试给出我在scala中所做的一些指示.
问题:
rdd.map然后做一些逻辑检查.这是正确的方法吗?
它的一种方法.
DataFrame.withColumn pySpark中的方法支持添加新列或替换同名的现有列.
在这种情况下,你必须处理Column
via - spark udf或其他语法
from pyspark.sql import functions as F
df.select(df.name, F.when(df.age > 4, 1).when(df.age < 3, -1).otherwise(0)).show()
+-----+--------------------------------------------------------+
| name|CASE WHEN (age > 4) THEN 1 WHEN (age < 3) THEN -1 ELSE 0|
+-----+--------------------------------------------------------+
|Alice| -1|
| Bob| 1|
+-----+--------------------------------------------------------+
from pyspark.sql import functions as F
df.select(df.name, F.when(df.age > 3, 1).otherwise(0)).show()
+-----+---------------------------------+
| name|CASE WHEN (age > 3) THEN 1 ELSE 0|
+-----+---------------------------------+
|Alice| 0|
| Bob| 1|
+-----+---------------------------------+
Run Code Online (Sandbox Code Playgroud)
你可以使用udf代替when otherwise.
Sha*_*han 14
这些都是写一些方法If-Else/ When-Then-Else/ When-Otherwise表达pyspark。
样本数据框
df = spark.createDataFrame([(1,1),(2,2),(3,3)],['id','value'])
df.show()
#+---+-----+
#| id|value|
#+---+-----+
#| 1| 1|
#| 2| 2|
#| 3| 3|
#+---+-----+
#Desired Output:
#+---+-----+----------+
#| id|value|value_desc|
#+---+-----+----------+
#| 1| 1| one|
#| 2| 2| two|
#| 3| 3| other|
#+---+-----+----------+
Run Code Online (Sandbox Code Playgroud)
选项#1:withColumn()使用when-otherwise
from pyspark.sql.functions import when
df.withColumn("value_desc",when(df.value == 1, 'one').when(df.value == 2, 'two').otherwise('other')).show()
Run Code Online (Sandbox Code Playgroud)
选项2:select()使用when-otherwise
from pyspark.sql.functions import when
df.select("*",when(df.value == 1, 'one').when(df.value == 2, 'two').otherwise('other').alias('value_desc')).show()
Run Code Online (Sandbox Code Playgroud)
选项3: selectExpr()使用SQL等效的CASE表达式
df.selectExpr("*","CASE WHEN value == 1 THEN 'one' WHEN value == 2 THEN 'two' ELSE 'other' END AS value_desc").show()
Run Code Online (Sandbox Code Playgroud)
类似SQL的表达式也可以使用pyspark.sql.functions.expr函数编写withColumn()和select()使用。这是例子。
选项4: select()使用expr函数
from pyspark.sql.functions import expr
df.select("*",expr("CASE WHEN value == 1 THEN 'one' WHEN value == 2 THEN 'two' ELSE 'other' END AS value_desc")).show()
Run Code Online (Sandbox Code Playgroud)
选项5: withColumn()使用expr函数
from pyspark.sql.functions import expr
df.withColumn("value_desc",expr("CASE WHEN value == 1 THEN 'one' WHEN value == 2 THEN 'two' ELSE 'other' END AS value_desc")).show()
Run Code Online (Sandbox Code Playgroud)
输出:
#+---+-----+----------+
#| id|value|value_desc|
#+---+-----+----------+
#| 1| 1| one|
#| 2| 2| two|
#| 3| 3| other|
#+---+-----+----------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31891 次 |
| 最近记录: |