
Google BigQuery: Insert New Data
This item inserts a new data row to a table on BigQuery.
Basic Configs
- Step Name
- Note
Configs for this Auto Step
- conf_auth
- C1: OAuth2 Setting *
- conf_projectId
- C2: Project ID *
- conf_datasetId
- C3: Dataset ID *
- conf_tableId
- C4: Table ID *
- conf_templateSuffix
- C5: Template Suffix (required if the table is a base template)#{EL}
- conf_field1
- C6F: Field 1
- conf_value1
- C6V: Value 1#{EL}
- conf_field2
- C7F: Field 2
- conf_value2
- C7V: Value 2#{EL}
- conf_field3
- C8F: Field 3
- conf_value3
- C8V: Value 3#{EL}
- conf_field4
- C9F: Field 4
- conf_value4
- C9V: Value 4#{EL}
- conf_field5
- C10F: Field 5
- conf_value5
- C10V: Value 5#{EL}
- conf_field6
- C11F: Field 6
- conf_value6
- C11V: Value 6#{EL}
- conf_field7
- C12F: Field 7
- conf_value7
- C12V: Value 7#{EL}
Notes
- The Google account that uses this modeling element must have permissions to update BigQuery tables, such as the bigquery.dataEditor role. For details of BigQuery permissions/roles see the Google Cloud IAM Documentation.
- Supported data types are: STRING, BYTES, INTEGER, FLOAT, NUMERIC, BIGNUMERIC, BOOLEAN, TIMESTAMP, DATE, TIME, DATETIME.
- Each value must be set in the format that matches its field’s data type defined by the table schema. For example, to set a value in a DATETIME field, the input must be something like 2015-03-17 10:20:00. For more details see the BigQuery Documentation.
Capture

See also
Script (click to open)
- An XML file that contains the code below is available to download
- google-bigquery-data-insert.xml (C) Questetra, Inc. (MIT License)
- If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step
// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
// - Token Endpoint URL: https://oauth2.googleapis.com/token
// - Scope: https://www.googleapis.com/auth/bigquery
// - Consumer Key: (Get by Google Developer Console)
// - Consumer Secret: (Get by Google Developer Console)
const FIELD_NUM = 7; // 扱えるフィールドの数
function main(){
//// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
const oauth2 = configs.getObject("conf_auth");
const projectId = configs.get("conf_projectId");
const datasetId = configs.get("conf_datasetId");
const tableId = configs.get("conf_tableId");
const templateSuffix = configs.get("conf_templateSuffix");
isValidProjectId( projectId );
isValidDatasetId( datasetId );
isValidTableId( `${tableId}${templateSuffix}` );
const dataObj = retrieveDataObj();
//// == 演算 / Calculating ==
insertData( oauth2, projectId, datasetId, tableId, templateSuffix, dataObj );
}
/**
* プロジェクト ID が不正な文字列であればエラー
* @param {String} projectId プロジェクト ID
*/
function isValidProjectId( projectId ) {
const reg = new RegExp( '^[a-zA-Z][0-9a-zA-Z-]{4,28}[0-9a-zA-Z]$' );
if ( !reg.test(projectId) ) {
throw new Error("Invalid Project ID.");
}
}
/**
* データセット ID が不正な文字列であればエラー
* @param {String} datasetId データセット ID
*/
function isValidDatasetId( datasetId ) {
const reg = new RegExp( '^\\w{1,1024}$' );
if ( !reg.test(datasetId) ) {
throw new Error("Invalid Dataset ID.");
}
}
/**
* テーブル ID (サフィックスを含む)が不正な文字列であればエラー
* @param {String} tableId テーブル ID
*/
function isValidTableId( tableId ) {
if( encodeURIComponent(tableId).replace(/%../g,"x").length > 1024 ) {
throw new Error("Invalid Table ID and/or Template Suffix. Too long.");
}
const reg = new RegExp( '^[\\p{L}\\p{M}\\p{N}\\p{Pc}\\p{Pd}\\p{Zs}]+$' , 'u');
if ( !reg.test(tableId) ) {
throw new Error("Invalid Table ID and/or Template Suffix. Includes an invalid character.");
}
}
/**
* config からフィールドと値の組を読み出し、JSON オブジェクトを返す
* @return {Object} dataObj データオブジェクト
*/
function retrieveDataObj() {
const dataObj = {};
for (let i = 0; i < FIELD_NUM; i++) {
const field = configs.get(`conf_field${i+1}`);
let value = configs.get(`conf_value${i+1}`);
if ( field === "" || field === null ) { // フィールドが空
continue;
}
if (field.length > 300) { // フィールド名が長すぎる
throw new Error(`Invalid Field Name: ${field}. Too long.`);
}
if ( dataObj[field] !== undefined ) { // フィールドコードの指定が重複
throw new Error(`The same field ${field} is set multiple times.`);
}
if ( value === "" || value === null ) { // 値が空
value = null;
}
dataObj[field] = value;
}
return dataObj;
}
/**
* データ追加の POST リクエストを送信する
* @param {AuthSettingWrapper} oauth2 OAuth2 認証設定
* @param {String} projectId プロジェクト ID
* @param {String} datasetId データセット ID
* @param {String} tableId テーブル ID
* @param {String} templateSuffix テンプレートサフィックス
* @param {Object} dataObj データオブジェクト
*/
function insertData( oauth2, projectId, datasetId, tableId, templateSuffix, dataObj ) {
const uri = `https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/insertAll`;
const body = {};
body["kind"] = "bigquery#tableDataInsertAllRequest";
body["rows"] = [{ "json": dataObj }];
if ( templateSuffix !== "" && templateSuffix !== null ) {
body["templateSuffix"] = templateSuffix;
}
const response = httpClient.begin()
.authSetting( oauth2 )
.body( JSON.stringify(body), "application/json; charset=UTF-8" )
.post(encodeURI(uri));
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if ( status >= 300 ) { // when error thrown
engine.log(responseStr);
throw new Error(`Failed to insert data. status: ${status}`);
}
const errors = JSON.parse(responseStr)["insertErrors"];
if ( errors !== undefined ) {
engine.log(JSON.stringify(errors));
throw new Error("Failed to insert data.");
}
}
Pingback: Adding Data to BigQuery Automatically – Questetra Support