【AWS】AWS Glue ~ Excelを扱うには / PySpark (Glue2.0) 版 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2019/11/08/000000

の続き。

今回は、AWS Glue の PySpark (Glue2.0) で、Excelを扱う。

補足:PySpark (Glue1.0) 未満について

今回は、Pandas を使用する。
Pandas の PySpark でのデフォルトサポートは、Glue2.0からである。

PySpark (Glue1.0) 未満の場合、Pandas がサポートされていないので
今回とは別の方法をとる必要があることに注意。

■ 課題

https://dk521123.hatenablog.com/entry/2020/08/19/130118

で触れたとおり、AWS Glue / PySpark (Glue2.0) では、
Pandas がデフォルトで導入された。

その Pandas を使って、Excelファイルを開こうとすると
以下の「エラー内容」が表示される。

エラー内容

Missing optional dependency 'xlrd'. install xrld >= 1.0.0 for Excel support Use pip or conda to install xlrd

エラーの原因

以下の関連記事と同様で「xlrdがないため」

https://dk521123.hatenablog.com/entry/2019/11/08/000000

■ サンプル

import io
import pandas as pd
import boto3

s3_client = boto3.client('s3')

response = s3_client.get_object(
  Bucket='bucket-name', Key="sample.xlsx")

data_frame = pd.read_excel(
  io.BytesIO(response['Body'].read()), encoding='utf-8')

# Output
print(data_frame)

■ 解決案

* xlrd を組み込む

具体的な方法

 今回は、以下の関連記事と同様に、モジュールを落としてきて
S3に格納し、「Python library path」に
そのパス(s3://xxxx/xxx/xlrd-1.2.0-py2.py3-none-any.whl)を指定した

https://dk521123.hatenablog.com/entry/2019/11/08/000000

補足:別方法について
https://dev.classmethod.jp/articles/20200812-aws-glue-version2-python-module-update/

でやっているように、AWS Glue2.0 でサポートされた 
Pythonモジュールの指定のサポートや
Pythonパッケージインストーラー(pip3)によるモジュールインストールで
対応したかったのだが、以下のエラーで断念した。
~~~~
Could not find a version that satisfies the requirement xlrd==1.0.0 ...
~~~~

恐らく、環境が外部アクセスできないからではないからかと思うが、、、

関連記事

AWS Glue ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/01/221926
AWS Glue ~ Excelを扱うには / Python shell 版 ~
https://dk521123.hatenablog.com/entry/2019/11/08/000000
AWS Glue ~ Glue Version 2.0 ~
https://dk521123.hatenablog.com/entry/2020/08/19/130118