0
import pyarrow.orc
orc_writer = pyarrow.orc.ORCWriter('test.orc')

とかいてみたところ
zsh: segmentation fault python test.py
となってしまいます

何かデータを書き込まないとダメなのかと思い
以下のコードで1レコードだけいろいろな型のデータを書き込んでみたんですがかわらず segmentation fault になります

import pyarrow as pa
import pyarrow.orc

with pyarrow.orc.ORCWriter('test.orc') as orc_writer:
  data = [
    pa.array(['abc'], type=pa.string()),
    pa.array([10], type=pa.int32()),
    pa.array([1.5], type=pa.float64()),
    pa.array([1646200394], type=pa.timestamp('s')),
    pa.array([['x1', 'y1']], type=pa.list_(pa.string())),
    pa.array([b"\x00\x00\x00\x00\x01\x01\x00\x00\x00" + struct.pack('d', 37) + struct.pack('d', 137)], type=pa.binary(length=25)),
  ]
  table = pa.table(data, names=[f'col{i}' for i in range(6)])
  orc_writer.write(table)

test.orc 自体は中身空で作成されました(消して実行すると再作成されます)


書き込み方があってるのかも不明です
https://arrow.apache.org/docs/python/generated/pyarrow.orc.ORCWriter.html#pyarrow.orc.ORCWriter.write
こちらをみると引数が table みたいなので

https://arrow.apache.org/docs/python/getstarted.html
こちらの getstart を参考にして table を作ってみました

最終的に作りたい Athena のカラム型が
string,double,int,timestamp,array,geom:binary
の 6 つで
https://arrow.apache.org/docs/python/api/datatypes.html
こちらをみながらテストデータを書き込んだ次第です
特に timestamp, string array, binary の与え方はあってるか自信ないですが
``
['x1', 'y1']
b"\x00\x00\x00\x00\x01\x01\x00\x00\x00" + struct.pack('d', 37) + struct.pack('d', 137)
の値自体は pyorc という別のモジュールを使って生成した orc では Athena 上で読めていました

1 件の回答 1

0

処理の目的が tableの書き込みであれば, 以下のようにできます

import pyarrow as pa
import pyarrow.orc as orc # ⇐ 追加
import pandas as pd
import struct

df = pd.DataFrame({
    'col0': ['abc'],
    'col1': [10],
    'col2': [1.5],
    'col3': [1646200394],
    'col4': [['x1', 'y1']],
    'col5': [b"\x00\x00\x00\x00\x01\x01\x00\x00\x00"
             + struct.pack('d', 37) + struct.pack('d', 137)],
    }).astype(
        {'col1': 'int32', 'col3': 'datetime64[s]'})

# table 準備とファイルへの書き込み
table = pa.Table.from_pandas(df)
display(table)
orc.write_table(table, 'test.orc')

# orc を pandasで読み取ってみる
df = pd.read_orc('test.orc')
display(df)

table & DataFrame


追記

DataFrameを pyorc使って書き込む場合は, 以下の方法で可能なはず

with open('test.orc', 'wb') as orc_file:
    writer = pyorc.Writer(orc_file,
        'struct<col0:string,col1:int,col2:double,col3:timestamp,col4:array<string>,col5:binary>',
        struct_repr=pyorc.StructRepr.DICT)
    writer.writerows(df.to_dict(orient="records"))
    writer.close()

この質問に回答するには、ログインする必要があります。