熊猫数据框类型datetime64 [ns]在Hive / Athena中不起作用

pra*_*ads 6 python hive pandas amazon-athena fastparquet

我正在使用一个python应用程序,它将csv文件转换为hive / athena兼容的拼花格式,并且我正在使用fastparquet和pandas库执行此操作。在csv文件中有时间戳值,例如2018-12-21 23:45:00需要timestamp在镶木地板文件中将其写入类型。以下是我正在运行的代码,

columnNames = ["contentid","processed_time","access_time"]

dtypes = {'contentid': 'str'}

dateCols = ['access_time', 'processed_time']

s3 = boto3.client('s3')

obj = s3.get_object(Bucket=bucketname, Key=keyname)

df = pd.read_csv(io.BytesIO(obj['Body'].read()), compression='gzip', header=0, sep=',', quotechar='"', names = columnNames, error_bad_lines=False, dtype=dtypes, parse_dates=dateCols)

s3filesys = s3fs.S3FileSystem()

myopen = s3filesys.open

write('outfile.snappy.parquet', df, compression='SNAPPY', open_with=myopen,file_scheme='hive',partition_on=PARTITION_KEYS)
Run Code Online (Sandbox Code Playgroud)

代码成功运行,下面是熊猫创建的数据框

contentid                 object
processed_time            datetime64[ns]
access_time               datetime64[ns]
Run Code Online (Sandbox Code Playgroud)

最后,当我在Hive和athena中查询实木复合地板文件时,时间戳记值+50942-11-30 14:00:00.000不是2018-12-21 23:45:00

任何帮助都受到高度赞赏

Dit*_*rne 8

我知道这个问题很旧,但仍然相关。

如前所述,Athena 仅支持 int96 作为时间戳。使用 fastparquet 可以生成 Athena 格式正确的 parquet 文件。重要的部分是 times='int96' 因为这告诉 fastparquet 将 pandas 日期时间转换为 int96 时间戳。

from fastparquet import write
import pandas as pd

def write_parquet():
  df = pd.read_csv('some.csv')
  write('/tmp/outfile.parquet', df, compression='GZIP', times='int96')
Run Code Online (Sandbox Code Playgroud)


Ama*_*ngh -1

我也遇到了同样的问题,经过大量研究,现在已经解决了。

当你这样做时

write('outfile.snappy.parquet', df, compression='SNAPPY', open_with=myopen,file_scheme='hive',partition_on=PARTITION_KEYS)
Run Code Online (Sandbox Code Playgroud)

它在幕后使用 fastparquet,它使用与 Athena 兼容的不同的 DateTime 编码。

解决方案是:卸载fastparquet并安装pyarrow

  • pip 卸载 fastparquet
  • pip 安装 pyarrow

再次运行您的代码。这次应该可以了。:)