3

我正在使用 python 和 pyarrow 库,我想在 HDFS 上编写一个 pandas 数据框。这是我的代码

import pandas as pd
import pyarrow as pa

fs = pa.hdfs.connect(namenode, port, username, kerb_ticket)
df = pd.DataFrame(...)
table = pa.Table.from_pandas(df)

根据文档,我应该使用以下代码在 HDFS 上编写 pyarrow.Table

import pyarrow.parquet as pq
pq.write_parquet(table, 'filename.parquet')

我不明白我应该在哪里使用我的连接(fs),因为如果我不使用它,write_parquet那么它怎么知道 HDFS 在哪里?

4

2 回答 2

1

你可以这样做

with fs.open(path, 'wb') as f:
   pq.write_parquet(table, f)

我打开了一个关于添加更多关于此的文档的 JIRA

https://issues.apache.org/jira/browse/ARROW-6239

于 2019-08-14T20:07:25.843 回答
1

基于文档:https ://arrow.apache.org/docs/python/api/formats.html#parquet-files

您可以使用 write_table 或 write_to_dataset 函数:

写表

write_table 接受多个参数,其中几个参数如下:

table -> pyarrow.Table
where -> this can be a string or the filesystem object
filesystem -> Default is None

例子

pq.write_table(table, path, filesystem = fs)

或者

with fs.open(path, 'wb') as f:
    pq.write_table(table, f)

write_to_dataset

如果要根据表中的特定列对数据进行分区,可以使用 write_to_dataset,例如:

pq.write_to_dataset(table, path, filesystem = fs, partition_cols = [col1])
于 2019-08-14T21:08:45.560 回答