2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WindowsのRubyで、Apache ArrowのParquetを使えるようにするまでの道のり

Last updated at Posted at 2025-05-01

msys2には、Arrowのパッケージが収録されているようです。

red-parquet を入れるまで

まずpacmanシステムを最新にする

> ridk enable
> ridk exec pacman.exe -Syuu
> ridk exec pacman.exe -Syuu

-Syuu でないとダウングレードが有効にならない

なお以下エラーが出るときは cmd.exe が管理者権限になっていない様子

error: failed to init transaction (unable to lock database)
error: could not lock database: Permission denied

Pacman を使って、msys2にarrowのバイナリを入れる

> ridk exec pacman.exe -S mingw-w64-ucrt-x86_64-arrow
 ごりごりインストール
 
> ridk exec pacman -Ss mingw-w64-ucrt-x86_64-arrow
ucrt64/mingw-w64-ucrt-x86_64-arrow 20.0.0-1 [インストール済み]
    Apache Arrow is a cross-language development platform for in-memory data (mingw-w64)

めでたく [installed] のフラグがつきました

gio2を入れる

red-arrow は gio2パッケージに依存するので

ridk exec pacman -Ss glib2
ucrt64/mingw-w64-ucrt-x86_64-glib2 2.84.1-2 [インストール済み]
Common C routines used by GTK+ 3 and other libs (mingw-w64)

gem list gio2
*** LOCAL GEMS ***
gio2 (4.2.9)

Rubyモジュールを入れる

> gem install red-parquet
Temporarily enhancing PATH for MSYS/MINGW...
Using msys2 packages: mingw-w64-ucrt-x86_64-arrow>=19.0.1
Building native extensions. This could take a while...
Successfully installed red-arrow-19.0.1
Building native extensions. This could take a while...
Successfully installed red-parquet-19.0.1
Parsing documentation for red-arrow-19.0.1
Installing ri documentation for red-arrow-19.0.1
Parsing documentation for red-parquet-19.0.1
Installing ri documentation for red-parquet-19.0.1
Done installing documentation for red-arrow, red-parquet after 2 seconds
2 gems installed

試してみる

> irb
irb(main):001:0> require "arrow"
=> true

やったね

RedAmberも入るかな?

> gem install red_amber
Fetching red_amber-0.5.2.gem
Successfully installed red_amber-0.5.2
Parsing documentation for red_amber-0.5.2
Installing ri documentation for red_amber-0.5.2
Done installing documentation for red_amber after 0 seconds
1 gem installed

require 'red_amber'

ができるようになった

各種レシピ

郵便番号から住所を得るサンプル

日本郵便のサイトから、郵便番号csvをダウンロードしておく

post.rb
require "parquet"
require 'red_amber'
include RedAmber

# 引数は CSV.new() と互換あり https://docs.ruby-lang.org/ja/latest/class/CSV.html
df1 = DataFrame.load("01HOKKAI.CSV", format: :csv, headers:false, encoding: "cp932")
df2 = DataFrame.load("02AOMORI.CSV", format: :csv, headers:false, encoding: "cp932")

df = df1.concat(df2)		# 本当はすべての県を連結する必要がある
print df

# 郵便番号 〒0650023 の住所はどこかな?
print res = df[ df[:f2] == "0650023".to_i ]
print res[:f6,:f7,:f8]

# 次からはparquetを開くといいね
df.save("./jpzip.parquet")

出力結果

> ruby post.rb
          f0      f1      f2 f3             f4                       ...     f14
     <int64> <int64> <int64> <string>       <string>                 ... <int64>
   0    1101      60  600000 ホツカイドウ サツポロシチユウオウク ...       0
   1    1101      64  640941 ホツカイドウ サツポロシチユウオウク ...       0
   2    1101      60  600041 ホツカイドウ サツポロシチユウオウク ...       0
   3    1101      60  600042 ホツカイドウ サツポロシチユウオウク ...       0
   4    1101      64  640820 ホツカイドウ サツポロシチユウオウク ...       0
    :       :       :       : :              :                            ...       :
10716    2446    3912  391204 アオモリケン   サンノヘグンハシカミチヨウ ...       0
10717    2450    3918  391800 アオモリケン   サンノヘグンシンゴウムラ ...       0
10718    2450    3918  391802 アオモリケン   サンノヘグンシンゴウムラ ...       0
10719    2450    3918  391801 アオモリケン   サンノヘグンシンゴウムラ ...       0
       f0      f1      f2 f3             f4                     f5                     ...     f14
  <int64> <int64> <int64> <string>       <string>               <string>               ... <int64>
0    1103      65  650023 ホツカイドウ サツポロシヒガシク キタ23ジヨウヒガシ ...       0
  f6       f7         f8
  <string> <string>   <string>
0 北海道   札幌市東区 北二十三条東

できました

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?