我正在研究 PySpark( Python 3.6 和 Spark 2.1.1 )并尝试使用 从 Excel 文件中获取数据spark.read.format("com.crealytics.spark.excel") ,但它推断日期类型列的 double 。
例子:
输入 -
df = spark.read.format("com.crealytics.spark.excel").\
option("location", "D:\\Users\\ABC\\Desktop\\TmpData\\Input.xlsm").\
option("spark.read.simpleMode","true"). \
option("treatEmptyValuesAsNulls", "true").\
option("addColorColumns", "false").\
option("useHeader", "true").\ option("inferSchema", "true").\
load("com.databricks.spark.csv")
结果:
Name | Age | Gender | DateOfApplication
________________________________________
X | 12 | F | 5/20/2015
Y | 15 | F | 5/28/2015
Z | 14 | F | 5/29/2015
打印模式 -
df.printSchema()
root
|-- Name: string (nullable = true)
|-- Age: double (nullable = true)
|-- Gender: string (nullable = true)
|-- DateOfApplication: double (nullable = true)
做.show -
df.show()
Name | Age | Gender | DateOfApplication
________________________________________
X | 12.0 | F | 42144.0
Y | 15.0 | F | 16836.0
Z | 14.0 | F | 42152.0
在读取数据集时,日期或任何其他数值被转换为 double (日期的特殊问题是它完全改变了难以恢复到原始日期的值。
请问我可以得到帮助吗?
最佳答案
插件的作者在这里:)
推断列类型为 done in the plugin itself .
那个代码是taken from spark-csv .从代码中可以看出,目前仅推断出 String、Numeric、Boolean 和 Blank 单元格类型。
最好的选择是创建一个使用 the corresponding DateUtil API 正确推断日期列的 PR .
第二好的选择是手动指定模式,类似于@addmeaning 的描述。请注意,我刚刚发布了版本 0.9.0,其中 makes some required parameters optional和 changes the way the path to the file needs to be specified .
yourSchema = StructType()
.add("Name", StringType(), True)
.add("Age", DoubleType(), True)
.add("Gender", StringType(), True)
.add("DateOfApplication", DateType(), True)
df = spark.read.format("com.crealytics.spark.excel").
schema(yourSchema).
option("useHeader", "true").\
load("D:\\Users\\ABC\\Desktop\\TmpData\\Input.xlsm")
关于excel - inferSchema using spark.read.format ("com.crealytics.spark.excel")正在为日期类型列推断 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45713699/