首先,这是Maven依赖 https://mvnrepository.com/artifact/com.crealytics/spark-excel_2.11/0.12.0 I used
<!-- https://mvnrepository.com/artifact/com.crealytics/spark-excel -->
<dependency>
<groupId>com.crealytics</groupId>
<artifactId>spark-excel_2.11</artifactId>
<version>0.12.0</version>
</dependency>
问题:据我了解,未来的excel文件保存在hdfs文件系统中,对吧?我需要设定未来的道路
.save() 方法中的 excel 文件,对吧?我也不懂什么
格式应该在 dataAddress 选项中吗?
什么是数据地址?
来自文档
数据地址: https://github.com/crealytics/spark-excel#data-addresses的位置
可以使用 dataAddress 选项指定要读取或写入的数据。
目前支持以下地址样式:
B3:数据的起始单元格。读取将返回下面的所有行以及所有
右侧的列。写作将从这里开始并使用尽可能多的栏目
和所需的行。 B3:F35:数据单元格范围。读书会回来
仅指定范围内的行和列。写作将开始于
第一个单元格(本例中为 B3)并仅使用指定的列
和行。如果 DataFrame 中有更多行或列要写入,
它们将被截断。确保这是您想要的。 '我的
Sheet'!B3:F35:与上面相同,但使用特定的工作表。
MyTable[#All]:数据表。读取将返回所有行和列
在此表中。写入时只会在当前范围内写入
桌子。不会执行表的增长
所以 "My Sheet1'!B3:C35" 意味着您正在告诉 api...我的 Sheet1 和 B3:C35
excel工作表中的列位置..
下面是我实现期望的完整列表。
package com.examples
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.SparkSession
object ExcelTest {
def main(args: Array[String]) {
import org.apache.spark.sql.functions._
Logger.getLogger("org").setLevel(Level.OFF)
val spark = SparkSession.builder.
master("local")
.appName(this.getClass.getName)
.getOrCreate()
import spark.implicits._
val df1 = Seq(
("2019-01-01 00:00:00", "7056589658"),
("2019-02-02 00:00:00", "7778965896")
).toDF("DATE_TIME", "PHONE_NUMBER")
df1.show()
val df2 = Seq(
("2019-01-01 01:00:00", "194.67.45.126"),
("2019-02-02 00:00:00", "102.85.62.100"),
("2019-03-03 03:00:00", "102.85.62.100")
).toDF("DATE_TIME", "IP")
df2.show()
df1.coalesce(1).write
.format("com.crealytics.spark.excel")
.option("dataAddress", "'My Sheet1'!B3:C35")
.option("useHeader", "true")
.option("dateFormat", "yy-mmm-d")
.option("timestampFormat", "mm-dd-yyyy hh:mm:ss")
.mode("append")
.save(".\\src\\main\\resources\\testexcel.xlsx")
df2.coalesce(1).write
.format("com.crealytics.spark.excel")
.option("dataAddress", "'My Sheet2'!B3:C35")
.option("useHeader", "true")
.option("dateFormat", "yy-mmm-d")
.option("timestampFormat", "mm-dd-yyyy hh:mm:ss")
.mode("append")
.save(".\\src\\main\\resources\\testexcel.xlsx")
}
}
注意:.coalesce(1) 将创建单个文件而不是多个部分文件...
结果:由于我使用本地结果,如果它的纱线将保存在 hdfs 中,结果将保存在本地。如果你想使用像s3这样的云存储,也可以使用yarn作为master。基本上这是根据你的要求...
表 1:
表2:
还,
1)看我的文章如何使用 Apache Spark Scala 使用 Excel 工作表进行简单报告? https://www.linkedin.com/pulse/how-do-simple-reporting-excel-sheets-using-apache-spark-ghadiyaram/
2)看我的回答here. https://stackoverflow.com/a/47827565/647053
希望有帮助!