这个想法是使用 bq 工具为 R 会话提供存储在 BigQuery 系统中的 csv 格式数据。bq 工具的输出显示数据以及不需要的消息,例如作业状态和更新的警告消息。是否有任何标志,因此 bq 工具只会返回 csv 中的查询数据而没有任何其他消息?
> cmdbq <- "bq --format=csv query \"SELECT id, SEC_TO_TIMESTAMP(timestamp) AS time FROM publicdata:samples.wikipedia LIMIT 5\""
> r1 <- shell(cmdbq,intern=TRUE)
> r1
[1] "\rWaiting on bqjob_r1b11c1f4_00000144c13cea58_1 ... (0s) Current status: DONE " "id,time"
[3] "18524,2009-08-03 09:13:51" "2252318,2008-11-24 00:41:42"
[5] "3430772,2007-01-11 05:43:19" "11170233,2007-05-11 12:13:36"
[7] "1008805,2007-06-19 20:34:13" ""
[9] "There are available updates for some Cloud SDK components. To " "install them, please run:"
[11] " $ gcloud components update" ""
> r2 <- read.csv(text=paste(r1[2:7],collapse="\n"),stringsAsFactors=FALSE)
> r2
id time
1 18524 2009-08-03 09:13:51
2 2252318 2008-11-24 00:41:42
3 3430772 2007-01-11 05:43:19
4 11170233 2007-05-11 12:13:36
5 1008805 2007-06-19 20:34:13
>
注意:我知道 bigrquery 包的存在,但我想使用这种方法,因为这部分代码将在 foreach 并行循环中运行,它简化了每个节点的身份验证步骤。