我正在尝试将s3的orc文件读取到Pandas数据帧中。在我的熊猫版本中,没有pd.read_orc(...)。
我试图这样做:
session = boto3.Session()
s3_client = session.client('s3')
s3_key = "my_object_key"
data = s3_client.get_object(
Bucket='my_bucket',
Key=s3_key
)
orc_bytes = data['Body'].read()
哪个对象以字节为单位读取。
现在我尝试这样做:
orc_data = pyorc.Reader(orc_bytes)
但是它失败了,因为:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-deaabe8232ce> in <module>
----> 1 data = pyorc.Reader(orc_data)
/anaconda3/envs/linear_opt_3.7/lib/python3.7/site-packages/pyorc/reader.py in __init__(self, fileo, batch_size, column_indices, column_names, struct_repr, converters)
65 conv = converters
66 super().__init__(
---> 67 fileo, batch_size, column_indices, column_names, struct_repr, conv
68 )
69
TypeError: Parameter must be a file-like object, but `<class 'bytes'>` was provided
最终,我希望将其作为.csv或我可以读入大熊猫的东西。有更好的方法吗?
答案 0 :(得分:1)
尝试将S3数据包装在io.BytesIO
中:
import io
orc_bytes = io.BytesIO(data['Body'].read())
orc_data = pyorc.Reader(orc_bytes)
答案 1 :(得分:0)
以下是端到端解决问题的功能:
import boto3
import pyorc
import io
import pandas as pd
session = boto3.Session()
s3_client = session.client('s3')
def load_s3_orc_to_local_df(key, bucket):
data = s3_client.get_object(Bucket=bucket, Key=key)
orc_bytes = io.BytesIO(data['Body'].read())
reader = pyorc.Reader(orc_bytes)
schema = reader.schema
columns = [item for item in schema.fields]
rows = [row for row in reader]
df = pd.DataFrame(data=rows, columns=columns)
return df