MATLABでのParquetファイルの読み込み アイキャッチ画像
MATLABでのParquetファイルの読み込み アイキャッチ画像

以前の記事ではmatlab.net.http でファイルのアップロードをする方法を書きました。今回もMATLAB ネタです。

ビッグデータでParquet ファイルの存在がますます増えてきています。Parquet は英語読みだと「パーケット」、フランス語読みだと「パルケ」だそうです。

機械学習などの例によく使われる米国ニューヨーク州のタクシーの乗車ログのデータも、これまでCSV ファイルでデータを公開していたのですが、2022年5月13日からParquet ファイルで公開されるようになりました。

ATTENTION!

On 05/13/2022, we are making the following changes to trip record files:

  1. All files will be stored in the PARQUET format. Please see the ‘Working With PARQUET Format’ under the Data Dictionaries and MetaData section.
  2. Trip data will be published monthly (with two months delay) instead of bi-annually.
  3. HVFHV files will now include 17 more columns (please see High Volume FHV Trips Dictionary for details). Additional columns will be added to the old files as well. The earliest date to include additional columns: February 2019.
  4. Yellow trip data will now include 1 additional column (‘airport_fee’, please see Yellow Trips Dictionary for details). The additional column will be added to the old files as well. The earliest date to include the additional column: January 2011.
https://www.nyc.gov/site/tlc/about/tlc-trip-record-data.page

MATLAB もバージョンR2019a からParquet ファイルの読み込み (parquetreadparquetDatastore)とメタデ―タの取得 (parquetinfo)、書き込み(parquetwrite) に対応しています。

[MathWorks] parquetread のドキュメント

しかしR2023a 時点で、parquetread やparquetwrite で対応していないParquet ファイルもあります。

例えばニューヨーク州のタクシーデータのうちイエロータクシー (例として2023年1月の Yellow Taxi Trip Records)は拡張子は.parquet ですが、圧縮形式がZSTD になっています。

parquetread を実行してみるとエラーが出ます。

if ~isfile("yellow_tripdata_2023-04.parquet") 
    websave("yellow_tripdata_2023-04.parquet", "https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2023-04.parquet") 
end 
t = parquetread('yellow_tripdata_2023-04.parquet');Code language: Matlab (matlab)

【実行結果】

次を使用中のエラー: parquetread 
Parquet ファイルを読み取れません: 

何が原因かが分からないのでparquetread 関数にブレークポイントを張ってデバッグしてみると、try/catchのエクセプションで、以下のエラーが確認できました。

>> ME.message

NotImplemented: Support for codec ‘zstd’ not built

ZSTD の圧縮形式に対応していなくて読めていないということですね。

parquetinfo でメタデ―タを取得

次にparquetinfo でParquet ファイルのメタデ―タを取得しましょう。

pinf = parquetinfo('yellow_tripdata_2023-04.parquet') Code language: Matlab (matlab)

【実行結果】

pinf =  
ParquetInfo のプロパティ: 
Filename: “C:\yellow_tripdata_2023-04.parquet” 
FileSize: 54222699
NumRowGroups: 1 
RowGroupHeights: 3288250
VariableNames: [“VendorID”    “tpep_pickup_datetime”    …    ] 
  VariableTypes: [“int32”    “datetime”    “datetime”    …    ] 
VariableCompression: [“zstd”    “zstd”    “zstd”    “zstd”    …    ] 
VariableEncoding: [“dictionary”    “dictionary”    “dictionary”    …    ] 
Version: “2.0” 

確かに圧縮形式がzstd になっています。

MATLAB のparquetwrite のドキュメントを読むと、対応している圧縮形式がsnappy、brotli、gzip の3種類だと書いてあります。

圧縮方式名。次の値のいずれかとして指定します。

  • 'snappy''brotli''gzip'、または 'uncompressed'。1 つの圧縮アルゴリズムを指定すると、parquetwrite は同じアルゴリズムを使用してすべての変数を圧縮します。
https://jp.mathworks.com/help/matlab/ref/parquetwrite.html

一旦snappy かgzip で変換

Python のPyArrow を使って、MATLAB が対応している圧縮形式に変換します。

スクリプトに書くほどでも無いのでMATLAB のコマンドウィンドウからそのままpyrun で実行させます。一度pyarrow でread_table をして、write_table で圧縮形式をsnappy に指定して書き込みます。

system('pip install pyarrow') 

pyrun('import pyarrow.parquet as pq') 

pyrun('trips = pq.read_table("yellow_tripdata_2023-04.parquet")') 

pyrun('pq.write_table(trips, "yellow_tripdata_2023-04_snappy.parquet", compression="snappy")') Code language: Matlab (matlab)

これで圧縮形式がsnappy のParquet ファイルができましたので、MATLAB のparquetread で読めます。

t = parquetread('yellow_tripdata_2023-04_snappy.parquet'); 

head(t) % 先頭8行を確認Code language: JavaScript (javascript)

【実行結果】

VendorID tpep_pickup_datetime tpep_dropoff_datetime passenger_count trip_distance RatecodeID store_and_fwd_flag PULocationID DOLocationID payment_type fare_amount extra mta_tax tip_amount tolls_amount improvement_surcharge total_amount congestion_surcharge Airport_fee
 ________ ____________________ _____________________ _______________ _____________ __________ __________________ ____________ ____________ ____________ ___________ _____ _______ __________ ____________ _____________________ ____________ ____________________ ___________
1 2023/04/01 00:14:49 2023/04/01 00:45:01 2 4.9 1 "N" 48 223 1 28.9 3.5 0.5 6 0 1 39.9 2.5 0
2 2023/04/01 00:00:24 2023/04/01 00:56:19 1 21.89 2 "N" 132 43 2 70 0 0.5 0 6.55 1 81.8 2.5 1.25
1 2023/04/01 00:03:50 2023/04/01 00:14:42 2 1.3 1 "N" 148 113 1 11.4 3.5 0.5 2 0 1 18.4 2.5 0
1 2023/04/01 00:53:18 2023/04/01 01:01:28 1 1.5 1 "N" 249 79 1 10 3.5 0.5 1 0 1 16 2.5 0
2 2023/04/01 00:07:00 2023/04/01 00:17:16 2 1.49 1 "N" 158 246 1 11.4 1 0.5 1 0 1 17.4 2.5 0
1 2023/04/01 00:08:59 2023/04/01 00:15:39 6 1.2 1 "N" 163 141 1 9.3 3.5 0.5 3.55 0 1 17.85 2.5 0
2 2023/04/01 00:27:52 2023/04/01 00:43:07 1 8.61 1 "N" 138 170 1 33.8 6 0.5 10.32 6.55 1 61.92 2.5 1.25
2 2023/04/01 00:48:38 2023/04/01 01:08:37 1 3.88 1 "N" 170 145 1 21.9 1 0.5 6.72 0 1 33.62 2.5 0Code language: Matlab (matlab)

圧縮形式を変えることでMATLAB でテーブル型として読み込めましたね。

Categories:

No responses yet

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

スポンサーリンク
カテゴリー