python 如何在pyspark DataFrame上下文中调用aes_encrypt(和其他Spark SQL函数)

xn1cxnb4  于 11个月前  发布在  Python
关注(0)|答案(2)|浏览(107)

我需要在DataFrame上下文中调用新的Spark函数aes_encrypt
该函数可以在SQL上下文中调用,如下所示:

SELECT *, aes_encrypt(col1, key, 'GCM') AS col1_encrypted FROM myTable

字符串
或者像这样:

df = sql("SELECT *, aes_encrypt(col1, key, 'GCM') AS col1_encrypted FROM myTable")


在DataFrame上下文中是否有其他方法调用它,类似这样?

from pyspark.sql.functions import aes_encrypt

df = table("myTable").withColumn("col1_encrypted", aes_encrypt("col1", key, 'GCM')


(我知道它不能被导入,因为它存在于pyspark中,这只是可以调用的其他Spark函数的一个例子)

lh80um4z

lh80um4z1#

你可以使用expr函数(doc)来实现-只需传递相应的SQL表达式:

df = table("myTable") \
  .withColumn("col1_encrypted", expr("aes_encrypt(col1, key, 'GCM')"))

字符串
selectExpr(doc):

df = table("myTable") \
  .selectExpr("*", "aes_encrypt(col1, key, 'GCM') as col1_encrypted")

im9ewurl

im9ewurl2#

data = [(
    "João",25,"Masculino",
), (
    "Maria",20,"Feminino",
), (
    "Pedro",30,"Masculino",
)]

df = spark.createDataFrame(data, schema=["nome", "idade", "sexo"])

df.show()

encryption_key = 'key'

jujuba = df.withColumn(
    'teste_encryption', expr(f"base64(aes_encrypt(nome, '{encryption_key}', 'ECB'))")
).withColumn(
    'teste_descryption', expr(f"aes_decrypt(unbase64(teste_encryption), '{encryption_key}', 'ECB')").cast(StringType())
)

字符串

相关问题