【问题标题】:Can I write to a Athena table on s3 using DBI?我可以使用 DBI 在 s3 上写入 Athena 表吗?
【发布时间】:2020-06-18 14:16:12
【问题描述】:

我与 Athena 建立了 odbc 连接,并且已经能够读取和检索数据。例如,我在 hive 中创建了一个新的空表,它使用与 Athena 的 s3 相同的元存储:

CREATE EXTERNAL TABLE IF NOT EXISTS adhoc.mtcars
(
mpg integer,
cyl integer,
disp integer,
hp integer,
drat integer,
wt integer,
qsec integer,
vs integer,
am integer,
gear integer,
carb integer)
partitioned by (year string, month string, day string)
stored as orc
location 's3://ourco-emr/tables/adhoc.db/mtcars';

我可以使用 DBI::dbReadTable 读取这个新的空表:

con <- dbConnect(odbc(), "Athena")
dbReadTable(con, DBI::Id(schema = "adhoc", table = "mtcars"))

返回:

 [1] mpg   cyl   disp  hp    drat  wt    qsec  vs    am    gear  carb  year  month day  
<0 rows> (or 0-length row.names)

所以,空表清晰可见。

在 hive create table 中注意以上内容:

location 's3://ourco-emr/tables/adhoc.db/mtcars'

此表的数据应存储在该位置的 s3 中。

我尝试使用 dbWriteTable 将 mtcars 写入此位置:

dbWriteTable(conn = con,
             name = "tables/adhoc.db/mtcars",
             value = mtcars,
             overwrite = FALSE,
             append = TRUE,
             file.type = "orc",
             partition = c(year = "2020", month = "02", day = "01"),
             s3.location =  "s3://ourco-emr/tables/adhoc.db/mtcars/mtcars")

这似乎在返回此错误消息之前运行了几秒钟:

错误:nanodbc/nanodbc.cpp:1617: 00000: [Simba][Athena] (1040) AWS Athena 客户端引发错误。 Athena 错误编号:130,HTTP 响应代码:400,异常名称:InvalidRequestException,错误消息:第 1:14 行:输入“CREATE TABLE“tables/adhoc.db/mtcars”'[执行 ID:] 处没有可行的替代方案 '创建表“表/adhoc.db/mtcars”( "row_names" VARCHAR(255), “mpg”双精度, “圆柱”双精度, “disp”双精度, “马力”双精度, “drat”双精度, “wt”双精度, “qsec”双精度, “vs”双精度, “我”双精度, “齿轮”双精度, “碳水化合物”双精度 )

看起来 dbi 正在尝试创建一个新表,我只想在其中追加到现有表,尽管我之前创建的表是空的。

如何使用 DBI 向 s3 发送数据帧?

【问题讨论】:

    标签: r amazon-s3 dbi rodbc


    【解决方案1】:

    我无法评论 odbc,但有两个软件包 RAthenanoctua 具有用于将数据上传到 AWS Athena 的 DBI 方法。

    RAthena 利用 Python SDK boto3 创建到 AWS 的连接。 noctua 利用 R SDK paws 创建到 AWS 的连接。

    library(DBI)
    
    # connect to AWS Athena using RAthena
    con = dbConnect(RAthena::athena())
    
    # OR connect to AWS Athena using noctua
    con = dbConnect(noctua::athena())
    
    # Uploading to existing AWS Athena table
    dbWriteTable(conn = con,
                 name = "adhoc.mtcars",
                 value = mtcars,
                 append = TRUE,
                 file.type = "parquet",
                 partition = c(year = "2020", month = "02", day = "01"),
                 s3.location =  "s3://ourco-emr/tables/")
    
    dbGetQuery(con, "select * from adhoc.iris")
    

    目前这些包在上传到 AWS Athena 时仅支持 file.types ["tsv", "csv", "parquet"]。要扩展当前软件包的功能,请通过https://github.com/DyfanJones/RAthena/issueshttps://github.com/DyfanJones/noctua/issues 提出功能请求。

    注意:不要在相同的环境中加载两个包,因为连接类会发生冲突。

    【讨论】:

    • 我能够使用 noctua 包和您的示例代码块来完成这项工作。谢谢!
    猜你喜欢
    • 2021-12-04
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2023-04-02
    • 2020-04-28
    • 2018-07-19
    相关资源
    最近更新 更多