【问题标题】:Use pyarrow and json.dump to save json file in hdfs使用 pyarrow 和 json.dump 将 json 文件保存在 hdfs 中
【发布时间】:2019-04-23 11:35:51
【问题描述】:

我正在尝试使用 pyarrow 将 json 文件保存在 HDFS 中。这是我的代码的样子。

from pyarrow import hdfs
fs = hdfs.connect(driver='libhdfs')
with fs.open(outputFileVal1, 'wb') as fp:
    json.dump(list(value1set), fp)

这给出了一个错误提示TypeError: a bytes-like object is required, not 'str'

当我尝试使用 joblib.dump 或 pickle.dump 时,它可以工作,但它不会以 json 格式保存。有没有办法使用pyarrow将json文件直接保存到hdfs。

【问题讨论】:

    标签: python json hdfs pyarrow


    【解决方案1】:

    看起来您可能需要使用包装器,将json.dump 写入的数据编码为二进制,使用chunk.encode('utf8')。类似的东西

    class Utf8Encoder(object);
    
        def __init__(self, fp):
            self.fp = fp
    
        def write(self, data):
            if not isinstance(data, bytes):
                data = data.encode('utf-8')
            self.fp.write(data)
    

    那你就可以写了

    json.dump(..., UtfEncoder(fp))
    

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 1970-01-01
      • 2017-11-03
      • 2020-03-18
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2021-09-17
      相关资源
      最近更新 更多