Google BigQuery
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
建立 BigQuery 資料集
function createDataSet() {
// Replace this value with the project ID listed in the Google
// Cloud Platform project.
var projectId = 'INSERT_PROJECT_ID_HERE';
var dataSetId = 'INSERT_DATASET_ID_HERE';
var dataSet = BigQuery.newDataset();
dataSet.id = dataSetId;
dataSet.friendlyName = 'Fruit prices';
dataSet.datasetReference = BigQuery.newDatasetReference();
dataSet.datasetReference.projectId = projectId;
dataSet.datasetReference.datasetId = dataSetId;
dataSet = BigQuery.Datasets.insert(dataSet, projectId);
console.log('Data set with ID = %s, Name = %s created.', dataSet.id,
dataSet.friendlyName);
}
建立 BigQuery 資料表
function createTable() {
// Replace this value with the project ID listed in the Google
// Cloud Platform project.
var projectId = 'INSERT_PROJECT_ID_HERE';
var dataSetId = 'INSERT_DATASET_ID_HERE';
var tableId = 'INSERT_TABLE_ID_HERE';
var table = BigQuery.newTable();
var schema = BigQuery.newTableSchema();
var nameFieldSchema = BigQuery.newTableFieldSchema();
nameFieldSchema.description = 'Name';
nameFieldSchema.name = 'Name';
nameFieldSchema.type = 'STRING';
var ageFieldSchema = BigQuery.newTableFieldSchema();
ageFieldSchema.description = 'Price';
ageFieldSchema.name = 'Price';
ageFieldSchema.type = 'FLOAT';
schema.fields = [
nameFieldSchema, ageFieldSchema
];
table.schema = schema;
table.id = tableId;
table.friendlyName = 'Fruit prices';
table.tableReference = BigQuery.newTableReference();
table.tableReference.datasetId = dataSetId;
table.tableReference.projectId = projectId;
table.tableReference.tableId = tableId;
table = BigQuery.Tables.insert(table, projectId, dataSetId);
console.log('Data table with ID = %s, Name = %s created.',
table.id, table.friendlyName);
}
匯入至 BigQuery 資料表
function importData() {
// Replace this value with the project ID listed in the Google
// Cloud Platform project.
var projectId = 'INSERT_PROJECT_ID_HERE';
var dataSetId = 'INSERT_DATASET_ID_HERE';
var tableId = 'INSERT_TABLE_ID_HERE';
var insertAllRequest = BigQuery.newTableDataInsertAllRequest();
insertAllRequest.rows = [];
var row1 = BigQuery.newTableDataInsertAllRequestRows();
row1.insertId = 1;
row1.json = {
'Name': 'Orange',
'Price': 3.34
};
insertAllRequest.rows.push(row1);
var row2 = BigQuery.newTableDataInsertAllRequestRows();
row2.insertId = 2;
row2.json = {
'Name': 'Grape',
'Price': 5.48
};
insertAllRequest.rows.push(row2);
var row3 = BigQuery.newTableDataInsertAllRequestRows();
row3.insertId = 3;
row3.json = {
'Name': 'Apple',
'Price': 2.50
};
insertAllRequest.rows.push(row3);
var result = BigQuery.Tabledata.insertAll(insertAllRequest, projectId,
dataSetId, tableId);
if (result.insertErrors != null) {
var allErrors = [];
for (var i = 0; i < result.insertErrors.length; i++) {
var insertError = result.insertErrors[i];
allErrors.push(Utilities.formatString('Error inserting item: %s',
insertError.index));
for (var j = 0; j < insertError.errors.length; j++) {
var error = insertError.errors[j];
allErrors.push(Utilities.formatString('- ' + error));
}
}
console.log(allErrors.join('\n'));
} else {
console.log(Utilities.formatString('%s data rows inserted successfully.',
insertAllRequest.rows.length));
}
}
對 BigQuery 資料表執行查詢
function queryDataTable() {
// Replace this value with the project ID listed in the Google
// Cloud Platform project.
var projectId = 'INSERT_PROJECT_ID_HERE';
var dataSetId = 'INSERT_DATASET_ID_HERE';
var tableId = 'INSERT_TABLE_ID_HERE';
var fullTableName = projectId + ':' + dataSetId + '.' + tableId;
var queryRequest = BigQuery.newQueryRequest();
queryRequest.query = 'select Name, Price from [' + fullTableName + '];';
var query = BigQuery.Jobs.query(queryRequest, projectId);
if (query.jobComplete) {
for (var i = 0; i < query.rows.length; i++) {
var row = query.rows[i];
var values = [];
for (var j = 0; j < row.f.length; j++) {
values.push(row.f[j].v);
}
console.log(values.join(','));
}
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\u003cp\u003eThis webpage demonstrates how to create a BigQuery dataset using Google Apps Script.\u003c/p\u003e\n"],["\u003cp\u003eThe code snippets showcase creating and populating a BigQuery table with sample fruit and price data.\u003c/p\u003e\n"],["\u003cp\u003eFunctionality is provided to import data and subsequently query the table using Google Apps Script.\u003c/p\u003e\n"],["\u003cp\u003eProvided examples include defining a schema, inserting rows, and running SQL-like queries to retrieve data.\u003c/p\u003e\n"]]],[],null,["# Google BigQuery\n\nCreate a BigQuery data set\n--------------------------\n\n```gdscript\nfunction createDataSet() {\n // Replace this value with the project ID listed in the Google\n // Cloud Platform project.\n var projectId = 'INSERT_PROJECT_ID_HERE';\n\n var dataSetId = 'INSERT_DATASET_ID_HERE';\n\n var dataSet = BigQuery.newDataset();\n dataSet.id = dataSetId;\n dataSet.friendlyName = 'Fruit prices';\n dataSet.datasetReference = BigQuery.newDatasetReference();\n dataSet.datasetReference.projectId = projectId;\n dataSet.datasetReference.datasetId = dataSetId;\n\n dataSet = BigQuery.Datasets.insert(dataSet, projectId);\n console.log('Data set with ID = %s, Name = %s created.', dataSet.id,\n dataSet.friendlyName);\n}\n```\n\nCreate a BigQuery data table\n----------------------------\n\n```gdscript\nfunction createTable() {\n // Replace this value with the project ID listed in the Google\n // Cloud Platform project.\n var projectId = 'INSERT_PROJECT_ID_HERE';\n\n var dataSetId = 'INSERT_DATASET_ID_HERE';\n var tableId = 'INSERT_TABLE_ID_HERE';\n\n var table = BigQuery.newTable();\n var schema = BigQuery.newTableSchema();\n\n var nameFieldSchema = BigQuery.newTableFieldSchema();\n nameFieldSchema.description = 'Name';\n nameFieldSchema.name = 'Name';\n nameFieldSchema.type = 'STRING';\n\n var ageFieldSchema = BigQuery.newTableFieldSchema();\n ageFieldSchema.description = 'Price';\n ageFieldSchema.name = 'Price';\n ageFieldSchema.type = 'FLOAT';\n\n schema.fields = [\n nameFieldSchema, ageFieldSchema\n ];\n\n table.schema = schema;\n table.id = tableId;\n table.friendlyName = 'Fruit prices';\n\n table.tableReference = BigQuery.newTableReference();\n table.tableReference.datasetId = dataSetId;\n table.tableReference.projectId = projectId;\n table.tableReference.tableId = tableId;\n\n table = BigQuery.Tables.insert(table, projectId, dataSetId);\n\n console.log('Data table with ID = %s, Name = %s created.',\n table.id, table.friendlyName);\n}\n```\n\nImport into BigQuery data table\n-------------------------------\n\n```transact-sql\nfunction importData() {\n // Replace this value with the project ID listed in the Google\n // Cloud Platform project.\n var projectId = 'INSERT_PROJECT_ID_HERE';\n\n var dataSetId = 'INSERT_DATASET_ID_HERE';\n var tableId = 'INSERT_TABLE_ID_HERE';\n\n var insertAllRequest = BigQuery.newTableDataInsertAllRequest();\n insertAllRequest.rows = [];\n\n var row1 = BigQuery.newTableDataInsertAllRequestRows();\n row1.insertId = 1;\n row1.json = {\n 'Name': 'Orange',\n 'Price': 3.34\n };\n insertAllRequest.rows.push(row1);\n\n var row2 = BigQuery.newTableDataInsertAllRequestRows();\n row2.insertId = 2;\n row2.json = {\n 'Name': 'Grape',\n 'Price': 5.48\n };\n insertAllRequest.rows.push(row2);\n\n var row3 = BigQuery.newTableDataInsertAllRequestRows();\n row3.insertId = 3;\n row3.json = {\n 'Name': 'Apple',\n 'Price': 2.50\n };\n insertAllRequest.rows.push(row3);\n\n var result = BigQuery.Tabledata.insertAll(insertAllRequest, projectId,\n dataSetId, tableId);\n\n if (result.insertErrors != null) {\n var allErrors = [];\n\n for (var i = 0; i \u003c result.insertErrors.length; i++) {\n var insertError = result.insertErrors[i];\n allErrors.push(Utilities.formatString('Error inserting item: %s',\n insertError.index));\n\n for (var j = 0; j \u003c insertError.errors.length; j++) {\n var error = insertError.errors[j];\n allErrors.push(Utilities.formatString('- ' + error));\n }\n }\n console.log(allErrors.join('\\n'));\n } else {\n console.log(Utilities.formatString('%s data rows inserted successfully.',\n insertAllRequest.rows.length));\n }\n}\n```\n\nRun query against BigQuery data table\n-------------------------------------\n\n```transact-sql\nfunction queryDataTable() {\n // Replace this value with the project ID listed in the Google\n // Cloud Platform project.\n var projectId = 'INSERT_PROJECT_ID_HERE';\n\n var dataSetId = 'INSERT_DATASET_ID_HERE';\n var tableId = 'INSERT_TABLE_ID_HERE';\n\n var fullTableName = projectId + ':' + dataSetId + '.' + tableId;\n\n var queryRequest = BigQuery.newQueryRequest();\n queryRequest.query = 'select Name, Price from [' + fullTableName + '];';\n var query = BigQuery.Jobs.query(queryRequest, projectId);\n\n if (query.jobComplete) {\n for (var i = 0; i \u003c query.rows.length; i++) {\n var row = query.rows[i];\n var values = [];\n for (var j = 0; j \u003c row.f.length; j++) {\n values.push(row.f[j].v);\n }\n console.log(values.join(','));\n }\n }\n}\n```"]]