我正在寻找使用scala从spark中的excel文件构造数据框的方法?我在下面提到了 SO 帖子,并尝试对附加的 excel 表进行操作。
如何从 Scala Spark 中的 Excel (xls,xlsx) 文件构造数据框?
不幸的是,以下修改后的代码并未读取 excel 中的所有列。
val df = spark.read.format("com.crealytics.spark.excel")
.option("sheetName", "Sheet1") // Required
.option("useHeader", "false") // Required
.option("treatEmptyValuesAsNulls", "false") // Optional, default: true
.option("inferSchema", "true") // Optional, default: false
.option("addColorColumns", "false") // Optional, default: false
.option("startColumn", 0) // Optional, default: 0
.option("endColumn", 99) // Optional, default: Int.MaxValue
.option("timestampFormat", "MM-dd-yyyy HH:mm:ss") // Optional, default: yyyy-mm-dd hh:mm:ss[.fffffffff]
.option("maxRowsInMemory", 20) // Optional, default None. If set, uses a streaming reader which can help with big files
.option("excerptSize", 10) // Optional, default: 10. If set and if schema inferred, number of rows to infer schema from
.option("path", excelFile)
//.schema(customSchema)
.load()
+---+---+--------------+---+---+
|_c0|_c1| _c2|_c3|_c4|
+---+---+--------------+---+---+
| | |Test Profile 1| A|123|
| | |Test Profile 2| B| |
| | |Test Profile 3| C| |
| | |Test Profile 4| D| |
| | |Test Profile 5| E| |
| | |Test Profile 6| F| |
+---+---+--------------+---+---+
我在这里错过了什么吗?
我的目标是从随机分布的表格中获取所有数据,然后从中获取特定值。一些单元格可以是空白的。
我可以使用 apache poi 在 scala 中执行此操作,获取所需的值,转换为 csv,然后加载到数据帧中。
但是,我正在寻找一种方法来使用 scala 将 excel 表直接解析为数据框,遍历数据框行并应用条件来获取所需的行/列。
ps 对不起,我不知道如何从我的本地机器附加一个 excel 文件。
谢谢!