Pyathena to_sql 创建空表

发布于 2025-01-09 04:26:02 字数 503 浏览 1 评论 0原文

我试图将 df 写入 Athena,但创建的表始终为空。我使用python 3.8和windows 11系统。我使用 pyathena 将数据帧写入 Athena,但到目前为止从未出现过问题。

from pyathena import connect
from pyathena.pandas.util import to_sql

conn = connect(aws_access_key_id="KEY",
           aws_secret_access_key="SKEY",
           s3_staging_dir="STAGINGDIR",  # query location dir
           region_name="eu-central-1")

to_sql(df, 
   "TABLENAME", 
   conn, 
   "MYS3PATH",
   schema="MYSCHEMA", 
   index=False, 
   if_exists="replace"
   )

I am trying to write a df into Athena, but the created table is always empty. I use python 3.8 and windows 11 system. I use pyathena writing dataframes to Athena but problems have never occurred till now.

from pyathena import connect
from pyathena.pandas.util import to_sql

conn = connect(aws_access_key_id="KEY",
           aws_secret_access_key="SKEY",
           s3_staging_dir="STAGINGDIR",  # query location dir
           region_name="eu-central-1")

to_sql(df, 
   "TABLENAME", 
   conn, 
   "MYS3PATH",
   schema="MYSCHEMA", 
   index=False, 
   if_exists="replace"
   )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠魁 2025-01-16 04:26:02

当您在 S3 路径末尾遗漏“/”时,通常会发生这种情况。
我通常在将 var 传递给 to_sql 之前使用一个函数来格式化 s3 路径,无论如何只是为了确定。

def format_s3_path(s3_path):
    if strip(str(s3_path))[-1] != '/':
        return str(s3_path) + '/'
    return str(s3_path)
to_sql(df, 
   "TABLENAME", 
   conn, 
   format_s3_path("MYS3PATH"),
   schema="MYSCHEMA", 
   index=False, 
   if_exists="replace"
   )

这应该有效。

This usually happens when you miss a '/' at the end of the S3 Path.
I usually use a function to format the s3 path at right before passing the var to to_sql no matter what just to be sure.

def format_s3_path(s3_path):
    if strip(str(s3_path))[-1] != '/':
        return str(s3_path) + '/'
    return str(s3_path)
to_sql(df, 
   "TABLENAME", 
   conn, 
   format_s3_path("MYS3PATH"),
   schema="MYSCHEMA", 
   index=False, 
   if_exists="replace"
   )

This should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文