您可以使用StringType,因为它返回的是 JSON 字符串,而不是字符串数组。您还可以使用json.dumps 将字典转换为 JSON 字符串。
import pyspark.sql.functions as F
from pyspark.sql.types import StringType
import json
def test(test1,test2):
d = [{'amount': a, 'discount': t} for a, t in zip(test1, test2)]
return json.dumps(d)
arrayToMapUDF = F.udf(test, StringType())
df2 = df.withColumn("jsonarraycolumn", arrayToMapUDF(F.col("amount"), F.col("discount")))
df2.show(truncate=False)
+-------------------------------+------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|amount |discount |jsonarraycolumn |
+-------------------------------+------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|[1000, 15000, 2000, 3000, 4000]|[0.01, 0.02, 0.03, 0.04, 0.05]|[{"amount": 1000, "discount": 0.01}, {"amount": 15000, "discount": 0.02}, {"amount": 2000, "discount": 0.03}, {"amount": 3000, "discount": 0.04}, {"amount": 4000, "discount": 0.05}]|
+-------------------------------+------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
如果你不想要引号,
import pyspark.sql.functions as F
from pyspark.sql.types import StringType
import json
def test(test1,test2):
d = [{'amount': a, 'discount': t} for a, t in zip(test1, test2)]
return json.dumps(d).replace('"', '')
arrayToMapUDF = F.udf(test, StringType())
df2 = df.withColumn("jsonarraycolumn", arrayToMapUDF(F.col("amount"), F.col("discount")))
df2.show(truncate=False)
+-------------------------------+------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
|amount |discount |jsonarraycolumn |
+-------------------------------+------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
|[1000, 15000, 2000, 3000, 4000]|[0.01, 0.02, 0.03, 0.04, 0.05]|[{amount: 1000, discount: 0.01}, {amount: 15000, discount: 0.02}, {amount: 2000, discount: 0.03}, {amount: 3000, discount: 0.04}, {amount: 4000, discount: 0.05}]|
+-------------------------------+------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
如果你想要一个真正的 JSON 类型的列:
def test(test1,test2):
d = [{'amount': a, 'discount': t} for a, t in zip(test1, test2)]
return d
arrayToMapUDF = F.udf(test,
ArrayType(
StructType([
StructField('amount', StringType()),
StructField('discount', StringType())
])
)
)
df2 = df.withColumn("jsonarraycolumn", arrayToMapUDF(F.col("amount"), F.col("discount")))
df2.show(truncate=False)
+-------------------------------+------------------------------+-----------------------------------------------------------------------+
|amount |discount |jsonarraycolumn |
+-------------------------------+------------------------------+-----------------------------------------------------------------------+
|[1000, 15000, 2000, 3000, 4000]|[0.01, 0.02, 0.03, 0.04, 0.05]|[[1000, 0.01], [15000, 0.02], [2000, 0.03], [3000, 0.04], [4000, 0.05]]|
+-------------------------------+------------------------------+-----------------------------------------------------------------------+
df2.printSchema()
root
|-- amount: array (nullable = false)
| |-- element: integer (containsNull = false)
|-- discount: array (nullable = false)
| |-- element: double (containsNull = false)
|-- jsonarraycolumn: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- amount: string (nullable = true)
| | |-- discount: string (nullable = true)