将 dplyr 查询保存到 postgresql

2024-01-08

很抱歉问了这个非常简单的问题,但是......

如何保存查询结果构建dbplyr无需在内存中加载数据。

这就是我尝试过的。

library(DBI)
library(odbc)
library(RPostgreSQL)
library(tidyverse)
library(magrittr)
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract
library(dbplyr)
#> 
#> Attaching package: 'dbplyr'
#> The following objects are masked from 'package:dplyr':
#> 
#>     ident, sql

pgdrv <- dbDriver(drvName = "PostgreSQL")
con <-dbConnect(pgdrv,
               dbname="genomes",
               host="127.0.0.1", port=5432,
               user = 'rotifer')

mtcars %<>% 
  rownames_to_column()

dbWriteTable(con, "cars", mtcars)
#> [1] TRUE

dbmtcars <- tbl(con, "cars") 

dbmtcars %>% 
  mutate(ts = cyl * carb) -> newtb

newtb %>% show_query()
#> <SQL>
#> SELECT "row.names", "rowname", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb", "cyl" * "carb" AS "ts"
#> FROM "cars"

newtb %>% db_write_table(con, "newtb")
#> Error in UseMethod("db_write_table"): no applicable method for 'db_write_table' applied to an object of class "c('tbl_PostgreSQLConnection', 'tbl_dbi', 'tbl_sql', 'tbl_lazy', 'tbl')"

Created on 2020-10-09 by the reprex package https://reprex.tidyverse.org (v0.3.0)

提前致谢


我暂时找到了答案,我不知道如何解释,但它有效。

我刚用过db_compute and db_sql_render解决要执行的查询并保存在新表中。db_compute(con=con, sql=db_sql_render(con, newtb), table="newtb")

完整代码如下:

library(DBI)
library(odbc)
library(RPostgreSQL)
library(tidyverse)
library(magrittr)
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract
library(dbplyr)
#> 
#> Attaching package: 'dbplyr'
#> The following objects are masked from 'package:dplyr':
#> 
#>     ident, sql

pgdrv <- dbDriver(drvName = "PostgreSQL")
con <-dbConnect(pgdrv,
               dbname="genomes",
               host="127.0.0.1", port=5432,
               user = 'rotifer')

mtcars %<>% 
  rownames_to_column()
#> [1] TRUE

dbmtcars <- tbl(con, "cars") 

dbmtcars %>% 
  mutate(ts = cyl * carb) -> newtb

newtb %>% show_query()
#> <SQL>
#> SELECT "row.names", "rowname", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb", "cyl" * "carb" AS "ts"
#> FROM "cars"

db_compute(con=con, sql=db_sql_render(con, newtb), table="newtb")
#> [1] "newtb"
dbCommit(con)
#> [1] TRUE

sbpsigt <- tbl(con, "newtb")
sbpsigt
#> # Source:   table<newtb> [?? x 14]
#> # Database: postgres 10.0.14 [[email protected] /cdn-cgi/l/email-protection:5432/genomes]
#>    row.names rowname   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear
#>    <chr>     <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 1         Mazda …  21       6  160    110  3.9   2.62  16.5     0     1     4
#>  2 2         Mazda …  21       6  160    110  3.9   2.88  17.0     0     1     4
#>  3 3         Datsun…  22.8     4  108     93  3.85  2.32  18.6     1     1     4
#>  4 4         Hornet…  21.4     6  258    110  3.08  3.22  19.4     1     0     3
#>  5 5         Hornet…  18.7     8  360    175  3.15  3.44  17.0     0     0     3
#>  6 6         Valiant  18.1     6  225    105  2.76  3.46  20.2     1     0     3
#>  7 7         Duster…  14.3     8  360    245  3.21  3.57  15.8     0     0     3
#>  8 8         Merc 2…  24.4     4  147.    62  3.69  3.19  20       1     0     4
#>  9 9         Merc 2…  22.8     4  141.    95  3.92  3.15  22.9     1     0     4
#> 10 10        Merc 2…  19.2     6  168.   123  3.92  3.44  18.3     1     0     4
#> # … with more rows, and 2 more variables: carb <dbl>, ts <dbl>

dbDisconnect(con)
#> [1] TRUE

Created on 2020-10-09 by the reprex package https://reprex.tidyverse.org (v0.3.0)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 dplyr 查询保存到 postgresql 的相关文章

  • 如何自动启动我的 ec2 实例、运行命令然后将其关闭?

    我想每周对 redshift postgres 数据库中的数据运行一次机器学习模型 我使用以下命令将 R 脚本设置为休息 apiplumbr然后我将其设置为一项任务来管理pm2 我有它 所以任务会在ec2实例启动然后继续运行 要让 R 脚本
  • postgreSQL 在 WAMP 上的集成

    我刚刚在 Windows 7 上安装了 postgreSQL 我正在尝试将 postgreSQL 与 WAMP 服务器集成 为此 我在 httpd conf 和 php ini 文件中进行了以下更改 1个加载模块c path to libp
  • 合并数据框而不重复行

    我想合并两个数据框 但如果有多个匹配项 则不想重复行 相反 我想总结一下那天的观察结果 来自 合并 提取两个数据框中与指定列匹配的行并将其连接在一起 如果有多个匹配项 则所有可能的匹配项各贡献一行 这是一些示例代码 days lt as d
  • R- 将某些列从 0 标准化为 1,其值等于 0

    我最近开始使用 are 我想扩展我的数据矩阵 我在这里找到了一种方法在两点之间缩放系列 https stackoverflow com questions 5468280 scale a series between two points
  • 使用 NLog .NET Core 将日志记录到 PostgreSQL DB

    我尝试将日志记录集成到 NET Core 中的数据库 我能够设置 NLog 并将消息记录到 SQL Server 这很容易 但是当我尝试将 DB 切换到 PostgreSQL 时 似乎没有记录任何内容 以下是startup cs中的代码 p
  • 对 data.table 中的列表列执行操作

    假设我有一个data table 例如dt lt data table foo list 1 3 4 6 bar c 2 7 如何使用 dt 框架对 foo 向量列表执行操作 操作可能是将 bar 添加到 foo 返回列表 3 5 11 1
  • 融化R中的下半矩阵

    如何融化下半三角形加对角矩阵 11 NA NA NA NA 12 22 NA NA NA 13 23 33 NA NA 14 24 34 44 NA 15 25 35 45 55 A lt t matrix c 11 NA NA NA NA
  • dplyr 返回每个组的全局平均值,而不是每个组的平均值

    有人可以解释一下我在这里做错了什么 library dplyr temp lt data frame a c 1 2 3 1 2 3 1 2 3 b c 1 2 3 1 2 3 1 2 3 temp gt group by temp 1 g
  • 如何在knitr和RStudio中为word和html设置不同的全局选项?

    我正在使用 RStudio 0 98 932 和 knitr 1 6 想要为word和html设置不同的全局knitr选项 例如 想要将word的fig width和fig height设置为6 html的fig width和fig hei
  • 为 PostgreSQL 查询选择正确的索引

    简化表 CREATE TABLE products product no integer PRIMARY KEY sales integer status varchar 16 category varchar 16 CREATE INDE
  • R中的字典数据结构

    在 R 中 我有 例如 gt foo lt list a 1 b 2 c 3 如果我输入foo I get a 1 1 b 1 2 c 1 3 我怎样才能看透foo仅获取 键 列表 在这种情况下 a b c R 列表可以具有命名元素 因此可
  • 将字符串列拆分为多个虚拟变量

    作为 R 中 data table 包的相对缺乏经验的用户 我一直在尝试将一个文本列处理为大量指示符列 虚拟变量 每列中的 1 表示特定的子字符串是在字符串列中找到 例如我想处理这个 ID String 1 a b 2 b c 3 c 进入
  • 将 ftransform 与折叠 R 包中的 fgroup_by 一起使用

    我正在尝试重现以下输出dplyr代码与R包裹collapse dplyr Code library tidyverse starwars gt select name mass species gt group by species gt
  • 使用 PostGIS 拥有通用 GEOGRAPHY 列

    在 PostgreSQL 9 PostGIS 1 5 中 有没有办法创建 通用 GEOGRAPHY 列 我指的是接受 POINT 以及 POLYGON LINESTRING 等的列 到目前为止 我只在 Internet 上看到过像 GEOG
  • 如何加速spark df.write jdbc到postgres数据库?

    我是 Spark 新手 正在尝试使用 df write 加速将数据帧的内容 可以有 200k 到 2M 行 附加到 postgres 数据库 df write format jdbc options url psql url spark d
  • 在Python中检索PostgreSQL数据库的新记录

    在数据库表中 第二列和第三列有数字 将会不断添加新行 每次 每当数据库表中添加新行时 python 都需要不断检查它们 当 sql 表中收到的新行数低于 105 时 python 应打印一条通知消息 警告 数量已降至 105 以下 另一方面
  • 在R中循环子文件夹

    我正在 R 环境中包含多个子文件夹的文件夹中工作 我想要循环遍历多个子文件夹 然后在每个子文件夹中调用 R 脚本来执行 我想出了下面的代码 但我的代码似乎添加了 到子文件夹列表 我收到错误 文件中的错误 文件名 r 编码 编码 无效的 描述
  • Django 模型:默认日期时间未转换为 SQL CURRENT_TIMESTAMP

    我正在使用 Django 模型创建 PostgreSQL DB 我有一个 DateTimeField 我想将当前时间戳设置为默认值 我知道有多个消息来源建议如何做到这一点 但是 当我在 Django 之外检查数据库时 默认时间戳不会显示 我
  • 实现 XGboost 自定义目标函数

    我正在尝试使用 XGboost 实现自定义目标函数 在 R 中 但我也使用 python 所以有关 python 的任何反馈也很好 我创建了一个返回梯度和粗麻布的函数 它工作正常 但是当我尝试运行 xgb train 时它不起作用 然后 我
  • 如何按用户定义(例如非字母顺序)对数据框进行排序[重复]

    这个问题在这里已经有答案了 给定一个数据框dna gt dna chrom start chr2 39482 chr1 203918 chr1 198282 chrX 7839028 chr17 3874 以下代码重新排序dna by ch

随机推荐