0

我想在 spark 中逐行读取 xls 和 xlsx (MS Excel) 文件,就像我们对文本文件做的那样,或者任何方式?

我想使用 spark 来提高读取大型 xls 文件(例如 1 GB)的性能,这就是为什么我需要 spark 像我们对文本文件一样读取部分文件的原因。

如何从spark中的excel文件中读取数据,无论是否逐行?

我只想使用 spark 读取 xls 文件中的条目。

请建议。

谢谢!!!

4

4 回答 4

3

这是我的做法。

在maven中添加依赖

<dependencies>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.crealytics</groupId>
        <artifactId>spark-excel_2.11</artifactId>
        <version>0.11.1</version>
    </dependency>
</dependencies>

我的主要课程

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

public class ReadExcelSheets {

    public static void main(String[] args) {
        //skip logging extras
        Logger.getLogger("org").setLevel(Level.ERROR);

       //build session
        SparkSession spark = SparkSession
                .builder()
                .appName("Java Spark SQL Example")
                .config("spark.master", "local")
                .getOrCreate();

        //read excel - change file name
        Dataset<Row> df = spark.read()
                .format("com.crealytics.spark.excel")
                .option("useHeader", "true")
                //.option("dataAddress", "'Sheet1'!A1:M1470") // optional when you want to read sheets where A1 first top cell and M1470 us very bottom left of sheet.
                .load("datasets/test1.xlsx");
        //show your data
        df.show();
    }
}
于 2019-05-28T08:24:26.810 回答
2

你不能用spark. 它不是为此而生的。使用其他库,例如Apache POI来读取 excel,然后将该数据作为文本提供给 spark。

于 2015-07-29T07:15:32.627 回答
2

虽然这个问题有点老了,但我仍然在回答。可能对其他人有用。答案是肯定的,你可以用 apache spark 2.x 做到这一点。假设您要将具有 3 列的 xls 转换为数据集。

  class Bean {
     private String col1;
     private String col2;   
     private Timestamp col3;
}

StructType structType= new StructType(new StructField[] {
                new StructField("col1", DataTypes.StringType, true, Metadata.empty()),
                new StructField("col2", DataTypes.StringType, true, Metadata.empty()),
                new StructField("col3", DataTypes.TimestampType, true, Metadata.empty())
        });

Dataset<Bean> ds = sparkSession.read().
                schema(structType).
                format("com.crealytics.spark.excel").
                option("useHeader", true). // If the xls file has headers
                option("timestampFormat", "yyyy-MM-dd HH:mm:ss"). // If you want to convert timestamp to a specific format
                option("treatEmptyValuesAsNulls", "false").
                option("inferSchema", "false").
                option("addColorColumns", "false").
                load("/home/user/test/sample.xls"). //path to xls or xlsx
                as(Encoders.bean(Bean.class)); // Bean in which you want to convert the data, you can remove this line if Dataset<Row> is just fine for you
于 2018-05-18T09:13:59.400 回答
0

您可以尝试使用 HadoopOffice 库使用 Spark ( https://github.com/ZuInnoTe/hadoopoffice/wiki ) 读取/写入 Excel 文件。它支持加密的 Excel、链接的工作簿、按元数据过滤......

于 2017-06-17T17:28:16.007 回答