Python 3.x 如何在pyspark dataframe中将字符串以外的任何数据类型转换为字符串
我试图对两个数据帧中的每一行应用pyspark sql函数哈希算法来识别差异。哈希算法是基于字符串的,所以我尝试将字符串以外的任何数据类型转换为字符串。我在日期列转换中遇到了很多问题,因为在转换为字符串之前需要更改日期格式,以使其与基于哈希的匹配保持一致。请帮助我解决方法Python 3.x 如何在pyspark dataframe中将字符串以外的任何数据类型转换为字符串,python-3.x,apache-spark,pyspark,spark-dataframe,pyspark-sql,Python 3.x,Apache Spark,Pyspark,Spark Dataframe,Pyspark Sql,我试图对两个数据帧中的每一行应用pyspark sql函数哈希算法来识别差异。哈希算法是基于字符串的,所以我尝试将字符串以外的任何数据类型转换为字符串。我在日期列转换中遇到了很多问题,因为在转换为字符串之前需要更改日期格式,以使其与基于哈希的匹配保持一致。请帮助我解决方法 #Identify the fields which are not strings from pyspark.sql.types import * fields = df_db1.schema.fields nonStrin
#Identify the fields which are not strings
from pyspark.sql.types import *
fields = df_db1.schema.fields
nonStringFields = map(lambda f: col(f.name), filter(lambda f: not isinstance(f.dataType, StringType), fields))
#Convert the date fields to specific date format and convert to string.
DateFields = map(lambda f: col(f.name), filter(lambda f: isistance(f.dataType, DateType), fields))
#convert all other fields other than string to string.
对于数字和日期字段,可以使用强制转换 以类似的方式,您可以创建长类型的列列表等,然后在答案中选择
#filter rows
DateFields = filter(lambda f: isinstance(f.dataType, DateType), fields)
# cast to string
dateFieldsWithCast = map(lambda f: col(f).cast("string").as(f.name), DateFields)