欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Apache Spark處理Excel文件的方法

 更新時間:2024年01月22日 11:39:27   作者:i查拉圖斯特拉如是  
Excel作為功能強大的數(shù)據(jù)處理軟件,廣泛應用于各行各業(yè),從企業(yè)管理到數(shù)據(jù)分析,可謂無處不在,本文介紹使用Apache Spark處理Excel文件的簡易指南,感興趣的朋友一起看看吧

前言

在日常的工作中,表格內(nèi)的工具是非常方便的x,但是當表格變得非常多的時候,就需要一些特定的處理。Excel作為功能強大的數(shù)據(jù)處理軟件,廣泛應用于各行各業(yè),從企業(yè)管理到數(shù)據(jù)分析,可謂無處不在。然而,面對大型且復雜的數(shù)據(jù),Excel的處理能力可能力不從心。

對此,我們可借助Apache Spark這一分布式計算框架,憑借其強大的計算與數(shù)據(jù)處理能力,快速有效地處理Excel數(shù)據(jù)。這些數(shù)據(jù)進行一個分析,整理,篩選,排序。分析整理有用的內(nèi)容。

操作

創(chuàng)建一個spark項目,在IntelliJ IDEA中創(chuàng)建Spark項目時,默認的目錄結構如下:

project-root/
│
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── (Java source files)
│   │   └── scala/
│   │       └── (Scala source files)
│   └── test/
│       ├── java/
│       │   └── (Test Java source files)
│       └── scala/
│           └── (Test Scala source files)
├── resources/
│   └── (Resource files)
└── target/
    └── (Compiled output and build artifacts)

導入包

在build.sbt中添加操作文件的包

libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % sparkVersion,
  "org.apache.spark" %% "spark-sql" % sparkVersion,
  "org.apache.spark" %% "spark-mllib" % sparkVersion,
  "org.apache.spark" %% "spark-streaming" % sparkVersion,
  "com.norbitltd" %% "spoiwo_2.12" % "1.4.1",
  "com.crealytics" %% "spark-excel" % "0.13.7",
  "com.monitorjbl" %% "xlsx-streamer" % "2.1.0"
)

測試數(shù)據(jù)

name

age

Mic

1

Andy

3

Steven

1

首先

使用Spark讀取Excel文件十分簡便。只需在DataFrame API中指定文件路徑及格式,Spark即可自動導入Excel文件并將其轉成DataFrame,進而展開數(shù)據(jù)處理和分析。

代碼示例

Spark不但提供多樣的數(shù)據(jù)處理方式,更在DataFrame API中支持篩選、聚合和排序等操作。此外,內(nèi)置豐富的數(shù)據(jù)處理函數(shù)和操作符使處理Excel數(shù)據(jù)更為便捷。

package com.example.spark
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SparkSession
object SparkTest {
  def main(args: Array[String]): Unit = {
    //scala版本
    val sparkConf = new SparkConf()
    sparkConf.setMaster("local")   //本地單線程運行
    sparkConf.setAppName("testJob")
//    val sc = new SparkContext(sparkConf)
    val spark = SparkSession.builder().config(sparkConf)
      .appName("Excel Demo")
      .getOrCreate()
    // 讀取 Excel 文件
    val df = spark.read
      .format("com.crealytics.spark.excel")
      .option("dataAddress", "'Sheet2'!A1:B2") // 可選,設置選擇數(shù)據(jù)區(qū)域 例如 A1:C2。
      .option("useHeader", "false") // 必須,是否使用表頭,false的話自己命名表頭(_c0),true則第一行為表頭
      .option("treatEmptyValuesAsNulls", "true") // 可選, 是否將空的單元格設置為null ,如果不設置為null 遇見空單元格會報錯 默認t: true
      .option("inferSchema", "true") // 可選, default: false
      //.option("addColorColumns", "true") // 可選, default: false
      //.option("timestampFormat", "yyyy-mm-dd hh:mm:ss") // 可選, default: yyyy-mm-dd hh:mm:ss[.fffffffff]
      //.option("excerptSize", 6) // 可選, default: 10. If set and if schema inferred, number of rows to infer schema from
      //.option("workbookPassword", "pass") // 可選, default None. Requires unlimited strength JCE for older JVMs====
      //.option("maxRowsInMemory", 20) // 可選, default None. If set, uses a streaming reader which can help with big files====
      .schema(schema) // 可選, default: Either inferred schema, or all columns are Strings
//      .option("header", "true")
      .load("path/to/excel/file.xlsx")
    // 顯示 DataFrame 的內(nèi)容
    df.show()
    // +-------+---+
    // |   name|age|
    // +-------+---+
    // |    Mic| 1|
    // |   Andy| 3|
    // | Steven| 1|
    // +-------+---+
    // 將 DataFrame 寫入 Excel 文件
    df.write
      .format("com.crealytics.spark.excel")
      .option("dataAddress", "'Sheet'!A1:B2")
      .option("useHeader", "true")
      //.option("dateFormat", "yy-mmm-d") // Optional, default: yy-m-d h:mm
      //.option("timestampFormat", "mm-dd-yyyy hh:mm:ss") // Optional, default: yyyy-mm-dd hh:mm:ss.000
      .mode("append") // Optional, default: overwrite.
      .option("header", "true")
      .save("path/to/save/excel/file.xlsx")
  }
}

數(shù)據(jù)處理結束后,可將結果保存在全新Excel文件或其他格式文件中。借助DataFrame API,無論保存在本地文件系統(tǒng)還是云端,均能輕松實現(xiàn)。保留數(shù)據(jù)亦可依照需求選擇不同輸出格式,如CSV,XLSX等。

總結一下

雖然僅處理基礎數(shù)據(jù),但在集群環(huán)境下,Spark展現(xiàn)出優(yōu)秀的大規(guī)模數(shù)據(jù)處理能力。無論海量Excel數(shù)據(jù)還是復雜的結構化數(shù)據(jù),都在Spark協(xié)助下,能輕松應對并滿足各種數(shù)據(jù)處理與分析任務。

借助Apache Spark處理Excel文件,充分發(fā)揮分布式計算潛能,可讓數(shù)據(jù)處理與分析過程更為高效出色,同時也極大提升數(shù)據(jù)處理效率和準確性。希望本文能讓您對Spark處理Excel有更深入了解,在實踐中更好地應用。

引用

https://github.com/crealytics/spark-excel

最后

到此這篇關于使用Apache Spark處理Excel文件的簡易指南的文章就介紹到這了,更多相關Apache Spark處理Excel文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論