java - 如何在apache spark java中使用hadoop office库将数据集写入excel文件

标签 java apache-spark apache-spark-sql spark-excel

目前我正在使用 com.crealytics.spark.excel 读取 Excel 文件,但是使用这个库我无法将数据集写入 Excel 文件。

link说使用 hadoop office 库 (org.zuinnote.spark.office.excel) 我们可以读取和写入 Excel 文件

请帮我将数据集对象写入 spark java 中的 excel 文件。

最佳答案

您可以使用 org.zuinnote.spark.office.excel 使用数据集读取和写入 excel 文件。示例在 https://github.com/ZuInnoTe/spark-hadoopoffice-ds/ 处给出。 .但是,如果您读取 Dataset 中的 Excel 并尝试将其写入另一个 Excel 文件,则会出现一个问题。请在 https://github.com/ZuInnoTe/hadoopoffice/issues/12 查看 Scala 中的问题和解决方法.

我使用 org.zuinnote.spark.office.excel 用 Ja​​va 编写了一个示例程序,并在该链接中给出了解决方法。请看看这是否对您有帮助。

public class SparkExcel {
    public static void main(String[] args) {
        //spark session
        SparkSession spark = SparkSession
                .builder()
                .appName("SparkExcel")
                .master("local[*]")
                .getOrCreate();

        //Read
        Dataset<Row> df = spark
                .read()
                .format("org.zuinnote.spark.office.excel")
                .option("read.locale.bcp47", "de")
                .load("c:\\temp\\test1.xlsx");

        //Print
        df.show();
        df.printSchema();

        //Flatmap function
        FlatMapFunction<Row, String[]> flatMapFunc = new FlatMapFunction<Row, String[]>() {
            @Override
            public Iterator<String[]> call(Row row) throws Exception {
                ArrayList<String[]> rowList = new ArrayList<String[]>();
                List<Row> spreadSheetRows = row.getList(0);
                for (Row srow : spreadSheetRows) {
                    ArrayList<String> arr = new ArrayList<String>();
                    arr.add(srow.getString(0));
                    arr.add(srow.getString(1));
                    arr.add(srow.getString(2));
                    arr.add(srow.getString(3));
                    arr.add(srow.getString(4));
                    rowList.add(arr.toArray(new String[] {}));
                }
                return rowList.iterator();
            }
        };

        //Apply flatMap function
        Dataset<String[]> df2 = df.flatMap(flatMapFunc, spark.implicits().newStringArrayEncoder());

        //Write
        df2.write()
           .mode(SaveMode.Overwrite)
           .format("org.zuinnote.spark.office.excel")
           .option("write.locale.bcp47", "de")
           .save("c:\\temp\\test2.xlsx");

    }
}

我已经使用 Java 8 和 Spark 2.1.0 测试了这段代码。我正在使用 Maven 并为来自 https://mvnrepository.com/artifact/com.github.zuinnote/spark-hadoopoffice-ds_2.11/1.0.3org.zuinnote.spark.office.excel 添加依赖项

关于java - 如何在apache spark java中使用hadoop office库将数据集写入excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44799949/

相关文章:

Java:如何取出字符串中的一个字母,同时打印字符串中的其余字母

java - 如何使用CDI从请求中获取 token ?

scala - 收集要设置的 Spark 数据帧列值

apache-spark - Spark 如何跟踪 randomSplit 中的分割?

java - Tika 将 docx 文件检测为 Zip

java - 游戏打不开,黑屏

hive - 无法使用 JDBC 连接到 Spark Thrift Server,继续使用 Hive

python - Spark DataFrame 聚合和分组多个列,同时保留顺序

scala - 在 Spark 中,我无法按现有列进行过滤

java - Java 中的通配符不适用于 Spark cogroup 函数