【问题标题】:Handling Parquet Files in Python在 Python 中处理 Parquet 文件
【发布时间】:2015-07-22 10:07:07
【问题描述】:

我正在尝试在 Python 中处理来自 hive 的镶木地板表,并面临一些数据类型问题。例如,如果我的 hive parquet 表中有一个字段为

decimal (10,2) ,当我尝试在 python 中读取文件时,它给出了一个垃圾值。 请对此提供一些意见。

【问题讨论】:

  • 您如何从 Python 访问数据?您是否从 Python 连接到 Hive 并尝试读取以 parquet 格式存储的表?
  • 不,我们正在从外部表位置读取 hdfs 文件 ..

标签: python hive parquet


【解决方案1】:

我认为这可能会有所帮助,尽管这不是一个正确的答案。在我存储到 Parquet 之前,我的 PySpark 作业中有这个方法,例如,将小数转换为浮点数,以便它们在 Pandas DataFrames 中读取正常。在这种情况下,我正在缩小类型,但你明白了:

def shrink_types(df):
    """Reduce data size by shrinking the types"""

    # Loop through the data type tuples and downcast the column
    for t in df.dtypes:
        column_name = t[0]
        column_type = t[1]

        if column_type == 'double' or 'decimal' in column_type:
            df = df.withColumn(
                column_name,
                F.col(column_name).cast('float')
            )

return df

然后我通过以下方式调用它:

equities_df = shrink_types(equities_df)

# Save and restore so it actually runs
equities_df.write.mode('overwrite').parquet(
    path='s3://bucket/path/dataset.parquet',
)

【讨论】:

    猜你喜欢
    • 2017-12-16
    • 2017-04-24
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    相关资源
    最近更新 更多