-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtables.R
More file actions
468 lines (406 loc) · 15 KB
/
tables.R
File metadata and controls
468 lines (406 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#' Copy BigQuery table
#'
#' Copy a source table to another destination
#'
#' @param source_projectid source table's projectId
#' @param source_datasetid source table's datasetId
#' @param source_tableid source table's tableId
#' @param destination_projectid destination table's projectId
#' @param destination_datasetid destination table's datasetId
#' @param destination_tableid destination table's tableId
#' @param createDisposition Create table's behaviour
#' @param writeDisposition Write to an existing table's behaviour
#'
#' @return A job object
#'
#' @export
#' @import assertthat
#' @family Table meta functions
bqr_copy_table <- function(source_tableid,
destination_tableid,
source_projectid = bqr_get_global_project(),
source_datasetid = bqr_get_global_dataset(),
destination_projectid = bqr_get_global_project(),
destination_datasetid = bqr_get_global_dataset(),
createDisposition = c("CREATE_IF_NEEDED","CREATE_NEVER"),
writeDisposition = c("WRITE_TRUNCATE", "WRITE_APPEND", "WRITE_EMPTY")){
createDisposition <- match.arg(createDisposition)
writeDisposition <- match.arg(writeDisposition)
labels <- check_labels(getOption("bigQueryR.labels"))
assert_that(
is.string(source_projectid),
is.string(source_datasetid),
is.string(source_tableid),
is.string(destination_projectid),
is.string(destination_datasetid),
is.string(destination_tableid)
)
config <- list(
configuration = list(
copy = list(
createDisposition = createDisposition,
sourceTable = list(
projectId = source_projectid,
datasetId = source_datasetid,
tableId = source_tableid
),
destinationTable = list(
projectId = destination_projectid,
datasetId = destination_datasetid,
tableId = destination_tableid
),
writeDisposition = writeDisposition
),
labels = labels
)
)
myMessage(sprintf("Copying table %s.%s.%s to %s.%s.%s",
source_projectid, source_datasetid, source_tableid,
destination_projectid,destination_datasetid, destination_tableid),
level = 3)
call_job(source_projectid, config = config)
}
#' List BigQuery tables in a dataset
#'
#' @param projectId The BigQuery project ID
#' @param datasetId A datasetId within projectId
#' @param maxResults Number of results to return, default \code{-1} returns all results
#'
#' @return dataframe of tables in dataset
#'
#' @examples
#'
#' \dontrun{
#' bqr_list_tables("publicdata", "samples")
#' }
#'
#' @family Table meta functions
#' @import assertthat
#' @importFrom googleAuthR gar_api_generator gar_api_page
#' @export
bqr_list_tables <- function(projectId = bqr_get_global_project(),
datasetId = bqr_get_global_dataset(),
maxResults = -1){
assert_that(is.string(projectId),
is.string(datasetId),
is.scalar(maxResults))
# support -1 for all results
if(maxResults < 0){
maxResults=NULL
}
pars <- list(maxResults = maxResults,
pageToken = "")
pars <- rmNullObs(pars)
check_bq_auth()
l <- gar_api_generator("https://bigquery.googleapis.com/bigquery/v2",
"GET",
path_args = list(projects = projectId,
datasets = datasetId,
tables = ""),
pars_args = pars,
data_parse_function = parse_bqr_list_tables)
pages <- gar_api_page(l,
page_f = get_attr_nextpagetoken,
page_method = "param",
page_arg = "pageToken")
Reduce(rbind, pages)
}
parse_bqr_list_tables <- function(x) {
d <- x$tables
out <- data.frame(id = d$id,
projectId = d$tableReference$projectId,
datasetId = d$tableReference$datasetId,
tableId = d$tableReference$tableId, stringsAsFactors = FALSE)
if(!is.null(x$nextPageToken)){
attr(out, "nextPageToken") <- x$nextPageToken
}
out
}
#' Get BigQuery Table meta data
#'
#' @param projectId The BigQuery project ID
#' @param datasetId A datasetId within projectId
#' @param tableId The tableId within the datasetId
#'
#' @return list of table metadata
#'
#' @examples
#'
#' \dontrun{
#' bqr_table_meta("publicdata", "samples", "github_nested")
#' }
#'
#'
#' @family Table meta functions
#' @export
bqr_table_meta <- function(projectId = bqr_get_global_project(),
datasetId = bqr_get_global_dataset(),
tableId){
check_bq_auth()
f <- function(x){
x <- rmNullObs(x)
}
l <- googleAuthR::gar_api_generator("https://bigquery.googleapis.com/bigquery/v2",
"GET",
path_args = list(projects = projectId,
datasets = datasetId,
tables = tableId),
data_parse_function = f)
res <- l(path_arguments = list(projects = projectId,
datasets = datasetId,
tables = tableId))
as.table(res)
}
#' Get BigQuery Table's data list
#'
#' @param projectId The BigQuery project ID
#' @param datasetId A datasetId within projectId
#' @param tableId The tableId within the datasetId
#' @param maxResults Number of results to return
#'
#' @return data.frame of table data
#'
#' This won't work with nested datasets, for that use \link{bqr_query} as that flattens results.
#'
#' @family Table meta functions
#' @export
bqr_table_data <- function(projectId = bqr_get_global_project(),
datasetId = bqr_get_global_dataset(),
tableId,
maxResults = 1000){
check_bq_auth()
l <- googleAuthR::gar_api_generator("https://bigquery.googleapis.com/bigquery/v2",
"GET",
path_args = list(projects = projectId,
datasets = datasetId,
tables = tableId,
data = ""),
pars_args = list(maxResults = maxResults),
data_parse_function = function(x) x)
l(path_arguments = list(projects = projectId,
datasets = datasetId,
tables = tableId),
pars_arguments = list(maxResults = maxResults))
}
#' Create a Table
#'
#' @param projectId The BigQuery project ID.
#' @param datasetId A datasetId within projectId.
#' @param tableId Name of table you want.
#' @param template_data A dataframe with the correct types of data. If \code{NULL} an empty table is made.
#' @param timePartitioning Whether to create a partioned table
#' @param expirationMs If a partioned table, whether to have an expiration time on the data. The default \code{0} is no expiration.
#'
#' @return TRUE if created, FALSE if not.
#'
#' @details
#'
#' Creates a BigQuery table.
#'
#' If setting \code{timePartioning} to \code{TRUE} then the table will be a
#' \href{https://cloud.google.com/bigquery/docs/creating-partitioned-tables}{partioned table}
#'
#' If you want more advanced features for the table, create it then call \link{bqr_patch_table} with advanced configuration configured from \link{Table}
#'
#' @family Table meta functions
#' @export
bqr_create_table <- function(projectId = bqr_get_global_project(),
datasetId = bqr_get_global_dataset(),
tableId,
template_data = NULL,
timePartitioning = FALSE,
expirationMs = 0L){
check_bq_auth()
l <- googleAuthR::gar_api_generator("https://bigquery.googleapis.com/bigquery/v2",
"POST",
path_args = list(projects = projectId,
datasets = datasetId,
tables = "")
)
expirationMs <- as.integer(expirationMs)
timeP <- NULL
if(timePartitioning){
if(expirationMs == 0) expirationMs <- NULL
timeP <- list(type = "DAY", expirationMs = expirationMs)
}
if(!is.null(template_data)){
schema <- list(
fields = schema_fields(template_data)
)
} else {
schema <- NULL
}
config <- list(
schema = schema,
tableReference = list(
projectId = projectId,
datasetId = datasetId,
tableId = tableId
),
timePartitioning = timeP
)
config <- rmNullObs(config)
req <- try(l(path_arguments = list(projects = projectId,
datasets = datasetId),
the_body = config), silent = TRUE)
if(is.error(req)){
if(grepl("Already Exists", error.message(req))){
message("Table exists: ", tableId, "Returning FALSE")
out <- FALSE
} else {
stop(error.message(req))
}
} else {
message("Table created: ", tableId)
out <- TRUE
}
out
}
#' Update a Table
#'
#' @param Table A Table object as created by \link{Table}
#'
#' @description
#' This uses PATCH semantics to alter an existing table.
#' You need to create the Table object first to pass in using \link{Table}
#'which will be transformed to JSON
#'
#' @export
#' @import assertthat
#' @importFrom googleAuthR gar_api_generator
#' @seealso \href{https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#resource}{Definition of tables}
#' @family Table meta functions
bqr_patch_table <- function(Table){
assert_that(
is.table(Table)
)
projectId <- Table$tableReference$projectId
datasetId <- Table$tableReference$datasetId
tableId <- Table$tableReference$tableId
myMessage("Patching ", tableId, level = 3)
the_url <- sprintf("https://bigquery.googleapis.com/bigquery/v2/projects/%s/datasets/%s/tables/%s",
projectId, datasetId, tableId)
call_api <- gar_api_generator(the_url, "PATCH", data_parse_function = function(x) x)
res <- call_api(the_body = Table)
as.table(res)
}
#' Delete a Table
#'
#' @param projectId The BigQuery project ID.
#' @param datasetId A datasetId within projectId.
#' @param tableId Name of table you want to delete.
#'
#' @return TRUE if deleted, FALSE if not.
#'
#' @details
#'
#' Deletes a BigQuery table
#'
#' @family Table meta functions
#' @export
bqr_delete_table <- function(projectId = bqr_get_global_project(),
datasetId = bqr_get_global_dataset(),
tableId){
check_bq_auth()
l <- googleAuthR::gar_api_generator("https://bigquery.googleapis.com/bigquery/v2",
"DELETE",
path_args = list(projects = projectId,
datasets = datasetId,
tables = tableId)
)
req <- try(suppressWarnings(l(path_arguments = list(projects = projectId,
datasets = datasetId,
tables = tableId))), silent = TRUE)
if(is.error(req)){
if(grepl("Not found", error.message(req))){
myMessage(error.message(req), level = 3)
out <- FALSE
} else {
stop(error.message(req))
}
} else {
out <- TRUE
}
out
}
#' Table Object
#'
#' Configure table objects as documented by
#' the \href{https://cloud.google.com/bigquery/docs/reference/rest/v2/tables}{Google docs for Table objects}
#'
#' @param tableId tableId
#' @param projectId projectId
#' @param datasetId datasetId
#' @param clustering [Beta] Clustering specification for the table
#' @param description [Optional] A user-friendly description of this table
#' @param encryptionConfiguration Custom encryption configuration (e
#' @param expirationTime [Optional] The time when this table expires, in milliseconds since the epoch
#' @param friendlyName [Optional] A descriptive name for this table
#' @param labels The labels associated with this table - a named list of key = value
#' @param materializedView [Optional] Materialized view definition
#' @param rangePartitioning [TrustedTester] Range partitioning specification for this table
#' @param requirePartitionFilter [Beta] [Optional] If set to true, queries over this table require a partition filter that can be used for partition elimination to be specified
#' @param schema [Optional] Describes the schema of this table
#' @param timePartitioning Time-based partitioning specification for this table
#' @param view [Optional] The view definition
#'
#' @return Table object
#'
#' @details
#'
#' A table object to be used within \link{bqr_patch_table}
#'
#' @family Table meta functions
#' @export
#' @import assertthat
Table <- function(tableId,
projectId = bqr_get_global_project(),
datasetId = bqr_get_global_dataset(),
clustering = NULL,
description = NULL,
encryptionConfiguration = NULL,
expirationTime = NULL,
friendlyName = NULL,
labels = NULL,
materializedView = NULL,
rangePartitioning = NULL,
requirePartitionFilter = NULL,
schema = NULL,
timePartitioning = NULL,
view = NULL) {
assert_that(
is.string(projectId),
is.string(datasetId),
is.string(tableId)
# is.string(friendlyName),
# is.string(description),
# is.list(labels),
# is.list(timePartitioning)
# is.flag(requirePartitionFilter)
)
labels <- if (!is.null(labels)) check_labels(labels) else check_labels(getOption("bigQueryR.labels"))
tt <- list(
tableReference = list(projectId = projectId,
datasetId = datasetId,
tableId = tableId),
clustering = clustering,
description = description,
encryptionConfiguration = encryptionConfiguration,
expirationTime = expirationTime,
friendlyName = friendlyName,
labels = labels,
materializedView = materializedView,
rangePartitioning = rangePartitioning,
requirePartitionFilter = NULL,
schema = schema,
timePartitioning = timePartitioning,
view = view)
tt <- rmNullObs(tt)
structure(tt, class = "gar_Table")
}
is.table <- function(x){
inherits(x, "gar_Table")
}
as.table <- function(x){
structure(x, class = "gar_Table")
}