Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在pyspark dataframe中将字符串以外的任何数据类型转换为字符串_Python 3.x_Apache Spark_Pyspark_Spark Dataframe_Pyspark Sql - Fatal编程技术网

Python 3.x 如何在pyspark dataframe中将字符串以外的任何数据类型转换为字符串

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

我试图对两个数据帧中的每一行应用pyspark sql函数哈希算法来识别差异。哈希算法是基于字符串的,所以我尝试将字符串以外的任何数据类型转换为字符串。我在日期列转换中遇到了很多问题,因为在转换为字符串之前需要更改日期格式,以使其与基于哈希的匹配保持一致。请帮助我解决方法

#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)