简体   繁体   English

BigRQuery 如何更新谷歌大查询表中的列?

[英]BigRQuery How to update a column in a table on google big query?

I am struggling to update a table in Google BigQuery using the BigRQuery package in R. I have only managed to patch the new column names but not to update the values.我正在努力使用 R 中的 BigRQuery 包更新 Google BigQuery 中的表。我只设法修补了新的列名,但没有更新值。

Here is my code so far:到目前为止,这是我的代码:

library(bigrquery)
library(tidyverse)
projectId = "ABCD"
datasetId = "test"
tableId = 'table1'

mydf = mpg #just loading an example dataset

x <- list(projectId = projectId,
          datasetId = datasetId,
          tableId   = tableId)

upload a table for the first time第一次上传表格

bq_table_upload(x, values = mydf, fields = as_bq_fields(mydf))

now add a column to mydf现在向 mydf 添加一列

mydf$new_column = 0

and try to upload it并尝试上传

bq_table_upload(x, values = mydf, fields = as_bq_fields(mydf)) 

this gives an error that the table already exists.这给出了该表已存在的错误。

I can add a new column using我可以使用添加一个新列

bq_table_patch(x, fields = as_bq_fields(mydf))

But it is created with "Null" values everywhere, I do not understand how to upload the values (in this case they should be 0) in "new_column"但是它到处都是用“Null”值创建的,我不明白如何在“new_column”中上传值(在这种情况下它们应该是 0)

Any help would be greatly appreciated!任何帮助将不胜感激!

I found out that one can do it with DBI.我发现可以用 DBI 做到这一点。

library(DBI)
DBI: dbWRiteTable

First we set up the connection:首先我们建立连接:

bq_conn <- dbConnect(bigquery(),
project = projectId,
dataset = datasetId)

and then接着

DBI::dbWriteTable(conn = bq_conn,
                  name = tableId,
                  value = df, 
                  as_bq_fields(df),
                  overwrite = TRUE, 
                  append = TRUE)

where df is a new dataset that contains new rows with at least some values for the existing columns.其中df是一个新数据集,其中包含至少具有现有列的某些值的新行。 Append and Overwrite can be chosen TRUE/FALSE as needed, of course.当然,Append 和 Overwrite 可以根据需要选择 TRUE/FALSE。 Hope it helps someone (I welcome other solutions/suggestions to make it better - I am a newbie in GCP and R).希望它对某人有所帮助(我欢迎其他解决方案/建议让它变得更好——我是 GCP 和 R 的新手)。 My googling efforts resulted only in finding one unanswered question: https://community.rstudio.com/t/bigrquery-how-to-update-tables/31533我的谷歌搜索结果只找到了一个未解决的问题: https ://community.rstudio.com/t/bigrquery-how-to-update-tables/31533

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM