Hướng dẫn how to read excel file in databricks - cách đọc file excel trong databricks

Phương thức pandas.read_excel không hỗ trợ bằng cách sử dụng URL sơ đồ wasbs hoặc abfss để truy cập vào tệp. Để biết thêm chi tiết, vui lòng tham khảo tại đây

Vì vậy, nếu bạn muốn truy cập tệp bằng gấu trúc, tôi khuyên bạn nên tạo mã thông báo SAS và sử dụng sơ đồ ____99 với mã thông báo SAS để truy cập tệp hoặc tải xuống tệp khi luồng sau đó đọc nó bằng gấu trúc. Trong khi đó, bạn cũng gắn tài khoản lưu trữ dưới dạng hệ thống tập tin sau đó truy cập tệp như @Cheekatlapradeep-MSFT cho biết.

Ví dụ

  • Truy cập với mã thông báo SAS
  1. Tạo mã thông báo SAS qua cổng thông tin Azure

    Hướng dẫn how to read excel file in databricks - cách đọc file excel trong databricks

  2. Mã số

pdf=pd.read_excel('https://.dfs.core.windows.net//?')
print(pdf)

Hướng dẫn how to read excel file in databricks - cách đọc file excel trong databricks

  • Tải xuống tệp dưới dạng luồng và đọc tệp
  1. Cài đặt gói

    import io
    
    import pandas as pd
    from azure.storage.filedatalake import BlobServiceClient
    from azure.storage.filedatalake import DataLakeServiceClient
    
    blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')
    
    file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
    with io.BytesIO() as f:
      downloader =file_client.download_file()
      b=downloader.readinto(f)
      print(b)
      df=pd.read_excel(f)
      print(df)
    
    0 và
    import io
    
    import pandas as pd
    from azure.storage.filedatalake import BlobServiceClient
    from azure.storage.filedatalake import DataLakeServiceClient
    
    blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')
    
    file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
    with io.BytesIO() as f:
      downloader =file_client.download_file()
      b=downloader.readinto(f)
      print(b)
      df=pd.read_excel(f)
      print(df)
    
    1 với PIP trong Databricks

  2. Mã số

import io

import pandas as pd
from azure.storage.filedatalake import BlobServiceClient
from azure.storage.filedatalake import DataLakeServiceClient

blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')

file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
with io.BytesIO() as f:
  downloader =file_client.download_file()
  b=downloader.readinto(f)
  print(b)
  df=pd.read_excel(f)
  print(df)

Hướng dẫn how to read excel file in databricks - cách đọc file excel trong databricks

Tải xuống tệp dưới dạng luồng và đọc tệp

Ví dụ

  1. Truy cập với mã thông báo SASplease note that if you use scala 2.11, please add package

    import io
    
    import pandas as pd
    from azure.storage.filedatalake import BlobServiceClient
    from azure.storage.filedatalake import DataLakeServiceClient
    
    blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')
    
    file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
    with io.BytesIO() as f:
      downloader =file_client.download_file()
      b=downloader.readinto(f)
      print(b)
      df=pd.read_excel(f)
      print(df)
    
    4

  2. Mã số

spark._jsc.hadoopConfiguration().set("fs.azure.account.key..dfs.core.windows.net",'')

print("use spark")
df=sqlContext.read.format("com.crealytics.spark.excel") \
        .option("header", "true") \
        .load('abfss:///data/sample.xlsx')

df.show()

Hướng dẫn how to read excel file in databricks - cách đọc file excel trong databricks

Kết quả hàng đầu từ trên web

Đọc tệp Excel từ Azure Databricks - Stack Overflow

Bước 2: Đọc tệp Excel bằng đường dẫn gắn kết. Nhập mô tả hình ảnh ở đây. Tham khảo: Azure Databricks - Azure Data Lake Storage Gen2.

Cách đọc tệp Excel bằng cách sử dụng Databricks

(1) Đăng nhập vào tài khoản Databricks của bạn, nhấp vào cụm, sau đó nhấp đúp vào cụm bạn muốn làm việc. · (2) Nhấp vào thư viện, ...

Đọc thêm>

Đọc và chuyển đổi tệp Excel trong Databricks - Microsoft Q & A

Tôi có yêu cầu đọc tệp Excel được đặt trong Azure Blob thông qua Databricks bằng máy tính xách tay Python và thay thế các ký tự dòng mới có mặt ...

Đọc thêm>

Đọc và chuyển đổi tệp Excel trong Databricks - Microsoft Q & A

Tôi có yêu cầu đọc tệp Excel được đặt trong Azure Blob thông qua Databricks bằng máy tính xách tay Python và thay thế các ký tự dòng mới có mặt ...

Đọc thêm>

Đọc và chuyển đổi tệp Excel trong Databricks - Microsoft Q & A

Tôi có yêu cầu đọc tệp Excel được đặt trong Azure Blob thông qua Databricks bằng máy tính xách tay Python và thay thế các ký tự dòng mới có mặt ...

Chúng ta có thể đọc tệp Excel trong Spark không?

Đọc một tệp excel vào một dòng dữ liệu hoặc sê-ri Pandas-on-Spark. Hỗ trợ cả phần mở rộng tệp XLS và XLSX từ hệ thống tập tin hoặc URL cục bộ. Hỗ trợ một tùy chọn để đọc một tờ hoặc một danh sách các tờ. Chuỗi có thể là một url.

Làm thế nào để Azure đọc các tệp excel?

Trong cơ sở dữ liệu Azure SQL, bạn không thể nhập trực tiếp từ Excel. Trước tiên bạn phải xuất dữ liệu vào tệp văn bản (CSV). Trước khi bạn có thể chạy truy vấn phân tán, bạn phải bật tùy chọn cấu hình máy chủ truy vấn phân tán ad hoc, như trong ví dụ sau.

Bài trung bình liên quan hàng đầu

Câu hỏi về Stackoverflow liên quan hàng đầu

Khắc phục sự cố mã trực tiếp

Lightrun cho phép các nhà phát triển thêm nhật ký, số liệu và ảnh chụp nhanh vào mã trực tiếp - không cần khởi động lại hoặc triển khai lại.

Bắt đầu miễn phí

Chủ đề Reddit liên quan hàng đầu

Blog này chúng tôi sẽ tìm hiểu cách đọc tệp excel trong pyspark (databricks = db, azure = az).

Hầu hết mọi người đã đọc tệp CSV dưới dạng nguồn thực hiện SPARK và thậm chí Spark cung cấp hỗ trợ trực tiếp để đọc tệp CSV nhưng vì tôi được yêu cầu đọc tệp Excel vì nhà cung cấp nguồn của tôi nghiêm ngặt với việc không cung cấp CSV, tôi có nhiệm vụ tìm Giải pháp Cách đọc dữ liệu từ tệp Excel và để tăng độ khó của tôi, tôi phải đọc từ các trang khác nhau của cùng một tệp Excel.

Sau khi tìm kiếm trên Google, tôi không thể tìm thấy bất kỳ câu trả lời trực tiếp nào nhưng vì vậy nghĩ đến việc viết blog này để những người muốn đọc tệp Excel trong Spark (Python) có thể đọc blog này và thực hiện nhanh chóng.

Vì vậy, tôi sẽ giải thích bước khôn ngoan về cách đọc tệp Excel trong PySpark (DB AZ).

Thư viện cần thiết để đọc tệp Excel là Creailytics/Spark-Excel, Thư viện này đã tiết kiệm cho tôi rất nhiều thời gian để đọc Excel và khiến cuộc sống của tôi hạnh phúc hơn với các nhà phát triển và người đóng góp. Đây là một cộng đồng tích cực đang quản lý plugin này.

Bạn có thể thấy nó lạ nhưng trang Git hiển thị mẫu mã trong scala và tất cả các tài liệu là dành cho scala và không phải là một dòng mã cho pyspark, nhưng tôi đã thử may mắn và nó hoạt động cho tôi trong pyspark.

Thư viện này yêu cầu Spark 2.0+

Bạn có thể liên kết với thư viện này trong chương trình của mình tại các tọa độ sau:

Scala 2.12

groupId: com.crealytics
artifactId: spark-excel_2.12
version: 0.13.1

Scala 2.11

groupId: com.crealytics
artifactId: spark-excel_2.11
version: 0.13.1

Cài đặt thư viện trong cụm DB trong AZ.

  1. Truy cập các cụm trong không gian làm việc của DB và nhấp vào cụm bạn muốn cài đặt thư viện, khi bạn nhấp vào tên cụm bạn sẽ gửi trên trang Chi tiết cụm, chuyển sang tab Thư viện và nhấp vào Cài đặt mới.DB workspace and click on the cluster you want to install the library on, once you click on the cluster name you will land on cluster details page, switch to Libraries tab and click on “Install New”.
Tab thư viện cụm
  1. 1 Sau khi nhấp vào thư viện cài đặt, bạn sẽ nhận được cửa sổ bật lên nếu bạn cần nhấp vào Maven và cung cấp các tọa độ sau.

com.crealytics:spark-excel_2.12:0.13.5

Hoặc nếu bạn muốn, bạn có thể nhấp vào các gói tìm kiếm và cửa sổ bật lên sẽ mở các gói tìm kiếm có tên. Từ thả xuống, chọn Maven Central, và gõ com.crealytics, trong hộp tìm kiếm văn bản và chọn phiên bản mới nhất của plugin hoặc theo phiên bản Scala của bạn trong cụm trên DB AZ. Tôi đang đi với Viking Spark-Excel_2.12, theo phiên bản Scala của tôi trên cụm. Khi bạn nhấp vào chọn, nó sẽ đưa các tọa độ như hiển thị trong ảnh chụp màn hình trên và sau đó nhấp vào Cài đặt.“Search Packages”. From dropdown select “Maven Central” and type “com.crealytics” in the text search box and select latest version of the plugin or as per your scala version in Cluster on DB Az. I am going with “spark-excel_2.12” as per my scala version on cluster. As you click on select it will populate the co-ordinates as show in the above screenshot and then click install.

Lựa chọn crealytics maven

Khi thư viện của bạn được cài đặt, bạn sẽ được hiển thị như dưới đây.

Tất cả chúng tôi đã được thiết lập để bắt đầu viết mã của chúng tôi để đọc dữ liệu từ tệp Excel.

2. Mã trong DB Notebook để đọc tệp Excel.

Mã mẫu

#Sample Tệp dữ liệu PathSampleDatafilePath = Hồi DBFS: /filestore/tables/sample_xls_file_for_testing-1.xls
sampleDataFilePath = “dbfs:/FileStore/tables/sample_xls_file_for_testing-1.xls”

#flags cần thiết để đọc excelisheaderon = True true isinferschemaon =
isHeaderOn = “true”
isInferSchemaOn = “false”

Địa chỉ #Sheet trong ExcelSample1Address = Hồi ‘Sample1,!
sample1Address = “‘sample1’!A1”
sample2Address = “‘sample2’!A1”

#read excelfilesample1df = spark.read.format (Hồi com.crealytics.spark.excel,) Voi)
sample1DF = spark.read.format(“com.crealytics.spark.excel”) \
.option(“header”, isHeaderOn) \
.option(“inferSchema”, isInferSchemaOn) \
.option(“treatEmptyValuesAsNulls”, “false”) \
.option(“dataAddress”, sample1Address) \
.load(sampleDataFilePath)
display(sample1DF)

Mã ở trên này sẽ đọc tất cả dữ liệu từ bảng mẫu mẫu11 từ tệp XLS mẫu bắt đầu từ ô A1 đến tất cả các cột.

Bạn cũng có thể chỉ định phạm vi của các ô trong mã, bao gồm bạn chỉ muốn đọc các ô cụ thể.

Mã mẫu để đọc cho phạm vi ô cụ thể.

#Sample Tệp dữ liệu PathSampleDatafilePath = Hồi DBFS: /filestore/tables/sample_xls_file_for_testing-1.xls
sampleDataFilePath = “dbfs:/FileStore/tables/sample_xls_file_for_testing-1.xls”

#flags cần thiết để đọc excelisheaderon = True true isinferschemaon =
isHeaderOn = “true”
isInferSchemaOn = “false”

Địa chỉ #Sheet trong Excelsample1Address = Hồi ‘Sample1,!
sample1Address = “‘sample1’!A1:P35”
sample2Address = “‘sample2’!A1:P35”

#read excelfilesample1df = spark.read.format (Hồi com.crealytics.spark.excel,) Voi)
sample1DF = spark.read.format(“com.crealytics.spark.excel”) \
.option(“header”, isHeaderOn) \
.option(“inferSchema”, isInferSchemaOn) \
.option(“treatEmptyValuesAsNulls”, “false”) \
.option(“dataAddress”, sample1Address) \
.load(sampleDataFilePath)
display(sample1DF)

Địa chỉ dữ liệu

Như bạn có thể thấy trong các ví dụ trên, vị trí của dữ liệu để đọc hoặc ghi có thể được chỉ định với tùy chọn

import io

import pandas as pd
from azure.storage.filedatalake import BlobServiceClient
from azure.storage.filedatalake import DataLakeServiceClient

blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')

file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
with io.BytesIO() as f:
  downloader =file_client.download_file()
  b=downloader.readinto(f)
  print(b)
  df=pd.read_excel(f)
  print(df)
5. Hiện tại các kiểu địa chỉ sau được hỗ trợ:

  • import io
    
    import pandas as pd
    from azure.storage.filedatalake import BlobServiceClient
    from azure.storage.filedatalake import DataLakeServiceClient
    
    blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')
    
    file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
    with io.BytesIO() as f:
      downloader =file_client.download_file()
      b=downloader.readinto(f)
      print(b)
      df=pd.read_excel(f)
      print(df)
    
    6: Tế bào bắt đầu của dữ liệu. Đọc sẽ trả về tất cả các hàng bên dưới và tất cả các cột ở bên phải. Viết sẽ bắt đầu ở đây và sử dụng càng nhiều cột và hàng theo yêu cầu.
  • import io
    
    import pandas as pd
    from azure.storage.filedatalake import BlobServiceClient
    from azure.storage.filedatalake import DataLakeServiceClient
    
    blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')
    
    file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
    with io.BytesIO() as f:
      downloader =file_client.download_file()
      b=downloader.readinto(f)
      print(b)
      df=pd.read_excel(f)
      print(df)
    
    7: Phạm vi dữ liệu của tế bào. Đọc sẽ chỉ trả về các hàng và cột trong phạm vi được chỉ định. Viết sẽ bắt đầu trong ô đầu tiên (
    import io
    
    import pandas as pd
    from azure.storage.filedatalake import BlobServiceClient
    from azure.storage.filedatalake import DataLakeServiceClient
    
    blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')
    
    file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
    with io.BytesIO() as f:
      downloader =file_client.download_file()
      b=downloader.readinto(f)
      print(b)
      df=pd.read_excel(f)
      print(df)
    
    6 trong ví dụ này) và chỉ sử dụng các cột và hàng được chỉ định. Nếu có nhiều hàng hoặc cột trong DataFrame để viết, chúng sẽ bị cắt ngắn. Hãy chắc chắn rằng đây là những gì bạn muốn.
  • import io
    
    import pandas as pd
    from azure.storage.filedatalake import BlobServiceClient
    from azure.storage.filedatalake import DataLakeServiceClient
    
    blob_service_client = DataLakeServiceClient(account_url='https://.dfs.core.windows.net/', credential='')
    
    file_client = blob_service_client.get_file_client(file_system='test', file_path='data/sample.xlsx')
    with io.BytesIO() as f:
      downloader =file_client.download_file()
      b=downloader.readinto(f)
      print(b)
      df=pd.read_excel(f)
      print(df)
    
    9: Tương tự như trên, nhưng với một tờ cụ thể.

Nếu tên trang tính không có sẵn, có thể truyền trong một chỉ mục:

#sheet address in excel
sample1Address = "'0'!A1"
#read excelfile
sample1DF = spark.read.format("com.crealytics.spark.excel") \
.option("header", isHeaderOn) \
.option("inferSchema", isInferSchemaOn) \
.option("treatEmptyValuesAsNulls", "false") \
.option("dataAddress", sample1Address) \
.load(sampleDataFilePath)
display(sample1DF)

Chúng ta có thể đọc tệp Excel trong Spark không?

Đọc một tệp excel vào một dòng dữ liệu hoặc sê-ri Pandas-on-Spark.Hỗ trợ cả phần mở rộng tệp XLS và XLSX từ hệ thống tập tin hoặc URL cục bộ.Hỗ trợ một tùy chọn để đọc một tờ hoặc một danh sách các tờ.Chuỗi có thể là một url.. Support both xls and xlsx file extensions from a local filesystem or URL. Support an option to read a single sheet or a list of sheets. The string could be a URL.

Làm thế nào để Azure đọc các tệp excel?

Trong cơ sở dữ liệu Azure SQL, bạn không thể nhập trực tiếp từ Excel.Trước tiên bạn phải xuất dữ liệu vào tệp văn bản (CSV).Trước khi bạn có thể chạy truy vấn phân tán, bạn phải bật tùy chọn cấu hình máy chủ truy vấn phân tán ad hoc, như trong ví dụ sau.You must first export the data to a text (CSV) file. Before you can run a distributed query, you have to enable the ad hoc distributed queries server configuration option, as shown in the following example.