有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

hadoop如何在spark中用java读取xls和xlsx文件?

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

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

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

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

请建议

谢谢


共 (4) 个答案

  1. # 2 楼答案

    你不能用spark来做这件事。这不是为它准备的。使用其他库,例如Apache POI读取excel,然后将数据作为文本输入spark

  2. # 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();
        }
    }
    
  3. # 4 楼答案

    虽然这个问题有点老了,但我仍在回答。也许对其他人有用。 答案是肯定的,你可以用ApacheSpark2来实现。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