lib/google/cloud/bigquery/project.rb in google-cloud-bigquery-0.26.0 vs lib/google/cloud/bigquery/project.rb in google-cloud-bigquery-0.27.0
- old
+ new
@@ -20,10 +20,11 @@
require "google/cloud/bigquery/dataset"
require "google/cloud/bigquery/job"
require "google/cloud/bigquery/query_data"
require "google/cloud/bigquery/project/list"
require "google/cloud/bigquery/time"
+require "google/cloud/bigquery/schema"
module Google
module Cloud
module Bigquery
##
@@ -153,10 +154,12 @@
#
# * `truncate` - BigQuery overwrites the table data.
# * `append` - BigQuery appends the data to the table.
# * `empty` - A 'duplicate' error is returned in the job result if the
# table exists and contains data.
+ # @param [Dataset, String] dataset The default dataset to use for
+ # unqualified table names in the query. Optional.
# @param [Boolean] large_results If `true`, allows the query to produce
# arbitrarily large result tables at a slight cost in performance.
# Requires `table` parameter to be set.
# @param [Boolean] standard_sql Specifies whether to use BigQuery's
# [standard
@@ -178,10 +181,19 @@
# to be set.
# @param [Boolean] flatten This option is specific to Legacy SQL.
# Flattens all nested and repeated fields in the query results. The
# default value is `true`. `large_results` parameter must be `true` if
# this is set to `false`.
+ # @param [Integer] maximum_billing_tier Limits the billing tier for this
+ # job. Queries that have resource usage beyond this tier will fail
+ # (without incurring a charge). Optional. If unspecified, this will be
+ # set to your project default. For more information, see [High-Compute
+ # queries](https://cloud.google.com/bigquery/pricing#high-compute).
+ # @param [Integer] maximum_bytes_billed Limits the bytes billed for this
+ # job. Queries that will have bytes billed beyond this limit will fail
+ # (without incurring a charge). Optional. If unspecified, this will be
+ # set to your project default.
#
# @return [Google::Cloud::Bigquery::QueryJob]
#
# @example Query using standard SQL:
# require "google/cloud/bigquery"
@@ -192,11 +204,11 @@
# "`my_project.my_dataset.my_table`"
#
# job.wait_until_done!
# if !job.failed?
# job.query_results.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
# end
#
# @example Query using legacy SQL:
# require "google/cloud/bigquery"
@@ -208,11 +220,11 @@
# legacy_sql: true
#
# job.wait_until_done!
# if !job.failed?
# job.query_results.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
# end
#
# @example Query using positional query parameters:
# require "google/cloud/bigquery"
@@ -225,11 +237,11 @@
# params: [1]
#
# job.wait_until_done!
# if !job.failed?
# job.query_results.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
# end
#
# @example Query using named query parameters:
# require "google/cloud/bigquery"
@@ -242,24 +254,28 @@
# params: { id: 1 }
#
# job.wait_until_done!
# if !job.failed?
# job.query_results.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
# end
#
def query_job query, params: nil, priority: "INTERACTIVE", cache: true,
table: nil, create: nil, write: nil, dataset: nil,
standard_sql: nil, legacy_sql: nil, large_results: nil,
- flatten: nil
+ flatten: nil, maximum_billing_tier: nil,
+ maximum_bytes_billed: nil
ensure_service!
options = { priority: priority, cache: cache, table: table,
create: create, write: write,
large_results: large_results, flatten: flatten,
dataset: dataset, legacy_sql: legacy_sql,
- standard_sql: standard_sql, params: params }
+ standard_sql: standard_sql,
+ maximum_billing_tier: maximum_billing_tier,
+ maximum_bytes_billed: maximum_bytes_billed,
+ params: params }
gapi = service.query_job query, options
Job.from_gapi gapi, service
end
##
@@ -354,11 +370,11 @@
#
# sql = "SELECT name FROM `my_project.my_dataset.my_table`"
# data = bigquery.query sql
#
# data.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
#
# @example Query using legacy SQL:
# require "google/cloud/bigquery"
#
@@ -366,22 +382,22 @@
#
# sql = "SELECT name FROM [my_project:my_dataset.my_table]"
# data = bigquery.query sql, legacy_sql: true
#
# data.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
#
# @example Retrieve all rows: (See {QueryData#all})
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
#
# data = bigquery.query "SELECT name FROM `my_dataset.my_table`"
#
# data.all do |row|
- # puts row["name"]
+ # puts row[:name]
# end
#
# @example Query using positional query parameters:
# require "google/cloud/bigquery"
#
@@ -391,11 +407,11 @@
# "FROM `my_dataset.my_table`" \
# "WHERE id = ?",
# params: [1]
#
# data.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
#
# @example Query using named query parameters:
# require "google/cloud/bigquery"
#
@@ -405,11 +421,11 @@
# "FROM `my_dataset.my_table`" \
# "WHERE id = @id",
# params: { id: 1 }
#
# data.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
#
def query query, params: nil, max: nil, timeout: 10000, dryrun: nil,
cache: true, dataset: nil, project: nil, standard_sql: nil,
legacy_sql: nil
@@ -712,11 +728,11 @@
# "FROM `my_dataset.my_table`" \
# "WHERE time_of_date = @time",
# params: { time: fourpm }
#
# data.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
#
# @example Create Time with fractional seconds:
# require "google/cloud/bigquery"
#
@@ -727,14 +743,53 @@
# "FROM `my_dataset.my_table`" \
# "WHERE time_of_date >= @time",
# params: { time: precise_time }
#
# data.each do |row|
- # puts row["name"]
+ # puts row[:name]
# end
#
def time hour, minute, second
Bigquery::Time.new "#{hour}:#{minute}:#{second}"
+ end
+
+ ##
+ # Creates a new schema instance. An optional block may be given to
+ # configure the schema, otherwise the schema is returned empty and may
+ # be configured directly.
+ #
+ # The returned schema can be passed to {Dataset#load} using the `schema`
+ # option. However, for most use cases, the block yielded by
+ # {Dataset#load} is a more convenient way to configure the schema for
+ # the destination table.
+ #
+ # @yield [schema] a block for setting the schema
+ # @yieldparam [Schema] schema the object accepting the schema
+ #
+ # @return [Google::Cloud::Bigquery::Schema]
+ #
+ # @example
+ # require "google/cloud/bigquery"
+ #
+ # bigquery = Google::Cloud::Bigquery.new
+ #
+ # schema = bigquery.schema do |s|
+ # s.string "first_name", mode: :required
+ # s.record "cities_lived", mode: :repeated do |nested_schema|
+ # nested_schema.string "place", mode: :required
+ # nested_schema.integer "number_of_years", mode: :required
+ # end
+ # end
+ #
+ # dataset = bigquery.dataset "my_dataset"
+ #
+ # gs_url = "gs://my-bucket/file-name.csv"
+ # load_job = dataset.load "my_new_table", gs_url, schema: schema
+ #
+ def schema
+ s = Schema.from_gapi
+ yield s if block_given?
+ s
end
##
# @private New Project from a Google API Client object, using the
# same Credentials as this project.