在 Spark 数据帧中解码 Base64 [英] Decode Base64 within Spark Dataframe
本文介绍了在 Spark 数据帧中解码 Base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
+----+-------------+----------+---------+
| key| value| topic|partition|
+----+-------------+----------+---------+
|null|dGVzdF90ZXh0 |Topic.Name| 0|
|null|dGVzdF90ZXh0 |Topic.Name| 0|
|null|dGVzdF90ZXh0 |Topic.Name| 0|
+----+-------------+----------+---------+
df_1
我有一个 Spark 数据框,其中 value
列是 Base64 编码的.我希望能够在数据帧的末尾添加一列,其中包含未编码的版本.
I have a Spark dataframe where the column value
is a Base64 encoded. I would like to be able to add a column at the end of the dataframe with an unencoded version of it.
import base64
df_2 = df_1.withColumn('unencoded_base64',base64.b64decode(df_1.value))
上面的代码给了我错误:
The above code give me the error:
TypeError: a2b_base64() 参数 1 必须可转换为缓冲区,不是列
TypeError: a2b_base64() argument 1 must be convertible to a buffer, not Column
推荐答案
您可以使用 unbase64 内置函数
You can use unbase64 inbuilt function for that
from pyspark.sql.functions import unbase64
df_2 = df_1.withColumn('unencoded_base64', unbase64(df_1.value))
应该给你
+----+------------+----------+---------+----------------------------+
|key |value |topic |partition|unencoded_base64 |
+----+------------+----------+---------+----------------------------+
|null|dGVzdF90ZXh0|Topic.Name|0 |[74 65 73 74 5F 74 65 78 74]|
|null|dGVzdF90ZXh0|Topic.Name|0 |[74 65 73 74 5F 74 65 78 74]|
|null|dGVzdF90ZXh0|Topic.Name|0 |[74 65 73 74 5F 74 65 78 74]|
+----+------------+----------+---------+----------------------------+
希望回答对你有帮助
这篇关于在 Spark 数据帧中解码 Base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文