Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs(samples): adds auth view, range partition, query external samples * docs(samples): adds relax column and create table * docs(samples): update samples * docs(samples): adds inserting data types sample * docs(samples): update header * docs(samples): updates samples * docs(samples): updates samples Co-authored-by: Justin Beckwith <justin.beckwith@gmail.com>
- Loading branch information
1 parent
a2e34ef
commit 93945dd
Showing
8 changed files
with
548 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
function main( | ||
projectId = 'my_project_id', | ||
sourceDatasetId = 'shared_views', | ||
sourceTableId = 'my_source_table', | ||
sharedDatasetId = 'shared_views', | ||
sharedViewId = 'github_analyst_view' | ||
) { | ||
// [START bigquery_authorized_view_tutorial] | ||
async function authorizedViewTutorial() { | ||
// [START bigquery_avt_create_source_dataset] | ||
const {BigQuery} = require('@google-cloud/bigquery'); | ||
const bigquery = new BigQuery(); | ||
|
||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const projectId = "my_project_id"; | ||
// const sourceDatasetId = "my_source_dataset"; | ||
// const sourceTableId = "my_source_table"; | ||
// const sharedDatasetId = "shared_views"; | ||
// const sharedViewId = "my_view"; | ||
|
||
// Make API request to create dataset | ||
const [sourceDataset] = await bigquery.createDataset(sourceDatasetId); | ||
console.log(`Source dataset ${sourceDataset.id} created.`); | ||
|
||
const destinationTable = sourceDataset.table(sourceTableId); | ||
|
||
const query = `SELECT commit, author, committer, repo_name | ||
FROM \`bigquery-public-data.github_repos.commits\` | ||
LIMIT 1000`; | ||
|
||
// For all options, see https://cloud.google.com/bigquery/docs/reference/v2/tables#resource | ||
const options = { | ||
query: query, | ||
destination: destinationTable, | ||
}; | ||
|
||
// Make API request to populate a source table | ||
await bigquery.query(options); | ||
// [END bigquery_avt_create_source_table] | ||
// [START bigquery_avt_create_shared_dataset] | ||
// Create a separate dataset to store your view | ||
|
||
// Make API request to create a new dataset | ||
const [sharedDataset] = await bigquery.createDataset(sharedDatasetId); | ||
|
||
console.log(`Dataset ${sharedDataset.id} created.`); | ||
// [END bigquery_avt_create_shared_dataset] | ||
// [START bigquery_avt_create_view] | ||
// Create the view in the new dataset | ||
|
||
const viewQuery = `SELECT | ||
commit, author.name as author, | ||
committer.name as committer, repo_name | ||
FROM | ||
\`${projectId}.${sourceDatasetId}.${sourceTableId}\``; | ||
|
||
const viewOptions = { | ||
view: {query: viewQuery, useLegacySql: false}, | ||
}; | ||
|
||
// Make API request to create the view | ||
const [view] = await sharedDataset.createTable(sharedViewId, viewOptions); | ||
|
||
const viewId = view.metadata.id; | ||
console.log(`View ${viewId} created.`); | ||
// [END bigquery_avt_create_view] | ||
// [START bigquery_avt_shared_dataset_access] | ||
// Assign access controls to the dataset containing the view | ||
|
||
// Note to user: This is a group email for testing purposes. Replace with | ||
// your own group email address when running this code. | ||
const analyst_group_email = 'example-analyst-group@google.com'; | ||
|
||
const analystAccessEntry = { | ||
role: 'READER', | ||
groupByEmail: analyst_group_email, | ||
}; | ||
|
||
// Make API request to retrieve dataset metadata | ||
const [sharedMetadata] = await sharedDataset.getMetadata(); | ||
|
||
const sharedAccessEntries = sharedMetadata.access; | ||
sharedAccessEntries.push(analystAccessEntry); | ||
|
||
sharedMetadata.access = sharedAccessEntries; | ||
|
||
// Make API request to update dataset metadata | ||
const [updatedSharedMetadata] = await sharedDataset.setMetadata( | ||
sharedMetadata | ||
); | ||
|
||
console.log(`Dataset ${updatedSharedMetadata.id} updated.`); | ||
// [END bigquery_avt_shared_dataset_access] | ||
// [START bigquery_avt_source_dataset_access] | ||
// Authorize the view to access the source dataset | ||
|
||
const viewReference = { | ||
projectId: projectId, | ||
datasetId: sharedDatasetId, | ||
tableId: sharedViewId, | ||
}; | ||
|
||
const datasetAccessEntry = {view: viewReference}; | ||
|
||
// Make API request to retrieve source dataset metadata | ||
const [sourceMetadata] = await sourceDataset.getMetadata(); | ||
|
||
const sourceAccessEntries = sourceMetadata.access; | ||
sourceAccessEntries.push(datasetAccessEntry); | ||
|
||
sourceMetadata.access = sourceAccessEntries; | ||
|
||
// Make API request to update source dataset metadata | ||
const [updatedSourceMetadata] = await sourceDataset.setMetadata( | ||
sourceMetadata | ||
); | ||
|
||
console.log(`Dataset ${updatedSourceMetadata.id} updated.`); | ||
// [END bigquery_avt_source_dataset_access] | ||
} | ||
// [END bigquery_authorized_view_tutorial] | ||
authorizedViewTutorial(); | ||
} | ||
|
||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
function main(datasetId = 'my_dataset', tableId = 'my_table') { | ||
// [START bigquery_create_table_range_partitioned] | ||
// Import the Google Cloud client library | ||
const {BigQuery} = require('@google-cloud/bigquery'); | ||
const bigquery = new BigQuery(); | ||
|
||
async function createTableRangePartitioned() { | ||
// Creates a new integer range partitioned table named "my_table" | ||
// in "my_dataset". | ||
|
||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const datasetId = "my_dataset"; | ||
// const tableId = "my_table"; | ||
|
||
const schema = [ | ||
{name: 'fullName', type: 'STRING'}, | ||
{name: 'city', type: 'STRING'}, | ||
{name: 'zipcode', type: 'INTEGER'}, | ||
]; | ||
|
||
// To use integer range partitioning, select a top-level REQUIRED or | ||
// NULLABLE column with INTEGER / INT64 data type. Values that are | ||
// outside of the range of the table will go into the UNPARTITIONED | ||
// partition. Null values will be in the NULL partition. | ||
const rangePartition = { | ||
field: 'zipcode', | ||
range: { | ||
start: 0, | ||
end: 100000, | ||
interval: 10, | ||
}, | ||
}; | ||
|
||
// For all options, see https://cloud.google.com/bigquery/docs/reference/v2/tables#resource | ||
const options = { | ||
schema: schema, | ||
rangePartitioning: rangePartition, | ||
}; | ||
|
||
// Create a new table in the dataset | ||
const [table] = await bigquery | ||
.dataset(datasetId) | ||
.createTable(tableId, options); | ||
|
||
console.log(`Table ${table.id} created with integer range partitioning: `); | ||
console.log(table.metadata.rangePartitioning); | ||
} | ||
// [END bigquery_create_table_range_partitioned] | ||
createTableRangePartitioned(datasetId, tableId); | ||
} | ||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
function main(datasetId = 'my_dataset', tableId = 'my_table') { | ||
// [START bigquery_inserting_data_types] | ||
// Import the Google Cloud client library | ||
const {BigQuery} = require('@google-cloud/bigquery'); | ||
const bigquery = new BigQuery(); | ||
|
||
async function instertingDataTypes() { | ||
// Inserts data of various BigQuery-supported types into a table. | ||
|
||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const datasetId = 'my_dataset'; | ||
// const tableId = 'my_table'; | ||
|
||
// Describe the schema of the table | ||
// For more information on supported data types, see | ||
// https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types | ||
const schema = [ | ||
{ | ||
name: 'name', | ||
type: 'STRING', | ||
}, | ||
{ | ||
name: 'age', | ||
type: 'INTEGER', | ||
}, | ||
{ | ||
name: 'school', | ||
type: 'BYTES', | ||
}, | ||
{ | ||
name: 'location', | ||
type: 'GEOGRAPHY', | ||
}, | ||
{ | ||
name: 'measurements', | ||
mode: 'REPEATED', | ||
type: 'FLOAT', | ||
}, | ||
{ | ||
name: 'datesTimes', | ||
type: 'RECORD', | ||
fields: [ | ||
{ | ||
name: 'day', | ||
type: 'DATE', | ||
}, | ||
{ | ||
name: 'firstTime', | ||
type: 'DATETIME', | ||
}, | ||
{ | ||
name: 'secondTime', | ||
type: 'TIME', | ||
}, | ||
{ | ||
name: 'thirdTime', | ||
type: 'TIMESTAMP', | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
// For all options, see https://cloud.google.com/bigquery/docs/reference/v2/tables#resource | ||
const options = { | ||
schema: schema, | ||
}; | ||
|
||
// Create a new table in the dataset | ||
const [table] = await bigquery | ||
.dataset(datasetId) | ||
.createTable(tableId, options); | ||
|
||
console.log(`Table ${table.id} created.`); | ||
|
||
// The DATE type represents a logical calendar date, independent of time zone. | ||
// A DATE value does not represent a specific 24-hour time period. | ||
// Rather, a given DATE value represents a different 24-hour period when | ||
// interpreted in different time zones, and may represent a shorter or longer | ||
// day during Daylight Savings Time transitions. | ||
const bqDate = bigquery.date('2019-1-12'); | ||
// A DATETIME object represents a date and time, as they might be | ||
// displayed on a calendar or clock, independent of time zone. | ||
const bqDatetime = bigquery.datetime('2019-02-17 11:24:00.000'); | ||
// A TIME object represents a time, as might be displayed on a watch, | ||
// independent of a specific date and timezone. | ||
const bqTime = bigquery.time('14:00:00'); | ||
// A TIMESTAMP object represents an absolute point in time, | ||
// independent of any time zone or convention such as Daylight | ||
// Savings Time with microsecond precision. | ||
const bqTimestamp = bigquery.timestamp('2020-04-27T18:07:25.356Z'); | ||
const bqGeography = bigquery.geography('POINT(1 2)'); | ||
const schoolBuffer = Buffer.from('Test University'); | ||
|
||
// Rows to be inserted into table | ||
const rows = [ | ||
{ | ||
name: 'Tom', | ||
age: '30', | ||
location: bqGeography, | ||
school: schoolBuffer, | ||
measurements: [50.05, 100.5], | ||
datesTimes: { | ||
day: bqDate, | ||
firstTime: bqDatetime, | ||
secondTime: bqTime, | ||
thirdTime: bqTimestamp, | ||
}, | ||
}, | ||
{ | ||
name: 'Ada', | ||
age: '35', | ||
measurements: [30.08, 121.7], | ||
}, | ||
]; | ||
|
||
// Insert data into table | ||
await bigquery | ||
.dataset(datasetId) | ||
.table(tableId) | ||
.insert(rows); | ||
|
||
console.log(`Inserted ${rows.length} rows`); | ||
} | ||
// [END bigquery_inserting_data_types] | ||
instertingDataTypes(); | ||
} | ||
main(...process.argv.slice(2)); |
Oops, something went wrong.